From 9a1a2a6852298069ffde603250fcbf3c9b135a7d Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 10 Feb 2023 13:20:07 +0000 Subject: [PATCH] feat(arabot): add button for managing access to non vegan section --- src/interaction-handlers/nonVeganAccess.ts | 86 ++++++++++++++++++++++ src/listeners/nonVeganAccess.ts | 78 ++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/interaction-handlers/nonVeganAccess.ts create mode 100644 src/listeners/nonVeganAccess.ts diff --git a/src/interaction-handlers/nonVeganAccess.ts b/src/interaction-handlers/nonVeganAccess.ts new file mode 100644 index 0000000..5160d43 --- /dev/null +++ b/src/interaction-handlers/nonVeganAccess.ts @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2023 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 { InteractionHandler, InteractionHandlerTypes, PieceContext } from '@sapphire/framework'; +import type { ButtonInteraction, GuildMember } from 'discord.js'; +import IDs from '#utils/ids'; + +export class NonVeganAccessButtonHandler extends InteractionHandler { + public constructor(ctx: PieceContext, options: InteractionHandler.Options) { + super(ctx, { + ...options, + interactionHandlerType: InteractionHandlerTypes.Button, + }); + } + + public override parse(interaction: ButtonInteraction) { + if (interaction.customId !== 'nvAccess') return this.none(); + + return this.some(); + } + + public async run(interaction: ButtonInteraction) { + let { member } = interaction; + + const errorMessage = 'There was an error giving you the role, please try again later or contact ModMail/the developer' + + 'to sort out this problem.'; + + if (member === null) { + await interaction.reply({ + content: errorMessage, + ephemeral: true, + }); + return; + } + + try { + member = member as GuildMember; + + if (!member.roles.cache.has(IDs.roles.vegan.vegan)) { + await interaction.reply({ + content: 'You need to be vegan to use this button!', + ephemeral: true, + }); + return; + } + + if (member.roles.cache.has(IDs.roles.vegan.activist)) { + await member.roles.remove(IDs.roles.vegan.activist); + await interaction.reply({ + content: 'Your access from the non vegan section has been removed successfully. ' + + 'If you want to gain access again, click this button again.', + ephemeral: true, + }); + return; + } + + await member.roles.add(IDs.roles.vegan.activist); + await interaction.reply({ + content: 'Your access to the non vegan section has been given back successfully. ' + + 'If you want to remove access again, click this button again.', + ephemeral: true, + }); + } catch (error) { + await interaction.reply({ + content: errorMessage, + ephemeral: true, + }); + } + } +} diff --git a/src/listeners/nonVeganAccess.ts b/src/listeners/nonVeganAccess.ts new file mode 100644 index 0000000..89243f4 --- /dev/null +++ b/src/listeners/nonVeganAccess.ts @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2023 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 { Listener } from '@sapphire/framework'; +import { ButtonStyle, ActionRowBuilder, ButtonBuilder } from 'discord.js'; + +import type { + Client, + TextChannel, +} from 'discord.js'; +import IDs from '#utils/ids'; + +export class NonVeganAccessReady extends Listener { + public constructor(context: Listener.Context, options: Listener.Options) { + super(context, { + ...options, + once: true, + event: 'ready', + }); + } + + public async run(client: Client) { + let roles = client.channels.cache + .get(IDs.channels.information.roles) as TextChannel | undefined; + if (roles === undefined) { + roles = await client.channels + .fetch(IDs.channels.information.roles) as TextChannel | undefined; + if (roles === undefined) { + this.container.logger.error('nonVeganAccess: Roles not found'); + return; + } + } + + const botId = this.container.client.id; + const messages = await roles.messages.fetch(); + const message = messages.first(); + + const content = 'If you\'re vegan want your access removed/added back to the non vegan sections' + + 'press the button bellow to gain/remove access to the non vegan sections.'; + + const button = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId('nvAccess') + .setLabel('Non Vegan Access') + .setStyle(ButtonStyle.Secondary), + ); + + if (message?.author.id !== botId) { + await roles.send({ + content, + components: [button], + }); + } else if (message?.author.id === botId && message?.components.length < 1) { + await message.delete(); + await roles.send({ + content, + components: [button], + }); + } + } +}