JavaScript中的voiceStateUpdate问题

时间:2018-09-13 08:26:17

标签: javascript bots discord

我正在尝试使这个不和谐的机器人工作。这个想法很简单,让机器人在有人加入语音聊天时发送通知。 我在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函数未定义

我从这里得到了几乎所有东西:

我尝试过的一切:

  • 添加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 =不和谐服务器上的常规聊天ID

  • 353996293154144260 =第一次语音聊天的ID

  • 489674797261783041 =第二次语音聊天的ID

更新

好的,我已经解决了部分问题,我需要使用message.channel.send('has joined a voice channel');而不是channel.send('has joined a voice channel');,但是我无法使其按需运行。


特别感谢YakovL。他帮助我使这篇文章更加完整和完善。

3 个答案:

答案 0 :(得分:1)

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();

答案 1 :(得分:0)

替换

var channel = client.channelID.get('353996293154144259');

使用

var channel = client.channels.get('353996293154144259');

正如我多次在不和谐的公会中告诉你的那样。

答案 2 :(得分:-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");