【问题标题】:How can I fix this error for my Discord bot I'm trying to make?我如何为我尝试制作的 Discord 机器人修复此错误?
【发布时间】:2022-12-18 08:27:22
【问题描述】:

我正在尝试用 JavaScript 创建一个 Discord 机器人,它为加入语音频道的每个特定人播放指定的声音。这有点像介绍音效/歌曲。

当我尝试对其进行测试时出现此错误。当我加入语音频道并且机器人没有加入语音频道播放声音时,它会在终端中输出。

cd "C:\javacode\Discord Audio Bot"
node Audio.js

输出:

On
C:\javacode\Discord Audio Bot\Audio.js:29
  } else if (newState.channel.members.size = 1) {
                              ^

TypeError: Cannot read properties of null (reading 'members')
    at Client.<anonymous> (C:\javacode\Discord Audio Bot\Audio.js:29:31)
    at Client.emit (node:events:513:28)
    at VoiceStateUpdate.handle (C:\javacode\Discord Audio Bot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:38:14)
    at module.exports [as VOICE_STATE_UPDATE] (C:\javacode\Discord Audio Bot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (C:\javacode\Discord Audio Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (C:\javacode\Discord Audio Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:489:22)
    at WebSocketShard.onMessage (C:\javacode\Discord Audio Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10)
    at callListener (C:\javacode\Discord Audio Bot\node_modules\ws\lib\event-target.js:290:14)
    at WebSocket.onMessage (C:\javacode\Discord Audio Bot\node_modules\ws\lib\event-target.js:209:9)
    at WebSocket.emit (node:events:513:28)

这是我下面的代码。另外,我将如何去做才能让每个用户都有自己不同的声音?

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

});

client.on('voiceStateUpdate', (_oldState, newState) => {
  // Check if the user who joined is the specific user we want
  if (newState.member.id === 'STtheGamer') {
      // 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\swtheme.mp3');

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

});

client.login('botkey here');

【问题讨论】:

  • 你为什么要检查频道的成员数量前检查频道是否存在?你可能想扭转这种情况

标签: javascript node.js discord discord.js bots


【解决方案1】:

您的代码中音频文件的路径似乎有问题。您提供的路径不是有效路径,因为它在目录之间包含反斜杠 () 而不是正斜杠 (/)。在 JavaScript 中,字符串中的反斜杠需要用另一个反斜杠进行转义,所以正确的路径应该是这样的:

const dispatcher = connection.play('C:\Users\storr\Music\botsounds\swtheme.mp3');

或者,您可以使用正斜杠而不是反斜杠来分隔路径中的目录,如下所示:

const dispatcher = connection.play('C:/Users/storr/Music/botsounds/swtheme.mp3');

为了让每个用户都有自己的声音,您可以创建用户 ID 到音频文件路径的映射,然后使用此映射为每个用户查找正确的音频文件:

const introSoundUser = {
   'user1ID' : 'audiofile.path',
   'user2ID' : 'audiofile.path'
};

【讨论】:

  • 您是使用 ChatGPT 创建此答案的吗?
  • @PresidentJamesK.Polk 嗨,我没有。我不喜欢它。我以前遇到过类似的问题,所以我想我会尝试回答:)
  • 没问题,内容不是问题,它的语法和拼写非常好,我对此表示怀疑。
  • @PresidentJamesK.Polk 我明白了。我喜欢通过语法检查器来运行我的消息,因为英语不是我的母语。
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 2017-07-22
  • 2020-12-23
  • 2021-03-06
相关资源
最近更新 更多