[英]How can I delete a temporary voice channel when everyone disconnects?
我編寫了代碼,如果有人連接到特定頻道,機器人將使用他們的名字創建一個頻道,然后將他們移動到其中。 我希望機器人在該用戶斷開連接並且沒有其他人連接到該頻道時自動刪除該頻道。 我有這個代碼,但我不知道如何刪除頻道。
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
沒有定義。
創建一個數組,將臨時通道的 ID 和創建此通道的服務器的 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)
}
}
})
您可以嘗試先定義一個空變量,例如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()};
});
這是基於Raifyks 的回答,但針對 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 (
// 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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.