Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #302 from OutiServer/develop
Browse files Browse the repository at this point in the history
release 3.2.0
  • Loading branch information
KenCir authored Aug 29, 2022
2 parents c9e2ea9 + f934cc8 commit fa68d76
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 3 deletions.
45 changes: 45 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"minecraft-server-util": "^5.3.1",
"ms": "^2.1.3",
"node-cron": "^3.0.2",
"quickchart-js": "^3.1.0",
"twitter": "^1.7.1",
"uuid": "^8.3.2",
"zlib-sync": "^0.1.7"
Expand Down
8 changes: 7 additions & 1 deletion src/commands/main/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
.setTitle(`${client.user.tag} helpページ`)
.addFields([
{ name: 'main', value: client.commands.filter(x => x.info.category == 'main').map((x) => '`' + x.info.name + '`').join(', ') },
{ name: 'casino', value: client.commands.filter(x => x.info.category == 'casino').map((x) => '`' + x.info.name + '`').join(', ') },
{ name: 'study', value: client.commands.filter(x => x.info.category == 'study').map((x) => '`' + x.info.name + '`').join(', ') },
])
.setTimestamp(),
);
Expand All @@ -39,6 +39,12 @@ module.exports = {
.setDescription(`${codeBlock(client.commands.filter(x => x.info.category == 'main').map((x) => `/${x.info.name}: ${x.info.description}`).join('\n'))}`)
.setTimestamp(),
);
embeds.push(
new EmbedBuilder()
.setTitle('study')
.setDescription(`${codeBlock(client.commands.filter(x => x.info.category == 'study').map((x) => `/${x.info.name}: ${x.info.description}`).join('\n'))}`)
.setTimestamp(),
);

if (interaction.member.roles.cache.has('822852335322923060') || interaction.member.roles.cache.has('771015602180587571') || interaction.member.permissions.has('ADMINISTRATOR')) {
embeds[0].addFields([
Expand Down
41 changes: 41 additions & 0 deletions src/commands/study/graph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { SlashCommandBuilder } = require('discord.js');
const QuickChart = require('quickchart-js');

module.exports = {
info: {
name: 'graph',
description: '勉強時間をグラフで表示',
category: 'study',
deferReply: true,
},

data: new SlashCommandBuilder()
.setName('graph')
.setDescription('勉強時間をグラフで表示'),

/**
* @param {import('../../Bot')} client
* @param {import('discord.js').CommandInteraction} interaction
*/

run: async function (client, interaction) {
const date = new Date();
const all = client.database.getStudyMonth(interaction.user.id, date.getFullYear(), date.getMonth() + 1);
if (!all || all.length < 1) return interaction.followUp('あなたはまだデータがないようです。');
const labels = [];
const time = [];

for (const data of all) {
labels.push(`${date.getMonth() + 1}${data.day}日`);
time.push(data.time);
}

const chart = new QuickChart();
chart.setConfig({
type: 'bar',
data: { labels: labels, datasets: [{ label: '勉強時間', data: time }] },
});
const url = await chart.getShortUrl();
await interaction.followUp(url);
},
};
32 changes: 32 additions & 0 deletions src/commands/study/studytimeadd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { SlashCommandBuilder } = require('discord.js');

module.exports = {
info: {
name: 'studytimeadd',
description: '勉強時間を追加',
category: 'study',
deferReply: true,
},

data: new SlashCommandBuilder()
.setName('studytimeadd')
.setDescription('勉強時間を追加')
.addIntegerOption(option => option
.setName('time')
.setDescription('勉強時間を分単位で追加する')
.setRequired(true)),

/**
* @param {import('../../Bot')} client
* @param {import('discord.js').CommandInteraction} interaction
*/

run: async function (client, interaction) {
const date = new Date();
const time = interaction.options.getInteger('time', true);

client.database.addStudyTime(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate(), time);

await interaction.followUp(`${time}分、今日の勉強時間に追加しました`);
},
};
51 changes: 51 additions & 0 deletions src/database/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Database {
this.sql.prepare('CREATE TABLE IF NOT EXISTS emoji_uses (emoji_id TEXT PRIMARY KEY, count INTEGER NOT NULL);').run();
this.sql.prepare('CREATE UNIQUE INDEX IF NOT EXISTS idx_emoji_uses_id ON emoji_uses (emoji_id);').run();

this.sql.prepare('CREATE TABLE IF NOT EXISTS study_times (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, year INTEGER NOT NULL, month INTEGER NOT NULL, day INTEGER NOT NULL, time INTEGER NOT NULL);').run();
this.sql.prepare('CREATE UNIQUE INDEX IF NOT EXISTS idx_study_times_id ON study_times (id);').run();

this.sql.pragma('synchronous = 1');
this.sql.pragma('journal_mode = wal');
}
Expand Down Expand Up @@ -116,6 +119,54 @@ class Database {
this.sql.prepare('UPDATE emoji_uses SET count = count + ? WHERE emoji_id = ?;').run(count, emojiId);
}
}

/**
*
* @param {string} userId
* @param {number} year
* @param {number} month
* @param {number} day
* @returns {{ id: string, user_id: string, year: number, month: number, day: number, time: number } | undefined}
*/
getStudy(userId, year, month, day) {
return this.sql.prepare('SELECT * FROM study_times WHERE user_id = ? AND year = ? AND month = ? AND day = ?;').get(userId, year, month, day);
}

/**
*
* @param {string} userId
* @param {number} year
* @param {number} month
* @param {number} day
*/
addStudy(userId, year, month, day) {
if (this.getStudy(userId, year, month, day)) return;
this.sql.prepare('INSERT INTO study_times VALUES (?, ?, ?, ?, ?, ?);').run(`${userId}-${year}-${month}-${day}`, userId, year, month, day, 0);
}

/**
*
* @param {string} userId
* @param {number} year
* @param {number} month
* @param {number} day
* @param {number} time
*/
addStudyTime(userId, year, month, day, time) {
if (!this.getStudy(userId, year, month, day)) return;
this.sql.prepare('UPDATE study_times SET time = time + ? WHERE user_id = ? AND year = ? AND month = ? AND day = ?;').run(time, userId, year, month, day);
}

/**
*
* @param {string} userId
* @param {number} year
* @param {number} month
* @returns {Array<{ id: string, user_id: string, year: number, month: number, day: number, time: number }>}
*/
getStudyMonth(userId, year, month) {
return this.sql.prepare('SELECT * FROM study_times WHERE user_id = ? AND year = ? AND month = ? ORDER BY day ASC;').all(userId, year, month);
}
}

module.exports = Database;
5 changes: 5 additions & 0 deletions src/events/discord/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,10 @@ module.exports = async (client, interaction) => {
if (cmd.info.deferReply) await interaction.deferReply();

cmd.run(client, interaction);

const date = new Date();
if (!client.database.getStudy(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate())) {
client.database.addStudy(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate());
}
}
};
5 changes: 5 additions & 0 deletions src/events/discord/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ module.exports = async (client, message) => {
else client.speakers.get(message.guildId).addSpearkQueue(message.content, message.id, speaker.speaker_id);
}
}

