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

Commit c6a8cce

Browse files
committed
学び舎での機能追加
1 parent c9e2ea9 commit c6a8cce

8 files changed

Lines changed: 189 additions & 2 deletions

File tree

package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"minecraft-server-util": "^5.3.1",
2929
"ms": "^2.1.3",
3030
"node-cron": "^3.0.2",
31+
"quickchart-js": "^3.1.0",
3132
"twitter": "^1.7.1",
3233
"uuid": "^8.3.2",
3334
"zlib-sync": "^0.1.7"

src/commands/study/graph.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { SlashCommandBuilder } = require('discord.js');
2+
const QuickChart = require('quickchart-js');
3+
4+
module.exports = {
5+
info: {
6+
name: 'graph',
7+
description: '勉強時間をグラフで表示',
8+
category: 'study',
9+
deferReply: true,
10+
},
11+
12+
data: new SlashCommandBuilder()
13+
.setName('graph')
14+
.setDescription('勉強時間をグラフで表示'),
15+
16+
/**
17+
* @param {import('../../Bot')} client
18+
* @param {import('discord.js').CommandInteraction} interaction
19+
*/
20+
21+
run: async function (client, interaction) {
22+
const date = new Date();
23+
const all = client.database.getStudyMonth(interaction.user.id, date.getFullYear(), date.getMonth() + 1);
24+
if (!all || all.length < 1) return interaction.followUp('あなたはまだデータがないようです。');
25+
const labels = [];
26+
const time = [];
27+
28+
for (const data of all) {
29+
labels.push(`${date.getMonth() + 1}${data.day}日`);
30+
time.push(data.time);
31+
}
32+
33+
const chart = new QuickChart();
34+
chart.setConfig({
35+
type: 'bar',
36+
data: { labels: labels, datasets: [{ label: '勉強時間', data: time }] },
37+
});
38+
const url = await chart.getShortUrl();
39+
await interaction.followUp(url);
40+
},
41+
};

src/commands/study/studytimeadd.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { SlashCommandBuilder } = require('discord.js');
2+
3+
module.exports = {
4+
info: {
5+
name: 'studytimeadd',
6+
description: '勉強時間を追加',
7+
category: 'study',
8+
deferReply: true,
9+
},
10+
11+
data: new SlashCommandBuilder()
12+
.setName('studytimeadd')
13+
.setDescription('勉強時間を追加')
14+
.addIntegerOption(option => option
15+
.setName('time')
16+
.setDescription('勉強時間を分単位で追加する')),
17+
18+
/**
19+
* @param {import('../../Bot')} client
20+
* @param {import('discord.js').CommandInteraction} interaction
21+
*/
22+
23+
run: async function (client, interaction) {
24+
const date = new Date();
25+
const time = interaction.options.getInteger('time', true);
26+
27+
client.database.addStudyTime(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate(), time);
28+
29+
await interaction.followUp(`${time}分、今日の勉強時間に追加しました`);
30+
},
31+
};

src/database/Database.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Database {
1313
this.sql.prepare('CREATE TABLE IF NOT EXISTS emoji_uses (emoji_id TEXT PRIMARY KEY, count INTEGER NOT NULL);').run();
1414
this.sql.prepare('CREATE UNIQUE INDEX IF NOT EXISTS idx_emoji_uses_id ON emoji_uses (emoji_id);').run();
1515

16+
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();
17+
this.sql.prepare('CREATE UNIQUE INDEX IF NOT EXISTS idx_study_times_id ON study_times (id);').run();
18+
1619
this.sql.pragma('synchronous = 1');
1720
this.sql.pragma('journal_mode = wal');
1821
}
@@ -116,6 +119,54 @@ class Database {
116119
this.sql.prepare('UPDATE emoji_uses SET count = count + ? WHERE emoji_id = ?;').run(count, emojiId);
117120
}
118121
}
122+
123+
/**
124+
*
125+
* @param {string} userId
126+
* @param {number} year
127+
* @param {number} month
128+
* @param {number} day
129+
* @returns {{ id: string, user_id: string, year: number, month: number, day: number, time: number } | undefined}
130+
*/
131+
getStudy(userId, year, month, day) {
132+
return this.sql.prepare('SELECT * FROM study_times WHERE user_id = ? AND year = ? AND month = ? AND day = ?;').get(userId, year, month, day);
133+
}
134+
135+
/**
136+
*
137+
* @param {string} userId
138+
* @param {number} year
139+
* @param {number} month
140+
* @param {number} day
141+
*/
142+
addStudy(userId, year, month, day) {
143+
if (this.getStudy(userId, year, month, day)) return;
144+
this.sql.prepare('INSERT INTO study_times VALUES (?, ?, ?, ?, ?, ?);').run(`${userId}-${year}-${month}-${day}`, userId, year, month, day, 0);
145+
}
146+
147+
/**
148+
*
149+
* @param {string} userId
150+
* @param {number} year
151+
* @param {number} month
152+
* @param {number} day
153+
* @param {number} time
154+
*/
155+
addStudyTime(userId, year, month, day, time) {
156+
if (!this.getStudy(userId, year, month, day)) return;
157+
this.sql.prepare('UPDATE study_times SET time = time + ? WHERE user_id = ? AND year = ? AND month = ? AND day = ?;').run(time, userId, year, month, day);
158+
}
159+
160+
/**
161+
*
162+
* @param {string} userId
163+
* @param {number} year
164+
* @param {number} month
165+
* @returns {Array<{ id: string, user_id: string, year: number, month: number, day: number, time: number }>}
166+
*/
167+
getStudyMonth(userId, year, month) {
168+
return this.sql.prepare('SELECT * FROM study_times WHERE user_id = ? AND year = ? AND month = ? ORDER BY day ASC;').all(userId, year, month);
169+
}
119170
}
120171

