avocadi-bot/src/controllers/discord.controller.ts
2025-01-01 18:53:33 +01:00

119 lines
3.1 KiB
TypeScript

import { Commands, type CommandsType } from "commands";
import type {
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
Interaction,
ModalSubmitInteraction,
} from "discord.js";
import client from "lib/client";
import EventEmitter from "node:events";
import DiscordService from "services/discord.service";
import config from "config";
import { WaterMeService } from "actions/waterMe/waterMe.service";
import { MedicationService } from "actions/medication/medication.service";
import { HelpService } from "actions/help/help.service";
import { custom } from "zod";
export default class DiscordController extends EventEmitter {
private discordService!: DiscordService;
waterMeService: WaterMeService;
medicationService: MedicationService;
helpService: HelpService;
constructor() {
super();
this.discordService = new DiscordService();
this.waterMeService = new WaterMeService();
this.medicationService = new MedicationService();
this.helpService = new HelpService();
// log when running
client.once("ready", async () => {
this.setActivity();
/*const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (channel?.isTextBased && channel?.isSendable()) {
await channel.send({
content: "bin wieder da :o war kurz weg :3",
});
}*/
});
// listen for interactions
client.on("interactionCreate", this.handleInteraction.bind(this));
}
async setActivity() {
client.user?.setActivity("frohes neues :3", { type: 4 });
console.log("set activity");
client.user?.setPresence({
status: "online",
});
}
async init() {
await this.discordService.init();
}
async handleInteraction(interaction: Interaction<CacheType>) {
if (interaction.isModalSubmit()) {
await this.handleModalSubmit(interaction);
return;
}
if (interaction.isChatInputCommand()) {
await this.handleChatInputCommand(interaction);
return;
}
if (interaction.isButton()) {
await this.handleButton(interaction);
return;
}
}
async handleButton(interaction: ButtonInteraction<CacheType>) {
const { customId } = interaction;
console.log(interaction.customId);
if (customId.toLowerCase().includes("moreWater")) {
await this.waterMeService.handleInteraction(interaction);
}
if (customId.toLowerCase().includes("medication")) {
await this.medicationService.handleInteraction(interaction);
}
}
async handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const commandName = interaction.commandName as CommandsType;
// add commands
switch (commandName) {
case Commands.Enum.giessen:
await this.waterMeService.handleInteraction(interaction);
return;
case Commands.Enum.medikamente:
await this.medicationService.handleInteraction(interaction);
return;
case Commands.Enum.hilfe:
await this.helpService.handleInteraction(interaction);
return;
default:
break;
}
}
// wenn neues fenster durch buttonclick or so
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
const { customId } = interaction;
switch (customId) {
default:
break;
}
}
}