Skip to content

Commit

Permalink
feat: Add a setting to control Melvin's laugh on a per-server basis, …
Browse files Browse the repository at this point in the history
…disabled by default
  • Loading branch information
LordLuceus committed May 18, 2024
1 parent 41f8141 commit 9f4cf7c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "guild" ADD COLUMN "laugh" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
98 changes: 98 additions & 0 deletions src/commands/admin/laugh.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
}
}
14 changes: 11 additions & 3 deletions src/commands/dice/roll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,29 @@ 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) {
return this.handleError(err, interaction);
}
}

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;
Expand Down

0 comments on commit 9f4cf7c

Please sign in to comment.