简体   繁体   English

我如何为我尝试创建的 Discord 机器人修复此错误?

[英]How can I fix this error for my Discord bot I'm trying to make?

I'm trying to create a Discord bot in JavaScript that plays a specified sound for each specific person that joins the voice channel.我正在尝试在 JavaScript 中创建一个 Discord 机器人,它为加入语音频道的每个特定人播放指定的声音。 It is kind of like an intro sound/song thing.这有点像介绍音效/歌曲。

I'm getting this error when I tried to test it out.当我尝试对其进行测试时出现此错误。 It outputs this in the terminal when I join the voice channel and the bot doesn't join the voice channel to play the sound.当我加入语音频道并且机器人没有加入语音频道播放声音时,它会在终端中输出。

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

Output: Output:

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)

This is my code below.这是我下面的代码。 Also, how would I go about doing this so that each user has their own different sound?另外,我 go 如何做到这一点,以便每个用户都有自己不同的声音?

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');

There seems to be an issue with the path to the audio file in your code.您的代码中音频文件的路径似乎有问题。 The path you have provided is not a valid path, as it contains a backslash () instead of a forward slash (/) between directories.您提供的路径不是有效路径,因为它在目录之间包含反斜杠 () 而不是正斜杠 (/)。 In JavaScript, backslashes in strings need to be escaped with another backslash, so the correct path should be something like this: JavaScript中,字符串中的反斜杠需要用另一个反斜杠转义,所以正确的路径应该是这样的:

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

Alternatively, you can use forward slash instead of a backslash to separate directories in the path like this:或者,您可以使用正斜杠而不是反斜杠来分隔路径中的目录,如下所示:

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

And to allow each user to have their own sound, you could create a mapping of user IDs to audio file paths, and then use this mapping to look up the correct audio file for each user:为了让每个用户都有自己的声音,您可以创建用户 ID 到音频文件路径的映射,然后使用此映射为每个用户查找正确的音频文件:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Discord 机器人的踢腿命令? - How can I fix my kick command for my Discord bot? 当我启动我的机器人 discord 时出现错误,我找不到解决方法 - when i launch my bot discord i have an error and i can't find how to fix it 我的 discord 机器人出现错误,请告知如何修复 - i an getting an error in my discord bot, please tell how fix 如何解决我的 discord 机器人遇到的这个错误 - How to fix this error I am getting with my discord bot 如何在我的 Discord 机器人中修复这些类型错误? - How can I fix these TypeErrors in my Discord bot? 我正在尝试为我的 discord 机器人发出轮询命令,但它一直给我错误:“TypeError: Cannot read property 'push' of undefined” - I'm trying to make a poll command for my discord bot, but it keeps giving me the error: “TypeError: Cannot read property 'push' of undefined” 嗨,我正在尝试让我的 discord 机器人记录使用的命令 - Hi I'm trying to make my discord bot log the command used 我正在尝试为我的机器人在 discord.js 中创建一个自动角色 function 但它不起作用 - I'm trying to make an autorole function in discord.js for my bot but it doesn't work 我正在尝试让我的 discord 机器人使用 prism-media 录制音频,如@discordjs/voice 示例中所述,但我遇到了错误 - i'm trying to make my discord bot to record audio using prism-media as said in the example of @discordjs/voice but i'm getting error 如何让我的 Discord 机器人给用户一个角色? - How can I make my Discord bot give a user a role?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM