mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-19 15:04:29 +02:00
feat(arabot): add button for managing access to non vegan section
This commit is contained in:
parent
80025b54ab
commit
9a1a2a6852
86
src/interaction-handlers/nonVeganAccess.ts
Normal file
86
src/interaction-handlers/nonVeganAccess.ts
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
78
src/listeners/nonVeganAccess.ts
Normal file
78
src/listeners/nonVeganAccess.ts
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<ButtonBuilder>()
|
||||||
|
.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],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user