Discord.js 尝试在用户加入语音频道时发送消息

Discord.js Trying to send a message if a user joins a voice channel

我正在尝试让我的机器人在有人进入语音支持等候室时在特定的文本频道中提及我的服务员。

这是我使用的脚本:

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 !`)

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

Console and script screen

我的 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...

voiceStateUpdate event calls the callback with two VoiceStates。 (oldStatenewState)

为此,您应该使用 newState 属性。

A VoiceState 不包含 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");
    }
  }); 
  }
}

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

例如,在上面的代码中,我可以将变量命名为 foobar(而不是 oldStatenewState),它会仍然有效。

非常感谢你们两个,现在工作正常。

如果脚本对路过的人有用:

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 ready, connected as ${client.user.username}!`);

  client.on('voiceStateUpdate', (oldState, newState) => {
    const newUserChannel = newState.channelID;
    const textChannel = client.channels.cache.get('852643030477307995');
// 852643030477307995 = ID of the text channel, I use, where the message will be posted
  
    if(newUserChannel === '759339336663040020') {
// 759339336663040020 = ID of the voice channel I want to lookup
      textChannel.send(`Hey <@&752168551103856700> ! ${newState.member} is waiting for help !`);
// <@&752168551103856700> = ID of the role I want to tag
    }
  }); 
  }
}