From 24dfa377172593d0c1a01e57bfabc0196820e7af Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 4 Aug 2022 04:49:35 +0100 Subject: [PATCH] feat(arabot): add toggle read-only command for diversity section --- src/commands/mod/diversityToggleOpen.ts | 104 ++++++++++++++++++ src/preconditions/DiversityCoordinatorOnly.ts | 52 +++++++++ src/utils/ids.ts | 9 ++ 3 files changed, 165 insertions(+) create mode 100644 src/commands/mod/diversityToggleOpen.ts create mode 100644 src/preconditions/DiversityCoordinatorOnly.ts diff --git a/src/commands/mod/diversityToggleOpen.ts b/src/commands/mod/diversityToggleOpen.ts new file mode 100644 index 0000000..93762b6 --- /dev/null +++ b/src/commands/mod/diversityToggleOpen.ts @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2022 Anthony Berg + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import { Command, RegisterBehavior } from '@sapphire/framework'; +import type { TextChannel } from 'discord.js'; +import { IDs } from '../../utils/ids'; + +export class ToggleOpenCommand extends Command { + public constructor(context: Command.Context, options: Command.Options) { + super(context, { + ...options, + name: 'toggleopen', + description: 'Toggles read-only for vegans in diversity section', + preconditions: ['DiversityCoordinatorOnly'], + }); + } + + // Registers that this is a slash command + public override registerApplicationCommands(registry: Command.Registry) { + registry.registerChatInputCommand( + (builder) => builder + .setName(this.name) + .setDescription(this.description), + { + behaviorWhenNotIdentical: RegisterBehavior.Overwrite, + }, + ); + } + + // Command run + public async chatInputRun(interaction: Command.ChatInputInteraction) { + // Check if guild is not null + if (interaction.guild === null) { + await interaction.reply({ + content: 'Guild not found!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Get the channel + const channel = interaction.guild.channels.cache.get(interaction.channelId); + // Check if channel is not undefined + if (channel === undefined) { + await interaction.reply({ + content: 'Channel not found!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Check if channel is text + if (!channel.isText()) { + await interaction.reply({ + content: 'Channel is not a text channel!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Converts GuildBasedChannel to TextChannel + const channelText = channel as TextChannel; + + // Check if the command was run in the diversity section + if (channel.parentId !== IDs.categories.diversity) { + await interaction.reply({ + content: 'Command was not run in the Diversity section!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Checks if the channel is open + const open = channel.permissionsFor(IDs.roles.vegan.vegan)!.has(['SEND_MESSAGES']); + + // Toggle send message in channel + await channelText.permissionOverwrites.edit(IDs.roles.vegan.vegan, { SEND_MESSAGES: !open }); + + await interaction.reply({ + content: `${!open ? 'Opened' : 'Closed'} this channel.`, + fetchReply: true, + }); + } +} diff --git a/src/preconditions/DiversityCoordinatorOnly.ts b/src/preconditions/DiversityCoordinatorOnly.ts new file mode 100644 index 0000000..55e2b13 --- /dev/null +++ b/src/preconditions/DiversityCoordinatorOnly.ts @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2022 Anthony Berg + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import { AllFlowsPrecondition } from '@sapphire/framework'; +import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js'; +import { IDs } from '../utils/ids'; +import type { GuildMember } from 'discord.js'; + +export class DiversityCoordinatorOnlyPrecondition extends AllFlowsPrecondition { + public override async messageRun(message: Message) { + // for message command + return this.checkDiversityCoordinator(message.member!); + } + + public override async chatInputRun(interaction: CommandInteraction) { + // for slash command + return this.checkDiversityCoordinator(interaction.member! as GuildMember); + } + + public override async contextMenuRun(interaction: ContextMenuInteraction) { + // for context menu command + return this.checkDiversityCoordinator(interaction.member! as GuildMember); + } + + private async checkDiversityCoordinator(user: GuildMember) { + return user.roles.cache.has(IDs.roles.staff.devCoordinator) + ? this.ok() + : this.error({ message: 'Only diversity coordinators can run this command!' }); + } +} + +declare module '@sapphire/framework' { + interface Preconditions { + DiversityCoordinatorOnly: never; + } +} diff --git a/src/utils/ids.ts b/src/utils/ids.ts index 80e87e7..e46a83c 100644 --- a/src/utils/ids.ts +++ b/src/utils/ids.ts @@ -56,6 +56,15 @@ const IDs = { coordinators: '989249700353953843', standup: '996009201237233684', }, + diversity: { + women: '938808963544285324', + lgbtqia: '956224226556272670', + potgm: '956224095509442600', + disabilities: '933078769365823518', + }, + }, + categories: { + diversity: '933078380394459146', }, };