avocadi-bot/src/controllers/discord.controller.ts
2024-12-30 22:45:41 +01:00

108 lines
2.8 KiB
TypeScript

import { Commands, type CommandsType } from "components/commands.component";
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 { WaterMeService } from "actions/waterMe/waterMe.service";
import config from "config";
import { MedicationService } from "actions/medication/medication.service";
export default class DiscordController extends EventEmitter {
private discordService!: DiscordService;
waterMeService: WaterMeService;
medicationService: MedicationService;
constructor() {
super();
this.discordService = new DiscordService();
this.waterMeService = new WaterMeService();
this.medicationService = new MedicationService();
// log when running
client.once("ready", async () => {
client.user?.setActivity("HALLOOO HOERT IHR MICH?", { type: 4 });
console.log("set activity");
client.user?.setPresence({
status: "idle",
});
/*const channels = client.channels;
const channel = channels.cache.get(config.discord.channelId);
if (channel?.isTextBased && channel?.isSendable()) {
await channel.send({
content: "bin gerade erst aufgewacht :o",
});
}*/
});
// listen for interactions
client.on("interactionCreate", this.handleInteraction.bind(this));
}
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 === "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;
default:
break;
}
}
// wenn neues fenster durch buttonclick or so
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
const { customId } = interaction;
switch (customId) {
default:
break;
}
}
}