Discord.js当所有人断开连接后,如何删除临时语音通道?

时间:2019-08-24 23:42:46

标签: javascript discord discord.js

如果有人连接特定的通道bot将创建一个与他的名字相同的通道,然后他将其移入其中,我就编写了代码,我想当此用户断开连接并且没有人连接到此通道bot自动删除通道时。而且我编写了这段代码,但是我不知道如何删除频道。

bot.on('voiceStateUpdate', (oldMember, newMember) =>{
    let mainCatagory = '604259561536225298';
    let mainChannel = '614954752693764119';
    if(newMember.voiceChannelID === mainChannel){
        newMember.guild.createChannel(`${newMember.user.username}'s Channel`,'voice')
        .then(temporary => {
            temporary.setParent(mainCatagory)
            .then(() => newMember.setVoiceChannel(temporary.id))
        }).catch(err =>{
            console.error(err);
        })
    }
});

我尝试做if(newMember.voiceChannel.members.size === 0){temporary.detele};,但未定义temporary

还有oldMember

3 个答案:

答案 0 :(得分:1)

在事件正文之前创建一个数组,以写入临时通道的ID以及创建此通道的服务器。

 var temporary = []

 bot.on('voiceStateUpdate', (oldMember, newMember) =>{
     const mainCatagory = '604259561536225298';
     const mainChannel = '614954752693764119';

     if(newMember.voiceChannelID == mainChannel){
        // Create channel...
         await newMember.guild.createChannel(`${newMember.user.username}'s channel`, {type: 'voice', parent: mainCatagory})
             .then(async channel => {
                 temporary.push({ newID: channel.id, guild: channel.guild })
                 // A new element has been added to temporary array!
                 await newMember.setVoiceChannel(channel.id)
             })
     }

     if(temporary.length >= 0) for(let i = 0; i < temporary.length; i++) {
         // Finding...
         let ch = temporary[i].guild.channels.find(x => x.id == temporary[i].newID)
         // Channel Found!         
         if(ch.members.size <= 0){

             await ch.delete()
             // Channel has been deleted!
             return temporary.splice(i, 1)
         }
     }
 })

答案 1 :(得分:0)

您可以尝试先定义一个空变量,例如temp,然后在返回createChannel()承诺时为其分配临时通道,例如:

 bot.on('voiceStateUpdate', (oldMember, newMember) =>{
        let mainCatagory = '604259561536225298';
        let mainChannel = '614954752693764119';
        let temp;
        if(newMember.voiceChannelID === mainChannel){
            newMember.guild.createChannel(`${newMember.user.username}'s Channel`,'voice')
            .then(temporary => {
                temp = temporary
                temporary.setParent(mainCatagory)
                .then(() => newMember.setVoiceChannel(temporary.id))
            }).catch(err =>{
                console.error(err);
            })
        }
        if(newMember.voiceChannel.members.size === 0){temp.delete()};
    });

答案 2 :(得分:0)

这是基于 Raifyks' answer,但针对 Discord.js v12 进行了更新,并有一些改进。

const mainCategory = '604259561536225298';
const mainChannel = '614954752693764119';

// A set that will contain the IDs of the temporary channels created.
/** @type {Set<import('discord.js').Snowflake>} */
const temporaryChannels = new Set();

bot.on('voiceStateUpdate', async (oldVoiceState, newVoiceState) => {
    try {
        const {channelID: oldChannelId, channel: oldChannel} = oldVoiceState;
        const {channelID: newChannelId, guild, member} = newVoiceState;

        // Create the temporary channel
        if (newChannelId === mainChannel) {
            // Create the temporary voice channel.
            // Note that you can set the parent of the channel in the
            // createChannel call, without having to set the parent in a
            // separate request to Discord's API.
            const channel = await guild.channels.create(
                `${member.user.username}'s channel`,
                {type: 'voice', parent: mainCategory}
            );
            // Add the channel id to the array of temporary channel ids.
            temporaryChannels.add(channel.id);
            // Move the member to the new channel.
            await newVoiceState.setChannel(channel);
        }

        // Remove empty temporary channels
        if (
            // Did the user come from a temporary channel?
            temporaryChannels.has(oldChannelId) &&
            // Did the user change channels or leave the temporary channel?
            oldChannelId !== newChannelId
        ) {
            // Delete the channel
            await oldChannel.delete();
            // Remove the channel id from the temporary channels set
            temporaryChannels.delete(oldChannelId);
        }
    } catch (error) {
        // Handle any errors
        console.error(error);
    }
});