fix(arabot): check private channel already exists

This commit is contained in:
smyalygames 2023-01-15 19:22:35 +00:00
parent 2880283e9a
commit 8c395784d0

View File

@ -18,13 +18,13 @@
*/
import { Command, RegisterBehavior } from '@sapphire/framework';
import type { TextChannel } from 'discord.js';
import type { Guild, TextChannel } from 'discord.js';
import {
CategoryChannel,
ChannelType,
EmbedBuilder,
GuildMember,
PermissionsBitField,
PermissionsBitField, Snowflake,
time,
} from 'discord.js';
import IDs from '../../utils/ids';
@ -116,6 +116,15 @@ class PrivateCommand extends Command {
const [name, coordinator] = this.getCoordinator(modGuildMember);
if (this.checkPrivate(guildMember.id, coordinator, guild)) {
await interaction.reply({
content: 'A private channel already exists!',
ephemeral: true,
fetchReply: true,
});
return;
}
const voiceChannel = await guild.channels.create({
name: 'Private Voice Channel',
type: ChannelType.GuildVoice,
@ -353,6 +362,25 @@ class PrivateCommand extends Command {
}
return [name, id];
}
private checkPrivate(user: Snowflake, coordinator: string, guild: Guild) {
const category = guild.channels.cache.get(IDs.categories.private) as CategoryChannel | undefined;
if (category === undefined) {
return true;
}
const textChannels = category.children.cache.filter((c) => c.type === ChannelType.GuildText);
let exists = false;
textChannels.forEach((c) => {
const textChannel = c as TextChannel;
// Checks if the channel topic has the user's snowflake
if (textChannel.topic?.includes(user) && textChannel.topic?.includes(coordinator)) {
exists = true;
}
});
return exists;
}
}
export default PrivateCommand;