[英]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.