【发布时间】: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