首页 > 解决方案 > Discord.js 尝试在用户加入语音频道时发送消息

问题描述

当有人进入语音支持候诊室时,我试图让我的机器人在特定的文本频道中提及我的服务器工作人员。

这是我使用的脚本:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);
  }
}

client.on('voiceStateUpdate', (newMember) => {
  const newUserChannel = newMember.voice.channelID
  const textChannel = client.channels.cache.get('815316823275995139')

  if(newUserChannel === '815316796067414016') {
    textChannel.send(`worked`)
  }
})

控制台中没有错误,当我加入语音支持频道时,没有任何反应。

这个:

textChannel.send(`worked`)

用于测试目的,我使用的线路是

textChannel.send(`Hey ! Le <@&${753367852299321404}>, ${newMember.user.username} (${newMember.id}) est en Attente de Support !`)

记录我脚本正在运行的脚本的第一部分......正在运行,所以我确定脚本已正确加载,机器人在我的服务器上并且拥有他需要的所有权限。

控制台和脚本屏幕

我的 discord.js 版本是12.5.3

编辑:

是的,现在我可以看到日志,所以我放回我的脚本来检测并发送消息:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);
  client.on('voiceStateUpdate', (newState) => {
    const newUserChannel = newState.voice.channelID
    const textChannel = client.channels.cache.get('815316823275995139')
  
    if(newUserChannel === '815316796067414016') {
      textChannel.send(`working`)
    }
  }); 
  }
}

但我有这个错误:


D:\DiscordBot\Ariabot\src\events\ready\ReadyEvent.js:13
    const newUserChannel = newState.voice.channelID
                                          ^

TypeError: Cannot read property 'channelID' of undefined
    at Client.<anonymous> (D:\DiscordBot\Ariabot\src\events\ready\ReadyEvent.js:13:43)
    at Client.emit (events.js:376:20)
    at VoiceStateUpdate.handle (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (D:\DiscordBot\Ariabot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (D:\DiscordBot\Ariabot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:376:20)
    at Receiver.receiverOnMessage (D:\DiscordBot\Ariabot\node_modules\ws\lib\websocket.js:834:20)
[nodemon] app crashed - waiting for file changes before starting...

标签: discorddiscord.jsbots

解决方案


事件用两个svoiceStateUpdate调用回调。VoiceState(oldStatenewState)

您应该newState为此目的使用该属性。

AVoiceState不包含voice属性,但包含channelID属性。

因此,您的代码应如下所示:

const { DiscordAPIError } = require('discord.js');
const BaseEvent = require('../../utils/structures/BaseEvent');
const Discord = require("discord.js");
const client = new Discord.Client();

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  async run (client) {
  console.log(`Bot prêt, connecté en tant qu'${client.user.username}!`);

  // use 2 parameters
  client.on('voiceStateUpdate', (oldState, newState) => {
    // use the .channelID property (.voice doesn't exist)
    const newUserChannel = newState.channelID;
    const textChannel = client.channels.cache.get('815316823275995139');
  
    if(newUserChannel === '815316796067414016') {
      textChannel.send("working");
    }
  }); 
  }
}

调用函数时,参数的名称无关紧要,位置决定了分配给这些变量的内容。

例如,在上面的代码中,我可以将变量命名为fooand bar(而不是oldStateand newState),它仍然可以工作。


推荐阅读