当有人进入语音支持等候室时,我试图让我的机器人在特定的文本频道中提及我的服务器员工。
这是我使用的脚本:
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...
答案 0 :(得分:0)
voiceStateUpdate
事件使用两个 VoiceState
调用回调。 (oldState
和 newState
)
您应该为此使用 newState
属性。
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");
}
});
}
}
调用函数时,参数的名称无关紧要,位置决定了分配给这些变量的内容。
例如,在上面的代码中,我可以将变量命名为 foo
和 bar
(而不是 oldState
和 newState
),它仍然可以工作.
答案 1 :(得分:0)
非常感谢你们两个,现在一切正常。
如果脚本对路过的人有用:
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
}
});
}
}