import { Commands, type CommandsType } from "commands"; import { ChannelType, Client, Events, IntentsBitField, type VoiceState, 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 { 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 { CustomMessageService } from "actions/customMessage/customMessage.service"; import { DynamicChannelService } from "actions/dynamicChannel/dynamicChannel.service"; import config from "config"; export default class DiscordController extends EventEmitter { private discordService!: DiscordService; waterMeService: WaterMeService; greetingService: GreetingService; medicationService: MedicationService; helpService: HelpService; activityService: ActivityService; dmService: DmService; customMessageService: CustomMessageService; channelListeners = new Map(); dynamicChannelService: DynamicChannelService; 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.customMessageService = new CustomMessageService(); this.dynamicChannelService = new DynamicChannelService(); let channelListeners = new Map(); // 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) { console.log("got msg"); this.dmService.forward(message); } }); client.on("guildMemberAdd", async (member) => { await this.greetingService.welcome(member); }); client.on( Events.VoiceStateUpdate, async (oldState: VoiceState, newState: VoiceState) => { // check if user joined a vc if ( (!oldState.channelId && newState.channelId) || oldState.channelId !== newState.channelId ) { // check if right vc if ( newState.channelId === config.discord.vchannelIdForTwo || newState.channelId === config.discord.vchannelIdForThree || newState.channelId === config.discord.vchannelIdForFour || newState.channelId === config.discord.vchannelIdForGroup ) { const channel = newState.channel; if (!channel) { console.error("channel not found"); return; } try { // create new channel with same settings /*const newChannel = await channel.clone({ name: channel.name + "; " + newState.member?.displayName, position: channel.position });*/ const newChannel = await this.dynamicChannelService.createChannel( oldState, newState, channel, ); // move user in new channel await newState.setChannel(newChannel); // create specific listener for channel const channelListener = async ( oldState: VoiceState, newState: VoiceState, ) => { /*if (oldState.channelId === newChannel.id || newState.channelId === newChannel.id) { // check if channel empty if (newChannel.members.size === 0) { newChannel.delete() .catch(console.error); // delete listener for this channel client.removeListener(Events.VoiceStateUpdate, channelListener); channelListeners.delete(newChannel.id); } }*/ channelListeners = await this.dynamicChannelService.deleteChannel( oldState, newState, newChannel, channelListeners, channelListener, ); }; // save listener in map channelListeners.set(newChannel.id, channelListener); // add listener client.on(Events.VoiceStateUpdate, channelListener); } catch (error) { console.error("error while duplicating channel", error); } } } }, ); } 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) { 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) { 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, ) { 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.customMessageService.handleInteraction(interaction); return; case Commands.Enum.message: await this.customMessageService.handleInteraction(interaction); return; case Commands.Enum.reminder: await this.greetingService.handleInteraction(interaction); return; default: break; } } // wenn neues fenster durch buttonclick or so async handleModalSubmit(interaction: ModalSubmitInteraction) { const { customId } = interaction; switch (customId) { default: break; } } }