Discord JS – 当用户加入时跟随他们进入语音频道

huangapple go评论58阅读模式
英文:

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);
    }
});

huangapple
  • 本文由 发表于 2023年4月20日 05:22:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定