implement command handling

This commit is contained in:
2026-02-18 18:59:31 +01:00
parent 0a460800b6
commit ca4b64bde1
6 changed files with 48 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema";
import { waterMeService } from "features/water-me/water-me.service";
import type z from "zod";
export const handleCommand = async (
command: z.output<typeof CommandKeys>,
parameters: Array<string>,
channelId: string,
) => {
switch (command) {
case CommandKeys.enum["water-avocadi"]: {
// handle giessen command
waterMeService.handleCommand(channelId);
break;
}
default: {
// handle unknown command
break;
}
}
};

View File

@@ -4,6 +4,7 @@ import { config } from "config";
import { messagesService } from "entities/messages/messages.service"; import { messagesService } from "entities/messages/messages.service";
import client from "lib/client"; import client from "lib/client";
import { logger } from "lib/common-logger"; import { logger } from "lib/common-logger";
import { handleCommand } from "./handle-command";
client.on(Events.MessageCreate, async (message: Message) => { client.on(Events.MessageCreate, async (message: Message) => {
if ( if (
@@ -13,16 +14,24 @@ client.on(Events.MessageCreate, async (message: Message) => {
await messagesService.logMessage(message); await messagesService.logMessage(message);
} }
if (message.content?.startsWith(config.commandPrefix)) { if (message.content?.startsWith(config.commandPrefix) && message.channel) {
await messagesService.logMessage(message); await messagesService.logMessage(message);
logger.info( logger.info(
`Command received: ${message.content} from user ${message.author.id}`, `Command received: ${message.content} from user ${message.author.id}`,
); );
const command = message.content const commandWithoutPrefix = message.content
.slice(config.commandPrefix.length) .slice(config.commandPrefix.length)
.trim() .trim();
.split(" ")[0];
const commandParts = commandWithoutPrefix.split(" ");
if (commandParts.length === 0) {
logger.warn("No command found after prefix.");
return;
}
const [command, ...parameters] = commandParts;
logger.info(`Parsed command: ${command}`); logger.info(`Parsed command: ${command}`);
@@ -30,6 +39,7 @@ client.on(Events.MessageCreate, async (message: Message) => {
if (result.success) { if (result.success) {
logger.info(`Command ${command} is valid.`); logger.info(`Command ${command} is valid.`);
await handleCommand(result.data, parameters, message.channel.id);
} else { } else {
logger.warn(`Command ${command} is not recognized.`); logger.warn(`Command ${command} is not recognized.`);
} }

View File

@@ -36,6 +36,7 @@
"./entities/channels/voice/voice-channels.service": "./dist/entities/channels/voice/voice-channels.service.js", "./entities/channels/voice/voice-channels.service": "./dist/entities/channels/voice/voice-channels.service.js",
"./entities/commands/commands.entity": "./dist/entities/commands/commands.entity.js", "./entities/commands/commands.entity": "./dist/entities/commands/commands.entity.js",
"./entities/commands/commands.schema": "./dist/entities/commands/commands.schema.js", "./entities/commands/commands.schema": "./dist/entities/commands/commands.schema.js",
"./entities/components": "./dist/entities/components/index.js",
"./entities/interactions/interactions.schema": "./dist/entities/interactions/interactions.schema.js", "./entities/interactions/interactions.schema": "./dist/entities/interactions/interactions.schema.js",
"./entities/messages/messages.service": "./dist/entities/messages/messages.service.js", "./entities/messages/messages.service": "./dist/entities/messages/messages.service.js",
"./entities/roles/roles.schema": "./dist/entities/roles/roles.schema.js", "./entities/roles/roles.schema": "./dist/entities/roles/roles.schema.js",

View File

@@ -1,7 +1,7 @@
import z from "zod"; import z from "zod";
export const CommandKeyOptions = [ export const CommandKeyOptions = [
"giessen", "water-avocadi",
"medikamente", "medikamente",
"hilfe", "hilfe",
"support", "support",

View File

@@ -64,4 +64,13 @@ export class WaterMeService {
reply, reply,
}; };
} }
async handleCommand(channelId: string) {
const result = this.waterMe();
this.messagesService.sendToChannel(
{ id: channelId },
{ content: result.reply },
);
}
} }