繁体   English   中英

Connection.Destroy() 离开频道语音 discordjs v13

[英]Connection.Destroy() Leave channel voice discordjs v13

这是我的代码“离开语音通道,但正如您所看到的,它不起作用,当我使用命令时,如果它不在语音通道中,它会向我发送消息”我不在语音通道中。

我想让它在不使用 const connection = 的情况下离开语音通道,因为这就是 if connect 和 disconnect 被窃听的原因。

我是新手,我正在试验这个,我了解编程的逻辑,但在这种情况下我不知道该怎么做

if(message.member.voice.channel)
        {
            const connection = joinVoiceChannel({
                channelId: message.member.voice.channel.id,
                guildId: message.guild.id,
                adapterCreator: message.guild.voiceAdapterCreator,
                });
            message.channel.send("Disconected!");
        }
        message.channel.send("i am not in a channel voice");

这是它的工作原理

client.on('messageCreate', async (message) => {

    if (message.content.toLocaleLowerCase() === prefix + 'join') { //Here's how to join a voice channel 

        if (!message.member.voice.channel) return message.channel.send('You need to be a voice channel to execute this command!')
        if(!message.member.voice.channel.joinable) return message.channel.send('I need permission to join your voice channel!')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.member.guild.id,
            adapterCreator: message.channel.guild.voiceAdapterCreator
        })

        console.log('Connected to voice!');
    }

    if (message.content.toLocaleLowerCase() === prefix + 'leave') { //Here's how to leave from voice channel
        const connection = getVoiceConnection(message.guild.id)

        if(!connection) return message.channel.send("I'm not in a voice channel!")

        connection.destroy()

        console.log('Disconnected from voice!');
    }
});

这是完整的代码

const { Client, Intents } = require('discord.js')
const { joinVoiceChannel, getVoiceConnection } = require('@discordjs/voice')

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES /// <= Don't miss this :)
    ]
});

var prefix = '!'

client.on('ready', async () => {
    console.log('Client is Ready...');
});

client.on('messageCreate', async (message) => {

    if (message.content.toLocaleLowerCase() === prefix + 'join') { //Here's how to join a voice channel 

        if (!message.member.voice.channel) return message.channel.send('You need to be a voice channel to execute this command!')
        if(!message.member.voice.channel.joinable) return message.channel.send('I need permission to join your voice channel!')

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.member.guild.id,
            adapterCreator: message.channel.guild.voiceAdapterCreator
        })

        console.log('Connected to voice!');
    }

    if (message.content.toLocaleLowerCase() === prefix + 'leave') { //Here's how to leave from voice channel
        const connection = getVoiceConnection(message.guild.id)

        if(!connection) return message.channel.send("I'm not in a voice channel!")

        connection.destroy()

        console.log('Disconnected from voice!');
    }
});

client.login('BOT TOKEN HERE!');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM