implement command handling
This commit is contained in:
23
adapters/fluxer/src/listeners/messages/handle-command.ts
Normal file
23
adapters/fluxer/src/listeners/messages/handle-command.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import { config } from "config";
|
||||
import { messagesService } from "entities/messages/messages.service";
|
||||
import client from "lib/client";
|
||||
import { logger } from "lib/common-logger";
|
||||
import { handleCommand } from "./handle-command";
|
||||
|
||||
client.on(Events.MessageCreate, async (message: Message) => {
|
||||
if (
|
||||
@@ -13,16 +14,24 @@ client.on(Events.MessageCreate, async (message: Message) => {
|
||||
await messagesService.logMessage(message);
|
||||
}
|
||||
|
||||
if (message.content?.startsWith(config.commandPrefix)) {
|
||||
if (message.content?.startsWith(config.commandPrefix) && message.channel) {
|
||||
await messagesService.logMessage(message);
|
||||
logger.info(
|
||||
`Command received: ${message.content} from user ${message.author.id}`,
|
||||
);
|
||||
|
||||
const command = message.content
|
||||
const commandWithoutPrefix = message.content
|
||||
.slice(config.commandPrefix.length)
|
||||
.trim()
|
||||
.split(" ")[0];
|
||||
.trim();
|
||||
|
||||
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}`);
|
||||
|
||||
@@ -30,6 +39,7 @@ client.on(Events.MessageCreate, async (message: Message) => {
|
||||
|
||||
if (result.success) {
|
||||
logger.info(`Command ${command} is valid.`);
|
||||
await handleCommand(result.data, parameters, message.channel.id);
|
||||
} else {
|
||||
logger.warn(`Command ${command} is not recognized.`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user