本文介绍了当某人加入特定语音频道时,向文本频道发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作我的机器人,当有人进入语音支持候机室时,它会通过特定的文本通道通知我的服务器员工。
遗憾的是,我只找到了一些不一致版本12的示例,在版本13中它不适用于我。我不知道要在该代码中更改什么。
没有错误消息等,只是在有人加入语音通道时不会发送消息。
client.on('voiceStateUpdate', (oldState, newState) => {
const newUserChannel = newState.channelID;
const textChannel = client.channels.cache.get('931223643362703521');
if (newUserChannel === '931156705303317013') {
textChannel.send('Somebody joined');
}
console.error();
});
推荐答案
首先,确保添加了GUILD_VOICE_STATES
意图。
使用fetch
通道可能比依赖缓存更好。如果该频道已缓存,fetch
将使用该频道,不会向Discorde接口发出请求,但如果该频道未缓存,channels.cache.get
将找不到它。
我做了几处更改,并在下面的代码中添加了一些注释:
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_VOICE_STATES,
],
});
client.on('voiceStateUpdate', async (oldState, newState) => {
const VOICE_SUPPORT_ID = '931156705303317013';
const LOG_CHANNEL_ID = '931223643362703521';
// if there is no newState channel, the user has just left a channel
const USER_LEFT = !newState.channel;
// if there is no oldState channel, the user has just joined a channel
const USER_JOINED = !oldState.channel;
// if there are oldState and newState channels, but the IDs are different,
// user has just switched voice channels
const USER_SWITCHED = newState.channel?.id !== oldState.channel?.id;
// if a user has just left a channel, stop executing the code
if (USER_LEFT)
return;
if (
// if a user has just joined or switched to a voice channel
(USER_JOINED || USER_SWITCHED) &&
// and the new voice channel is the same as the support channel
newState.channel.id === VOICE_SUPPORT_ID
) {
try {
let logChannel = await client.channels.fetch(LOG_CHANNEL_ID);
logChannel.send(
`${newState.member.displayName} joined the support channel`,
);
} catch (err) {
console.error('❌ Error finding the log channel, check the error below');
console.error(err);
return;
}
}
});
这篇关于当某人加入特定语音频道时,向文本频道发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!