-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
206 lines (171 loc) · 6.86 KB
/
Copy pathbot.js
File metadata and controls
206 lines (171 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { Client, GatewayIntentBits, SlashCommandBuilder, REST, Routes } from 'discord.js';
import dotenv from 'dotenv';
import { recordVoiceActivity, recordAbsence, checkAbsence } from './sheets.js';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
// Dayjsのプラグイン設定
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Asia/Tokyo');
dotenv.config();
// Discord Clientの作成
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
],
});
// ユーザーの参加状況を記録するMap
const activeUsers = new Map();
// Botが起動したとき
client.once('ready', () => {
console.log(`✅ Bot起動完了: ${client.user.tag}`);
console.log(`📊 朝活トラッキング開始...`);
console.log(`🎯 監視チャンネルID: ${process.env.VOICE_CHANNEL_ID}`);
// ヘルスチェック: 1時間ごとにBotの状態をログ出力
setInterval(() => {
const now = dayjs().tz('Asia/Tokyo').format('YYYY-MM-DD HH:mm:ss');
console.log(`💚 ヘルスチェック: Botは正常に動作しています (${now})`);
console.log(` 現在のアクティブユーザー数: ${activeUsers.size}`);
}, 60 * 60 * 1000); // 1時間ごと
});
// ボイスチャンネルの状態変化を監視
client.on('voiceStateUpdate', async (oldState, newState) => {
const targetChannelId = process.env.VOICE_CHANNEL_ID;
const userId = newState.member.id;
const username = newState.member.user.username;
const displayName = newState.member.displayName;
// デバッグログ: すべてのvoiceStateUpdateイベントを記録
const now = dayjs().tz('Asia/Tokyo').format('YYYY-MM-DD HH:mm:ss');
console.log(`🔍 [${now}] voiceStateUpdate検知: ${displayName} (${userId})`);
console.log(` oldChannel: ${oldState.channelId || 'なし'}, newChannel: ${newState.channelId || 'なし'}`);
console.log(` targetChannel: ${targetChannelId}`);
// 対象チャンネルに参加した場合
if (newState.channelId === targetChannelId && oldState.channelId !== targetChannelId) {
const joinTime = dayjs().tz('Asia/Tokyo');
activeUsers.set(userId, {
username,
displayName,
joinTime,
});
console.log(`🟢 ${displayName} が参加しました (${joinTime.format('YYYY-MM-DD HH:mm:ss')})`);
}
// 対象チャンネルから退出した場合
if (oldState.channelId === targetChannelId && newState.channelId !== targetChannelId) {
const leaveTime = dayjs().tz('Asia/Tokyo');
const userData = activeUsers.get(userId);
if (userData) {
const { joinTime, displayName } = userData;
const durationMinutes = leaveTime.diff(joinTime, 'minute');
console.log(`🔴 ${displayName} が退出しました (${leaveTime.format('YYYY-MM-DD HH:mm:ss')})`);
console.log(`⏱️ 滞在時間: ${durationMinutes}分`);
// 欠席申請をチェック
let wasAbsent = false;
try {
wasAbsent = await checkAbsence(userId, joinTime.format('YYYY-MM-DD'));
} catch (error) {
console.error(`⚠️ 欠席確認エラー:`, error.message);
}
// Google Sheetsに記録
try {
const result = await recordVoiceActivity({
userId,
username,
displayName,
joinTime: joinTime.format('YYYY-MM-DD HH:mm:ss'),
leaveTime: leaveTime.format('YYYY-MM-DD HH:mm:ss'),
durationMinutes,
date: joinTime.format('YYYY-MM-DD'),
joinHour: joinTime.hour(),
wasAbsent,
});
const { statusInfo } = result;
console.log(`${statusInfo.emoji} ${statusInfo.label} - ${statusInfo.points}ポイント`);
if (wasAbsent) {
console.log(`🎉 奇跡の参加!欠席申請していたのに参加しました!`);
}
console.log(`✅ Sheetsに記録完了`);
} catch (error) {
console.error(`❌ Sheets記録エラー:`, error.message);
console.error(`❌ データ: ${JSON.stringify({ userId, displayName, joinTime: joinTime.format('YYYY-MM-DD HH:mm:ss') })}`);
}
activeUsers.delete(userId);
} else {
console.warn(`⚠️ ${displayName} の参加記録が見つかりません(activeUsersにデータなし)`);
}
}
});
// スラッシュコマンドの処理
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'absence') {
const now = dayjs().tz('Asia/Tokyo');
const today = now.format('YYYY-MM-DD');
const hour = now.hour();
// 4時以降は欠席申請不可
if (hour >= 4) {
await interaction.reply({
content: '❌ 欠席申請は当日の朝4時までです!',
ephemeral: true,
});
return;
}
try {
await recordAbsence({
userId: interaction.user.id,
username: interaction.user.username,
displayName: interaction.member.displayName,
date: today,
requestTime: now.format('YYYY-MM-DD HH:mm:ss'),
});
await interaction.reply({
content: `✅ ${today}の欠席を申請しました。ストリークは維持されます!\n(でも参加したら「奇跡の参加」バッジがもらえるよ👀)`,
ephemeral: true,
});
console.log(`📝 ${interaction.member.displayName} が ${today} の欠席を申請しました`);
} catch (error) {
await interaction.reply({
content: '❌ エラーが発生しました。もう一度お試しください。',
ephemeral: true,
});
console.error('欠席申請エラー:', error);
}
}
});
// エラーハンドリング
client.on('error', (error) => {
console.error('❌ Discord Client エラー:', error);
});
// 再接続処理
client.on('shardDisconnect', (event, shardId) => {
console.warn(`⚠️ Discord切断 (Shard ${shardId}):`, event);
console.log(`🔄 自動再接続を試みます...`);
});
client.on('shardReconnecting', (shardId) => {
console.log(`🔄 Discord再接続中... (Shard ${shardId})`);
});
client.on('shardResume', (shardId, replayedEvents) => {
console.log(`✅ Discord再接続成功 (Shard ${shardId}, イベント再生: ${replayedEvents})`);
});
// Warnings
client.on('warn', (warning) => {
console.warn(`⚠️ Discord警告:`, warning);
});
process.on('unhandledRejection', (error) => {
console.error('❌ 未処理のPromise拒否:', error);
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('🛑 SIGINTを受信しました。Botを終了します...');
client.destroy();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('🛑 SIGTERMを受信しました。Botを終了します...');
client.destroy();
process.exit(0);
});
// Botにログイン
client.login(process.env.DISCORD_TOKEN);