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.`);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
"./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.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/messages/messages.service": "./dist/entities/messages/messages.service.js",
|
||||
"./entities/roles/roles.schema": "./dist/entities/roles/roles.schema.js",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import z from "zod";
|
||||
|
||||
export const CommandKeyOptions = [
|
||||
"giessen",
|
||||
"water-avocadi",
|
||||
"medikamente",
|
||||
"hilfe",
|
||||
"support",
|
||||
|
||||
@@ -64,4 +64,13 @@ export class WaterMeService {
|
||||
reply,
|
||||
};
|
||||
}
|
||||
|
||||
async handleCommand(channelId: string) {
|
||||
const result = this.waterMe();
|
||||
|
||||
this.messagesService.sendToChannel(
|
||||
{ id: channelId },
|
||||
{ content: result.reply },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user