Skip to content

Commit 19a4ab7

Browse files
committed
Implement LootDropCommand and update BotContext for development commands
1 parent fe7d34e commit 19a4ab7

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {
2+
type CommandInteraction,
3+
type GuildBasedChannel,
4+
SlashCommandBuilder,
5+
SlashCommandNumberOption,
6+
} from "discord.js";
7+
8+
import type { BotContext } from "#/context.ts";
9+
import type { ApplicationCommand } from "#/commands/command.ts";
10+
import { ensureChatInputCommand } from "#/utils/interactionUtils.ts";
11+
12+
import * as lootDataService from "#/service/lootData.ts";
13+
14+
import { postLootDrop } from "#/service/lootDrop.ts";
15+
16+
export default class LootDropCommand implements ApplicationCommand {
17+
name = "loot-drop";
18+
description = "Drops dir 1 Loot";
19+
20+
applicationCommand = new SlashCommandBuilder()
21+
.setName(this.name)
22+
.setDescription(this.description)
23+
.addNumberOption(
24+
new SlashCommandNumberOption()
25+
.setName("loot-kind-id")
26+
.setDescription("Loot ID die gedroppt werden soll")
27+
.setMinValue(0),
28+
);
29+
30+
async handleInteraction(interaction: CommandInteraction, context: BotContext) {
31+
const command = ensureChatInputCommand(interaction);
32+
33+
if (command.guild === null) {
34+
throw new Error("Interaction not in guild");
35+
}
36+
37+
const channel = command.channel as GuildBasedChannel;
38+
if (channel === null) {
39+
throw new Error("Interaction not in channel");
40+
}
41+
if (channel.isTextBased() === false) {
42+
throw new Error("Interaction not in text channel");
43+
}
44+
45+
const lootKindId = command.options.getNumber("loot-kind-id", false);
46+
if (lootKindId === null) {
47+
await command.reply({
48+
content: "Es muss eine Loot ID angegeben werden.",
49+
ephemeral: true,
50+
});
51+
return;
52+
}
53+
const lootTemplate = lootDataService.resolveLootTemplate(lootKindId);
54+
if (lootTemplate === undefined) {
55+
await command.reply({
56+
content: `Es konnte kein Loot mit der ID ${lootKindId} gefunden werden.`,
57+
ephemeral: true,
58+
});
59+
return;
60+
}
61+
await postLootDrop(context, channel, command.user, undefined, {
62+
rarity: undefined,
63+
template: lootTemplate,
64+
});
65+
await command.reply({
66+
content: `Es wurde ${lootTemplate.id} gedroppt!`,
67+
ephemeral: true,
68+
});
69+
}
70+
}

src/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export interface BotContext {
132132
sounds: string;
133133
commands: string;
134134
modCommands: string;
135+
devCommands: string;
135136
};
136137

137138
roleGuard: {
@@ -356,6 +357,7 @@ export async function createBotContext(client: Client<true>): Promise<BotContext
356357
sounds: soundsPath,
357358
commands: path.resolve("src/commands"),
358359
modCommands: path.resolve("src/commands/modcommands"),
360+
devCommands: path.resolve("src/commands/devcommands"),
359361
},
360362

361363
roleGuard: {

src/service/command.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export async function readAvailableCommands(context: BotContext): Promise<Comman
1616
const modules = [
1717
...(await loadRawCommandModulesFromDirectory(context.path.modCommands)),
1818
...(await loadRawCommandModulesFromDirectory(context.path.commands)),
19+
...(process.env.NODE_ENV === "development"
20+
? await loadRawCommandModulesFromDirectory(context.path.devCommands)
21+
: []),
1922
];
2023

2124
const res = [];

0 commit comments

Comments
 (0)