From 9f4cf7c103fbe9bdfbed228220205c4e905e1f92 Mon Sep 17 00:00:00 2001 From: LordLuceus Date: Sat, 18 May 2024 12:13:45 +0200 Subject: [PATCH] feat: Add a setting to control Melvin's laugh on a per-server basis, disabled by default --- .../migration.sql | 2 + prisma/schema.prisma | 1 + src/commands/admin/laugh.ts | 98 +++++++++++++++++++ src/commands/dice/roll.ts | 14 ++- 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 prisma/migrations/20240518094149_update_guild_add_laugh_flag/migration.sql create mode 100644 src/commands/admin/laugh.ts diff --git a/prisma/migrations/20240518094149_update_guild_add_laugh_flag/migration.sql b/prisma/migrations/20240518094149_update_guild_add_laugh_flag/migration.sql new file mode 100644 index 0000000..6ee404c --- /dev/null +++ b/prisma/migrations/20240518094149_update_guild_add_laugh_flag/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "guild" ADD COLUMN "laugh" BOOLEAN NOT NULL DEFAULT false; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6cc3e42..01c4af0 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,6 +10,7 @@ datasource db { model guild { id String @id(map: "PK_cfbbd0a2805cab7053b516068a3") @db.VarChar(20) gmChannel String? @db.VarChar(20) + laugh Boolean @default(false) rolls roll[] users user[] rollGroups RollGroup[] diff --git a/src/commands/admin/laugh.ts b/src/commands/admin/laugh.ts new file mode 100644 index 0000000..5114925 --- /dev/null +++ b/src/commands/admin/laugh.ts @@ -0,0 +1,98 @@ +import { + ChatInputCommand, + Command, + CommandOptionsRunTypeEnum, + LogLevel, + RegisterBehavior, +} from "@sapphire/framework"; +import { PermissionFlagsBits } from "discord.js"; +import { writeLog } from "../../util"; + +export class LaughCommand extends Command { + public constructor(context: Command.LoaderContext, options: Command.Options) { + super(context, { + ...options, + name: "laugh", + description: "Enable or disable Melvin's evil laugh when rolling a 1", + cooldownDelay: 5000, + cooldownLimit: 1, + requiredUserPermissions: [PermissionFlagsBits.ManageGuild], + runIn: CommandOptionsRunTypeEnum.GuildText, + }); + } + + public override registerApplicationCommands( + registry: ChatInputCommand.Registry + ) { + registry.registerChatInputCommand( + (builder) => + builder + .setName(this.name) + .setDescription(this.description) + .addBooleanOption((option) => + option + .setName("enabled") + .setDescription("Enable or disable Melvin's evil laugh") + .setRequired(true) + ), + { + behaviorWhenNotIdentical: RegisterBehavior.Overwrite, + } + ); + } + + public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { + const { guild } = interaction; + if (!guild) return null; + + const { prisma } = this.container; + + const enabled = interaction.options.getBoolean("enabled", true); + + const savedGuild = await prisma.guild.findUnique({ + where: { id: guild.id }, + }); + + try { + if (!savedGuild) { + await prisma.guild.create({ + data: { + id: guild.id, + laugh: enabled, + }, + }); + + return interaction.reply({ + content: `${ + enabled + ? "Foolish Steve! You shall regret this. Buahahahaha!" + : "Foolish Steve! I knew you could not handle it. Buahahahaha!" + }`, + ephemeral: true, + }); + } + + await prisma.guild.update({ + where: { id: guild.id }, + data: { + laugh: enabled, + }, + }); + + return interaction.reply({ + content: `${ + enabled + ? "Foolish Steve! You shall regret this. Buahahahaha!" + : "Foolish Steve! I knew you could not handle it. Buahahahaha!" + }`, + ephemeral: true, + }); + } catch (err: any) { + writeLog(LogLevel.Error, this.name, err.message); + return interaction.reply({ + content: `What the frig? There was an error setting the laugh option: ${err.message}`, + ephemeral: true, + }); + } + } +} diff --git a/src/commands/dice/roll.ts b/src/commands/dice/roll.ts index 920ee93..527896f 100644 --- a/src/commands/dice/roll.ts +++ b/src/commands/dice/roll.ts @@ -111,7 +111,7 @@ export class RollCommand extends Command { interaction.guild?.id ); - RollCommand.laugh(roll.roll as DiceRoll, interaction, display, secret); + this.laugh(roll.roll as DiceRoll, interaction, display, secret); return this.composeReply(interaction, roll, display, secret); } catch (err: any) { @@ -119,13 +119,21 @@ export class RollCommand extends Command { } } - private static laugh( + private async laugh( roll: DiceRoll, interaction: Command.ChatInputCommandInteraction, output: string, secret: boolean | null ) { - if (output !== "output" || secret) return; + if (output !== "output" || secret || !interaction.guild) return; + + const { prisma } = this.container; + + const guild = await prisma.guild.findUnique({ + where: { id: interaction.guild.id }, + }); + + if (!guild?.laugh) return; if (hasD20(roll.notation)) { const result = (roll.rolls[0] as RollResults).value;