首页 > 解决方案 > 试图制作一个机器人来计算语音频道中的成员

问题描述

如果认为我问的是愚蠢的,请不要发表评论。我对此完全陌生。我尝试搜索解决方案,但找不到有用的解决方案或 discord.js 中的菜鸟可以理解的解决方案。提前谢谢你的帮助!

const express = require ("express")
var app = require('express')();

app.get("/", (req, res) => {
  res.send("hello hell!")
})

app.listen(3000, () => {
  console.log("Project is ready")
})

let Discord = require("discord.js")
let client = new Discord.Client()

client.on('voiceStateUpdate', (oldVoiceState, newVoiceState) => {

let newUserChannel = newVoiceState.voiceChannel

let oldUserChannel = oldVoiceState.voiceChannel

if(oldUserChannel === undefined && newUserChannel !== undefined) {

// User Joins a voice channel
console.log(client.voiceConnections.size)

} else if(newUserChannel === undefined){

// User leaves a voice channel

console.log(client.voiceConnections.size)

}

})

client.login("token")

repl.it 某人加入时的结果 Project is ready

console.log(client.voiceConnections.size)
                                    ^

TypeError: Cannot read property 'size' of undefined
    at Client.<anonymous> (/home/runner/VCC-Bot/index.js:30:37)
    at Client.emit (events.js:315:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at VoiceStateUpdate.handle (/home/runner/VCC-Bot/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (/home/runner/VCC-Bot/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (/home/runner/VCC-Bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/VCC-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/VCC-Bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/VCC-Bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)

标签: discorddiscord.js

解决方案


不知道我在这里是否为时已晚,但你需要给机器人它的意图。您可以像这样添加意图:

const { Client, Intents } = require("discord.js");

const client = new Client({
    intents: [Intents.FLAGS.GUILD_VOICE_STATES],
});

推荐阅读