Bugfix: Deleted Original Channel

This commit is contained in:
2024-05-18 02:08:04 +02:00
parent afff8fa45f
commit 886ffd53f0

View File

@@ -4,6 +4,9 @@ const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBit
const token = process.env.BOT_TOKEN; const token = process.env.BOT_TOKEN;
const joinToCreateChannelId = process.env.JOINTOCREATE_CHANNEL_ID; const joinToCreateChannelId = process.env.JOINTOCREATE_CHANNEL_ID;
// Set to true if a channel was created by the bot
const CREATED_BY_BOT = 'created_by_bot';
client.once('ready', () => { client.once('ready', () => {
console.log('Ready!'); console.log('Ready!');
}); });
@@ -14,13 +17,18 @@ client.on('voiceStateUpdate', (oldState, newState) => {
name: `🔊 ${newState.member.displayName}'s Channel`, name: `🔊 ${newState.member.displayName}'s Channel`,
type: ChannelType.GuildVoice, type: ChannelType.GuildVoice,
parent: newState.channel.parentId, parent: newState.channel.parentId,
reason: 'Created by JoinToCreate bot', // Add a reason when creating the channel
}).then(channel => { }).then(channel => {
newState.member.voice.setChannel(channel); newState.member.voice.setChannel(channel);
channel[CREATED_BY_BOT] = true; // Mark the channel as created by the bot
}).catch(console.error); }).catch(console.error);
} }
if (oldState.channel && oldState.channel.members.size === 0 && oldState.channelId !== joinToCreateChannelId) { if (oldState.channel && oldState.channel.members.size === 0 && oldState.channel.id !== joinToCreateChannelId) {
oldState.channel.delete().catch(console.error); // Check if the channel was created by the bot before deleting it
if (oldState.channel[CREATED_BY_BOT]) {
oldState.channel.delete().catch(console.error);
}
} }
}); });