JavaScript 中的 voiceStateUpdate 问题
Posted
技术标签:
【中文标题】JavaScript 中的 voiceStateUpdate 问题【英文标题】:problem with voiceStateUpdate in JavaScript 【发布时间】:2019-02-17 22:40:51 【问题描述】:我正在尝试让这个不和谐机器人工作。这个想法很简单,让机器人在有人加入语音聊天时发送通知。 我在 Atom 上做这个,我得到的错误是这个:
TypeError: Cannot read property 'send' of undefined
at Client.client.on (C:\Users\Franco\Desktop\bot\bot.js:15:20)
at emitTwo (events.js:126:13)
at Client.emit (events.js:214:7)
at VoiceStateUpdateHandler.handle (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\handlers\VoiceStateUpdate.js:39:16)
at WebSocketPacketManager.handle (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (C:\Users\Franco\Desktop\bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (C:\Users\Franco\Desktop\bot\node_modules\ws\lib\event-target.js:120:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
代码尝试:
client.on("message", function(message)
client.on('voiceStateUpdate', (oldMember, newMember) =>
console.log('lol');
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
var channel = client.channels.get('353996293154144259');
if(oldUserChannel === 353996293154144260 && newUserChannel !== 489674797261783041)
channel.send('has joined a voice channel');
// User Joins a voice channel
else if(newUserChannel === 489674797261783041)
channel.send('has left a voice channel');
// User leaves a voice channel
)
)
console.log 仅用于测试voiceStateUpdate
是否正常工作。
当我尝试运行它时,我得到了这个错误:
“TypeError: 无法读取未定义的属性‘发送’”
据我了解,错误是因为 .send
函数未定义
我几乎从这里得到了所有东西:
https://www.reddit.com/r/discordapp/comments/6p85uf/discordjs_any_way_to_detect_when_someone_enter_a/ https://discord.js.org/#/docs/main/stable/general/welcome我尝试过的一切:
添加client.on("message", function(message)
将.send
编辑为.message
和.sendMessage
尝试定义.send
将 client.channels.get 更改为 lient.channelid.get
更改/删除var channel = client.channels.get('353996293154144259');
将client.on('voiceStateUpdate', (oldMember, newMember) =>
移出client.on("message", function(message)
当我这样做时,我又遇到了另一个错误
“当我将 client.on 移出时出现第二个错误:”(节点:18196)MaxListenersExceededWarning:检测到可能的 EventEmitter 内存泄漏。添加了 11 个消息侦听器。使用emitter.setMaxListeners() 增加限制"
也许是一些无用的信息:
353996293154144259 = Discord 服务器上一般聊天的 ID
353996293154144260 = 第一次语音聊天的 id
489674797261783041 = 第二次语音聊天的 id
更新
好的,我解决了部分问题,我需要使用 message.channel.send('has joined a voice channel');
而不是 channel.send('has joined a voice channel');
但我无法让它按我的意愿工作。
特别感谢 YakovL。他帮助我使这篇文章更完整、更好。
【问题讨论】:
据我所知,client.channels.get('353996293154144259');返回身份不明。 如果我尝试不使用该行,我仍然会收到相同的错误,即使我更换它 频道中可能没有 353996293154144259 是的,它现在,353996293154144259 它是我的不和谐测试服务器的一般聊天 ID 给我一个味道,添加一行client.channels.forEach((i,j)=>console.log(j))
,看看是否真的有你提供的id的对象。
【参考方案1】:
我是巴西人,我的语言是巴西葡萄牙语!
const Discord = require('discord.js'),
client = new Discord.Client(),
colors = require('colors/safe');
client.on('voiceStateUpdate', (oldMember, newMember) =>
let newUserChannel = newMember.channelID;
let oldUserChannel = oldMember.channelID;
let userid = newMember.id;
// ----------- verificação --------------
let selfmute = newMember.mute;
let mute = newMember.selfMute;
let selfmute1 = oldMember.mute;
let mute1 = oldMember.selfMute;
let selfdeaf = newMember.selfDeaf;
let selfdeaf1 = oldMember.selfDeaf;
let selfvid = newMember.selfVideo;
let selfvid1 = oldMember.selfVideo;
let deaf = newMember.deaf;
let deaf1 = oldMember.deaf;
let servmute = newMember.serverMute;
let servmute1 = oldMember.serverMute;
if ( selfmute == true ) return;
if ( mute == true ) return;
if ( selfmute1 == true ) return;
if ( mute1 == true ) return;
if ( selfdeaf == true ) return;
if ( selfdeaf1 == true ) return;
if ( selfvid == true ) return;
if ( selfvid1 == true ) return;
if ( deaf == true ) return;
if ( deaf1 == true ) return;
if ( servmute == true ) return;
if ( servmute1 == true ) return;
// -------- definindo cores ----------
var a = colors.red('[-] ');
var b = colors.green('[+] ');
var c = colors.blue('[!] ');
// -------- menssagem / log ----------
if ( newUserChannel === "827668874542579797") //id voice channel
console.log(c+"- id: "+userid)
console.log(b+"- Entrou em: "+newUserChannel);
else if ( oldUserChannel === "827668874542579797") //id voice channel
console.log(c+"- id: "+userid)
console.log(a+"- Saiu de: "+oldUserChannel);
else
console.log(c+"- Ratos estão dialogando...");
)
//----------------Comando pra ficar on-------------
client.login("TOKEN");
【讨论】:
【参考方案2】:替换
var channel = client.channelID.get('353996293154144259');
与
var channel = client.channels.get('353996293154144259');
编辑: 从 v12 开始,它是
var channel = client.channels.cache.get('353996293154144259');
【讨论】:
对我来说,这是行不通的,如果我把 var channel = client.channels.get('353996293154144259');或 var channel = client.channelID.get('353996293154144259');我无法让机器人将正确的消息发送到 353996293154144259 感谢您的帮助【参考方案3】:var channel = client.channels.get('353996293154144259');
这对我不起作用,所以我改用它:
const channel = client.channels.cache.filter((channel) => channel.name === 'name').first();
最好使用这个但搜索ID:
const channel = client.channels.cache.filter((channel) => channel.id === '353996293154144259').first();
【讨论】:
以上是关于JavaScript 中的 voiceStateUpdate 问题的主要内容,如果未能解决你的问题,请参考以下文章