const date = new Date();
if (!client.database.getStudy(message.author.id, date.getFullYear(), date.getMonth() + 1, date.getDate())) {
client.database.addStudy(message.author.id, date.getFullYear(), date.getMonth() + 1, date.getDate());
}
};
12 changes: 10 additions & 2 deletions src/events/discord/voiceStateUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ module.exports = async (client, oldMember, newMember) => {
await client.channels.cache.get('972852368620261416').send(`${newMember.member.user.tag}さんが${newMember.channel.name}で学習を開始しました!`);
}
else if (newMember.channelId === null) {
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${ms(Date.now() - client.study_times.get(oldMember.member.id))}でした`);
if (!client.study_times.get(oldMember.member.id)) return;
const result = ms(Date.now() - client.study_times.get(oldMember.member.id));
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${result.replace('h', '時間').replace('m', '分').replace('s', '秒')}でした`);
const date = new Date();
client.database.addStudyTime(oldMember.member.id, date.getFullYear(), date.getMonth() + 1, date.getDate(), Math.floor((Date.now() - client.study_times.get(oldMember.member.id)) / 60000));
client.study_times.delete(oldMember.member.id);
}
else if (newMember.channelId !== oldMember.channelId && oldMember.channel?.parentId !== '972467676951752734') {
client.study_times.set(newMember.member.id, Date.now());
await client.channels.cache.get('972852368620261416').send(`${newMember.member.user.tag}さんが${newMember.channel.name}で学習を開始しました!`);
}
else if (newMember.channelId !== oldMember.channelId && oldMember.channel?.parentId === '972467676951752734') {
await client.channels.cache.get('972852368620261416').send(`${oldMember.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${ms(Date.now() - client.study_times.get(oldMember.member.id))}でした`);
if (!client.study_times.get(oldMember.member.id)) return;
const result = ms(Date.now() - client.study_times.get(oldMember.member.id));
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${result.replace('h', '時間').replace('m', '分').replace('s', '秒')}でした`);
const date = new Date();
client.database.addStudyTime(oldMember.member.id, date.getFullYear(), date.getMonth() + 1, date.getDate(), Math.floor((Date.now() - client.study_times.get(oldMember.member.id)) / 60000));
client.study_times.delete(oldMember.member.id);
}
}
Expand Down

0 comments on commit fa68d76

Please sign in to comment.