簡體   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