NodeJS 当一个用户加入并在播放声音后离开时,让Discord机器人加入

wqsoz72f  于 2022-12-12  发布在  Node.js
关注(0)|答案(1)|浏览(128)

我正在尝试让Discord机器人在特定用户加入时加入。机器人在Discord服务器上显示在线,但在我加入时它并没有加入。是否必须格式化用户名,或者我遗漏了什么?
我以为是用户名缺少#标记,但没有产生任何结果。

console.log('On');
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.GuildVoiceStates

  ]

});

client.on('voiceStateUpdate', (oldState, newState) => {

  // Check if the user who joined is the specific user we want
  if (newState.member.id === 'SteveT') {
    // Check if the user joined a voice channel
    if (newState.channel) {
      // Play the specific sound
      newState.channel.join().then(connection => {
        const dispatcher = connection.play('C:\Users\storr\Music\botsounds');
        dispatcher.on('finish', () => {

          // Leave the voice channel after the sound is played
          newState.channel.leave();
        });
      });
    }
  }
});

client.login('insertbotkey');
q9yhzks0

q9yhzks01#

用途:

client.on('voiceStateUpdate', (oldState, newState) => {

  // Check if the user who joined is the specific user we want
  if (newState.member.id === 'USE_YOUR_ID_PLS') {

    // Check if the user joined a voice channel
    if (newState.channel) {

      // Join the voice channel and play the specific sound
      newState.channel.join()
        .then(connection => {

          // Replace 'song.mp3' with the path to the audio file you want to play
          const dispatcher = connection.play('C:\Users\storr\Music\botsounds\song.mp3');

          // Leave the voice channel after the sound is played
          dispatcher.on('finish', () => {
            newState.channel.leave();
          });
        })
        .catch(console.error);
      }
  }
});

离开时做同样的事情;再加一个else就行了。2我懒得再写了。

相关问题