【问题标题】:trying to make a bot that counts members in a voice channel试图制作一个机器人来计算语音频道中的成员
【发布时间】:2021-04-29 19:02:16
【问题描述】:

如果认为我问的是愚蠢的,请不要发表评论。 我对此完全陌生。我尝试搜索解决方案,但找不到有用的解决方案或 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 某人加入时的结果 项目准备就绪

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)

【问题讨论】:

  • 错误告诉您voiceConnections 未定义。换句话说,那里没有可以请求size 属性的对象。如果您有一个可能未定义的变量,那么您必须在使用它之前对其进行测试。
  • 我该如何定义它?
  • 这个问题显示了在使用 API 时东西从何而来的根本困惑。我会先阅读documentation。不知道您使用的是什么版本,但快速搜索文档表明 voiceConnections 已弃用。也许这就是这里的问题。
  • 版本是 12.5.1 我查看了文档,新命令应该是 guild.voice.connection;并且该语音连接现在是语音状态。虽然找不到任何关于 voiceconnections.size 的信息,所以我相信这从基础上讲是对的吗?。
  • 从更改列表中:“client.voiceConnections 已被删除,现在位于 ClientVoiceManager 类中。此外,Collection 不再是 getter。” i> 接下来是使用client.voice.connections 而不是client.voiceConnections 的建议——(link)

标签: discord discord.js


【解决方案1】:

不确定我是否来得太晚了,但你需要让机器人知道它的意图。 您可以像这样添加意图:

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

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

【讨论】:

【解决方案2】:

改变

let newUserChannel = newVoiceState.voiceChannel

到

let newUserChannel = newVoiceState.channel

然后改变

let oldUserChannel = oldVoiceState.voiceChannel

到

let oldUserChannel = oldVoiceState.channel

client.voiceConnections 不存在所以,如果你想让它工作,试试oldUserChannel.users.size ;)

【讨论】:

  • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
猜你喜欢
  • 2020-04-17
  • 2021-07-13
  • 2020-01-11
  • 2020-09-26
  • 2018-06-16
  • 2023-02-25
  • 2021-05-23
  • 2020-07-16
  • 2021-04-24
相关资源
最近更新 更多