233 lines
7.1 KiB
TypeScript
233 lines
7.1 KiB
TypeScript
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 "discord.service";
|
|
import { WaterMeService } from "actions/waterMe/waterMe.service";
|
|
import { MedicationService } from "actions/medication/medication.service";
|
|
import { HelpService } from "actions/help/help.service";
|
|
import { SupportService } from "actions/support/support.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 { DynamicVChannelService } from "actions/dynamicVChannel/dynamicVChannel.service";
|
|
import { ReactRolesService } from "actions/reactRole/reactRoles.service";
|
|
import config from "config";
|
|
|
|
export default class DiscordController extends EventEmitter {
|
|
private discordService: DiscordService;
|
|
private waterMeService: WaterMeService;
|
|
private greetingService: GreetingService;
|
|
private medicationService: MedicationService;
|
|
private helpService: HelpService;
|
|
private supportService: SupportService;
|
|
private activityService: ActivityService;
|
|
private dmService: DmService;
|
|
private customMessageService: CustomMessageService;
|
|
private channelListeners = new Map();
|
|
private dynamicVChannelService: DynamicVChannelService;
|
|
private reactRolesService: ReactRolesService;
|
|
|
|
|
|
constructor() {
|
|
super();
|
|
let channelListeners = new Map();
|
|
this.discordService = new DiscordService();
|
|
this.waterMeService = new WaterMeService();
|
|
this.greetingService = new GreetingService();
|
|
this.medicationService = new MedicationService();
|
|
this.helpService = new HelpService();
|
|
this.supportService = new SupportService();
|
|
this.activityService = new ActivityService();
|
|
this.dmService = new DmService();
|
|
this.customMessageService = new CustomMessageService();
|
|
this.dynamicVChannelService = new DynamicVChannelService();
|
|
this.reactRolesService = new ReactRolesService();
|
|
|
|
client.on("messageReactionAdd", async (reaction, user) => {
|
|
await this.reactRolesService.roleMention(reaction, user, true);
|
|
});
|
|
|
|
client.on("messageReactionRemove", async (reaction, user) => {
|
|
await this.reactRolesService.roleMention(reaction, user, false);
|
|
});
|
|
|
|
// 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");
|
|
await 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 {
|
|
const newChannel = await this.dynamicVChannelService.createVChannel(
|
|
newState,
|
|
channel,
|
|
);
|
|
// move user in new channel
|
|
await newState.setChannel(newChannel);
|
|
// create specific listener for channel
|
|
const channelListener = async (
|
|
oldState: VoiceState,
|
|
newState: VoiceState,
|
|
) => {
|
|
channelListeners =
|
|
await this.dynamicVChannelService.deleteVChannel(
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
);
|
|
console.log(`----------------\nversion ${config.discord.version}\n----------------`);
|
|
}
|
|
|
|
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); // zu chatinputcommand wechseln
|
|
return;
|
|
case Commands.Enum.medikamente:
|
|
await this.medicationService.handleChatInputCommand(interaction);
|
|
return;
|
|
case Commands.Enum.hilfe:
|
|
await this.helpService.handleInteraction(interaction); // zu chatinputcommand wechseln
|
|
return;
|
|
case Commands.Enum.support:
|
|
case Commands.Enum.kofi:
|
|
case Commands.Enum.disboard:
|
|
case Commands.Enum.discadia:
|
|
await this.supportService.handleInteraction(interaction);
|
|
return;
|
|
case Commands.Enum.accept:
|
|
await this.greetingService.handleChatInputCommand(interaction);
|
|
return;
|
|
case Commands.Enum.welcome:
|
|
await this.greetingService.handleChatInputCommand(interaction);
|
|
return;
|
|
case Commands.Enum.embed:
|
|
case Commands.Enum.message:
|
|
await this.customMessageService.handleChatInputCommand(interaction);
|
|
return;
|
|
case Commands.Enum.reminder:
|
|
await this.greetingService.handleChatInputCommand(interaction);
|
|
return;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// wenn neues fenster durch buttonclick or so
|
|
async handleModalSubmit(interaction: ModalSubmitInteraction<CacheType>) {
|
|
const { customId } = interaction;
|
|
|
|
switch (customId) {
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|