feat(arabot): add toggle read-only command for diversity section

This commit is contained in:
Anthony 2022-08-04 04:49:35 +01:00
parent 91e49b247c
commit 24dfa37717
3 changed files with 165 additions and 0 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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,
});
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -56,6 +56,15 @@ const IDs = {
coordinators: '989249700353953843',
standup: '996009201237233684',
},
diversity: {
women: '938808963544285324',
lgbtqia: '956224226556272670',
potgm: '956224095509442600',
disabilities: '933078769365823518',
},
},
categories: {
diversity: '933078380394459146',
},
};