Files
bot/src/controllers/discord.controller.ts
2025-01-07 00:31:01 +01:00

150 lines
4.1 KiB
TypeScript

import { Commands, type CommandsType } from "commands";
import {
ChannelType,
Client,
Events,
IntentsBitField,
type ButtonInteraction,
type CacheType,
type ChatInputCommandInteraction,
type Interaction,
type 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 { GreetingService } from "actions/greeting/greeting.service";
import { ActivityService } from "actions/activity/activity.service";
import { DmService } from "actions/dm/dm.service";
import { EmbedService } from "actions/embed/embed.service";
export default class DiscordController extends EventEmitter {
private discordService!: DiscordService;
waterMeService: WaterMeService;
greetingService: GreetingService;
medicationService: MedicationService;
helpService: HelpService;
activityService: ActivityService;
dmService: DmService;
embedService: EmbedService;
constructor() {
super();
this.discordService = new DiscordService();
this.waterMeService = new WaterMeService();
this.greetingService = new GreetingService();
this.medicationService = new MedicationService();
this.helpService = new HelpService();
this.activityService = new ActivityService();
this.dmService = new DmService();
this.embedService = new EmbedService();
// log when running
client.once("ready", async () => {
await this.setActivity();
console.log("ready");
});
// listen for interactions
client.on("interactionCreate", this.handleInteraction.bind(this));
client.on("messageCreate", async (message) => {
console.log(message.id)
if (message.channel.type === ChannelType.DM
&& message.author.id !== config.discord.botId
) {
console.log("got msg");
this.dmService.forward(message);
}
});
client.on("guildMemberAdd", async (member) => {
await this.greetingService.welcome(member);
await this.dmService.welcomePrivate(member);
});
}
async setActivity() {
client.user?.setActivity(":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;
case Commands.Enum.accept:
await this.greetingService.handleInteraction(interaction);
return;
case Commands.Enum.welcome:
await this.greetingService.handleInteraction(interaction);
return;
case Commands.Enum.embed:
await this.embedService.handleInteraction(interaction);
return;
default:
break;
}
}
// wenn neues fenster durch buttonclick or so
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
const { customId } = interaction;
switch (customId) {
default:
break;
}
}
}