121172
module.exports = Database;

src/events/discord/interactionCreate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,10 @@ module.exports = async (client, interaction) => {
195195
if (cmd.info.deferReply) await interaction.deferReply();
196196

197197
cmd.run(client, interaction);
198+
199+
const date = new Date();
200+
if (!client.database.getStudy(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate())) {
201+
client.database.addStudy(interaction.user.id, date.getFullYear(), date.getMonth() + 1, date.getDate());
202+
}
198203
}
199204
};

src/events/discord/messageCreate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,9 @@ module.exports = async (client, message) => {
116116
else client.speakers.get(message.guildId).addSpearkQueue(message.content, message.id, speaker.speaker_id);
117117
}
118118
}
119+
120+
const date = new Date();
121+
if (!client.database.getStudy(message.author.id, date.getFullYear(), date.getMonth() + 1, date.getDate())) {
122+
client.database.addStudy(message.author.id, date.getFullYear(), date.getMonth() + 1, date.getDate());
123+
}
119124
};

src/events/discord/voiceStateUpdate.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,23 @@ module.exports = async (client, oldMember, newMember) => {
4949
await client.channels.cache.get('972852368620261416').send(`${newMember.member.user.tag}さんが${newMember.channel.name}で学習を開始しました!`);
5050
}
5151
else if (newMember.channelId === null) {
52-
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${ms(Date.now() - client.study_times.get(oldMember.member.id))}でした`);
52+
if (!client.study_times.get(oldMember.member.id)) return;
53+
const result = ms(Date.now() - client.study_times.get(oldMember.member.id));
54+
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${result.replace('h', '時間').replace('m', '分').replace('s', '秒')}でした`);
55+
const date = new Date();
56+
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));
5357
client.study_times.delete(oldMember.member.id);
5458
}
5559
else if (newMember.channelId !== oldMember.channelId && oldMember.channel?.parentId !== '972467676951752734') {
5660
client.study_times.set(newMember.member.id, Date.now());
5761
await client.channels.cache.get('972852368620261416').send(`${newMember.member.user.tag}さんが${newMember.channel.name}で学習を開始しました!`);
5862
}
5963
else if (newMember.channelId !== oldMember.channelId && oldMember.channel?.parentId === '972467676951752734') {
60-
await client.channels.cache.get('972852368620261416').send(`${oldMember.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${ms(Date.now() - client.study_times.get(oldMember.member.id))}でした`);
64+
if (!client.study_times.get(oldMember.member.id)) return;
65+
const result = ms(Date.now() - client.study_times.get(oldMember.member.id));
66+
await client.channels.cache.get('972852368620261416').send(`${oldMember.member.user.tag}さんが${oldMember.channel.name}で学習を終えました、今回の学習時間は${result.replace('h', '時間').replace('m', '分').replace('s', '秒')}でした`);
67+
const date = new Date();
68+
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));
6169
client.study_times.delete(oldMember.member.id);
6270
}
6371
}

0 commit comments

Comments
 (0)