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