見出し画像

【自作discord bot】VCに入ったときに通知する機能をつくる

こんな感じのログを出したい
雑に解説します


用意するもの

  • Discord

  • Glitch

  • discord.js v14

  • やる気

botそのもののつくり方はこちらを

いきなりソースをドン

const {
  Client,
  GatewayIntentBits,
} = require("discord.js");
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildVoiceStates,
  ],
});

const GUILD = "xxxxxxxxxxxxxxxxx";          // サーバーID
const OUTPUT_CHANNEL= "xxxxxxxxxxxxxxxxx";  // チャンネルID

client.on("ready", () => {
  console.log("準備完了!");
});

// ここから
client.on("voiceStateUpdate", (oldState, newState) => {
  if (oldState.guild.id !== GUILD) return;
  const channel = oldState.member.guild.channels.cache.get(
    OUTPUT_CHANNEL
  );

  if (oldState.channelId === null && newState.channelId !== null) {
    return channel.send(
      `**参加** ${oldState.member.user.tag}さんが入室しました。`
    );
  } else if (oldState.channelId !== null && newState.channelId === null) {
    return channel.send(
      `**退出** ${newState.member.user.tag}さんが退出しました。`
    );
  }
});
// ここまで

client.login(process.env.DISCORD_BOT_TOKEN);

雑に解説

ボイスチャンネルを認識してもらう

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildVoiceStates,        // ←これ
  ],
});

GuildVoiceStatesを設定しないとVCへの入退室を認知してくれません

ボイスチャンネルに入ったらメッセージを送信する処理

client.on("voiceStateUpdate", (oldState, newState) => {
  if (oldState.guild.id !== GUILD) return;
  const channel = oldState.member.guild.channels.cache.get(
    OUTPUT_CHANNEL
  );

  if (oldState.channelId === null && newState.channelId !== null) {
    return channel.send(
      `**参加** ${oldState.member.user.tag}さんが入室しました。`
    );
  } else if (oldState.channelId !== null && newState.channelId === null) {
    return channel.send(
      `**退出** ${newState.member.user.tag}さんが退出しました。`
    );
  }
});

対象のサーバーじゃない場合は実行しない

if (oldState.guild.id !== GUILD) return;

const GUILD = "xxxxxxxxxxxxxxxxx";でサーバーのIDを設定します
botが1つのサーバーでしか稼働していない場合は要らないかも

ログを送信するチャンネルを指定する

const channel = oldState.member.guild.channels.cache.get(
  OUTPUT_CHANNEL
);

const OUTPUT_CHANNEL= "xxxxxxxxxxxxxxxxx";でチャンネルIDを指定します
外部(スプシとか)に出力する場合は要らない&外部用の処理を書く必要があります

入退室時にメッセージを送信する

if (oldState.channelId === null && newState.channelId !== null) {
  return channel.send(
    `**参加** ${oldState.member.user.tag}さんが入室しました。`
  );
} else if (oldState.channelId !== null && newState.channelId === null) {
  return channel.send(
    `**退出** ${newState.member.user.tag}さんが退出しました。`
  );
}

send()の中には好きにメッセージを書くことができるので

if (oldState.channelId === null && newState.channelId !== null) {
    let message = '@everyone \n' 
      + newState.channel.name + 'に'
      + oldState.member.user.tag + 'さんが入室しました';
    return channel.send(message);
  }

みたいな記述にすると
メンションチャンネル名もつけることができる

おわり

ざっくりこんな感じです
雑談サーバーなどでVCに入ったら呼びかける機能としてはアリじゃないでしょうか
因みに、この書き方のままだとVCからVCへ移動したときは入室ログ出ません、出ないということだけお伝えしておきます

おしまい


よろしければサポートお願いします! いただいたサポートはクリエイターとしての活動費に使わせていただきます!