discord - 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...
解决方案
事件用两个svoiceStateUpdate
调用回调。VoiceState
(oldState
和newState
)
您应该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");
}
});
}
}
调用函数时,参数的名称无关紧要,位置决定了分配给这些变量的内容。
例如,在上面的代码中,我可以将变量命名为foo
and bar
(而不是oldState
and newState
),它仍然可以工作。
推荐阅读
- c# - C#和PHP加密的字符串结果不同,字节相同
- node.js - 使用 node.js 将 XML 3GPP 转换为 csv
- android - 致命异常:com.facebook.react.common.JavascriptException:TypeError:无法将空值转换为对象
- javascript - getHours(),显示和隐藏
- android-emulator - 无法使用 fiddler 从 Android 7 版本的移动应用程序捕获流量
- angular8 - 使用功能模块 Angular 8 时,API 被调用了两次
- linux - 我的 bashrc 包含奇怪的字符(如果 Ä -f ü/.bash_aliases Å; 那么 . ü/.bash_aliases fi)
- jmeter - 响应断言中的附加“/”(斜杠)显示在 Jmeter 中
- lua - 如何匹配 Lua 中的可选模式?
- c - 我正在尝试用空格打印每一行字符串,直到换行。当我尝试在开始时采用 2 个 int 输入时,它说分段错误