【问题标题】:How can I delete a temporary voice channel when everyone disconnects?当所有人都断开连接时,如何删除临时语音通道?
【发布时间】:2019-12-29 17:18:08
【问题描述】:

我编写了这样的代码,如果有人连接到特定频道,机器人会用他们的名字创建一个频道,然后将他们移动到其中。我希望机器人在该用户断开连接并且没有其他人连接到该频道时自动删除该频道。我有这个代码,但我不知道如何删除频道。

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 没有定义。

【问题讨论】:

  • 对不起我的英语。

标签: javascript discord discord.js


【解决方案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)
         }
     }
 })

【讨论】:

  • 但他不删除频道只创建它。
  • @BrokenSoul 尝试将temporary.push({ newID: channel.id, guild: channel.guild }) 替换为temporary.push({ newID: channel.id, guild: newMember.guild.id }) 并将let ch = temporary[i].guild.channels.find(x =&gt; x.id == temporary[i].newID) 替换为let ch = bot.guilds.find(x =&gt; x.id === temporary[i].guild).channels.find(x =&gt; x.id === temporary[i].newID)
【解决方案2】:

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

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 (
            // Is the channel empty? (thanks to Rakshith B S for pointing this out)
            !oldChannel.members.size &&
            // 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);
    }
});

【讨论】:

    【解决方案3】:

    您可以尝试先定义一个空变量,例如 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()};
        });
    

    【讨论】:

    • 错误:TypeError: Cannot read property 'members' of undefined
    • 试试if(oldMember.voiceChannel.members.size === 0){temp.delete()}?因为不是新成员。不过要小心,您可能应该添加频道是临时频道的条件,否则它会删除我认为的任何空频道
    • 我试过oldMember。错误:temp.delete() => delete 未定义,因为温度为 undefined
    • 摆脱temp 并尝试if(oldMember.voiceChannel.members.size === 0){oldMember.voiceChannel.delete()}; 与以前相同的警告
    • 删除临时文件的工作原理是什么? :(我怎样才能得到临时频道的ID来删除它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2021-04-13
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2021-07-01
    相关资源
    最近更新 更多