英文:
Discord JS - Follow a user into a voice channel when they join
问题
I'm trying to make my bot detect when a specific user joins a voice channel (any channel) and then follow them into that channel. I've attempted to read the documentation but it seems to be absolutely useless. And basically every other resource is out of date. I'd show you my code but there is pretty much nothing so far. So far, my bot can simply come online in the server. Please could anybody help? Thanks.
So far I have tried to use this code to simply detect anyone joining any voice channel:
client.on('voiceStateUpdate', (oldState, newState) => {
if(newState.channelID === null) //left
console.log('user left channel', oldState.channelID);
else if(oldState.channelID === null) // joined
console.log('user joined channel', newState.channelID);
else // moved
console.log('user moved channels', oldState.channelID, newState.channelID);
});
This, among many other solutions I tried, has failed to work. I have attempted various different methods to get a guild member based on what I've found in the documentation and in tutorials - but these have also failed.
英文:
I'm trying to make my bot detect when a specific user joins a voice channel (any channel) and then follow them into that channel. I've attempted to read the documentation but it seems to be absolutely useless. And basically every other resource is out of date. I'd show you my code but there is pretty much nothing so far. So far, my bot can simply come online in the server. Please could anybody help? Thanks.
So far I have tried to use this code to simply detect anyone joining any voice channel:
client.on('voiceStateUpdate', (oldState, newState) => {
if(newState.channelID === null) //left
console.log('user left channel', oldState.channelID);
else if(oldState.channelID === null) // joined
console.log('user joined channel', newState.channelID);
else // moved
console.log('user moved channels', oldState.channelID, newState.channelID);
});
This, among many other solutions I tried, has failed to work. I have attempted various different methods to get a guild member based on what I've found in the documentation and in tutorials - but these have also failed.
答案1
得分: 0
请确保你有正确的意图。您可能还希望在Discord开发者门户中设置正确的权限,以便播放音频等。以下是可行的代码:
import { GatewayIntentBits, Client, Events } from 'discord.js';
const client= new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates
],
});
client.on(Events.VoiceStateUpdate, (oldState, newState) => {
if(newState.channelID === null) {
console.log('用户离开频道', oldState.channelID);
} else if(oldState.channelID === null) {
console.log('用户加入频道', newState.channelID);
} else {
console.log('用户移动频道', oldState.channelID, newState.channelID);
}
});
英文:
Make sure you have the correct intents. You probably also want to set the correct permissions in the Discord Developer Portal for playing audio and such too.
This code should work:
import { GatewayIntentBits, Client, Events } from 'discord.js';
const client= new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentsBits.GuildVoiceStates
],
});
client.on(Events.VoiceStateUpdate, (oldState, newState) => {
if(newState.channelID === null) {
console.log('user left channel', oldState.channelID);
} else if(oldState.channelID === null) {
console.log('user joined channel', newState.channelID);
} else {
console.log('user moved channels', oldState.channelID, newState.channelID);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论