夜更かし防止
Discordで友達と通話していてついつい夜更かししてしまうのを防止するために、時間になるとボイスチャンネルから強制的に退室させるようにした。
作成したBotのコードは以下の通り
require('dotenv').config();const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates ]});
client.once(Events.ClientReady, async () => {
const targetUserId = process.env.USER_ID;
for (const guild of client.guilds.cache.values()) { try { const member = await guild.members.fetch(targetUserId).catch(() => null); if (member?.voice?.channelId) { await member.voice.disconnect(); console.log(`Disconnected ${member.user.tag} from voice channel in ${guild.name}`); } } catch (err) { console.error(`Error in guild ${guild.name}:`, err.message); } }
client.destroy();});
client.login(process.env.TOKEN);環境変数にはBOTのトークンとアプリID、退室させたいユーザーのIDが登録されている。
最後にこのコードをWindowsのタスクスケジューラに登録しておくことで定期実行できるようにした。
次は自分で環境構築できない人でも使えるようにexe化して配布したい。