From dfb8b2781d4271e79bb155f1eef6481754a01107 Mon Sep 17 00:00:00 2001 From: mo Date: Mon, 9 Mar 2026 07:48:08 +0100 Subject: [PATCH] wip --- .../listeners/messages/messages.listener.ts | 68 ++++- .../handle-dynamic-voice-channel-creation.ts | 2 +- .../handle-dynamic-voice-channel-deletion.ts | 1 + .../listeners/messages/messages.listener.ts | 5 + .../customMessage/customMessage.service.ts | 238 +++++++++--------- .../accept-user/accept-user.service.ts | 4 - 6 files changed, 191 insertions(+), 127 deletions(-) diff --git a/adapters/discord/src/listeners/messages/messages.listener.ts b/adapters/discord/src/listeners/messages/messages.listener.ts index 28c7c39..24213dc 100644 --- a/adapters/discord/src/listeners/messages/messages.listener.ts +++ b/adapters/discord/src/listeners/messages/messages.listener.ts @@ -1,12 +1,70 @@ +import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema"; import { config } from "config"; +import type { Message } from "discord.js"; import { messagesService } from "entities/messages/messages.service"; import client from "lib/client"; +import { logger } from "lib/common-logger"; client.on("messageCreate", async (message) => { - if ( - message.channel.id !== config.channelMapping.text.log && - message.channel.id !== config.channelMapping.text.bot - ) { - messagesService.logMessage(message); + if (message.content?.startsWith(config.commandPrefix) && message.channel) { + if (!resolveModContext(message)) return; + + await messagesService.logMessage(message); + logger.info( + `Command received: ${message.content} from user ${message.author.id}`, + ); + + const commandWithoutPrefix = message.content + .slice(config.commandPrefix.length) + .trim(); + + const commandParts = commandWithoutPrefix.split(" "); + + if (commandParts.length === 0) { + logger.warn("No command found after prefix."); + return; + } + + const [command, ...parameters] = commandParts; + + logger.info(`Parsed command: ${command}`); + + const result = CommandKeys.safeParse(command); + + if (result.success) { + logger.info(`Command ${command} recognized.`); + await handleCommand(result.data, parameters, message.channel.id); + } } }); + +async function resolveModContext(message: Message) { + const guild = message.guild; + if (!guild) { + logger.error(`Guild not found for message ${message.id}.`); + return; + } + const roleMod = guild.roles.cache.get(config.roleMapping.mod); + // const roleMod = guild.roles.cache.find( + // (r) => r.name === config.roleMapping.mod, + // ); + + if (!roleMod) { + logger.error(`Role ${roleMod} not found in guild ${guild.name}.`); + return; + } + const member = await guild.members.fetch(message.author.id); + if (!member) { + logger.error( + `User with ID ${message.author.id} not found in guild ${guild.name}.`, + ); + throw new Error("User not found"); + } + if (!member.roles.cache.has(roleMod.id)) { + return; + } + throw new Error("Function not implemented."); +} +function handleCommand(data: string, parameters: string[], id: string) { + throw new Error("Function not implemented."); +} diff --git a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts index 417d523..bcc1547 100644 --- a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts +++ b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-creation.ts @@ -20,7 +20,7 @@ export const handleDynamicVoiceChannelCreation = async ( // New channel is created if user joined one of the specific channels (e.g. "Join to Create") or switched to one of them const newChannel = await dynamicVoiceChannelService.createDynamicVoiceChannel(channel.id); - + logger.debug(`Channel found for VoiceStateUpdate event: ${channel.name}`); if (newChannel) { await newState.setChannel(newChannel); } diff --git a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts index be25d9e..f6b6a5e 100644 --- a/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts +++ b/adapters/discord/src/listeners/voice-state/handle-dynamic-voice-channel-deletion.ts @@ -13,6 +13,7 @@ export const handleDynamicVoiceChannelDeletion = async ( if (dynamicVoiceChannelService.createdChannelIdsSet.has(channelId)) { const channel = await voiceChannelsService.getVoiceChannelById(channelId); + if (!channel) return; if (channel.members.size === 0) { await dynamicVoiceChannelService.deleteDynamicVoiceChannel(channelId); } diff --git a/adapters/fluxer/src/listeners/messages/messages.listener.ts b/adapters/fluxer/src/listeners/messages/messages.listener.ts index 890bba4..fe0710f 100644 --- a/adapters/fluxer/src/listeners/messages/messages.listener.ts +++ b/adapters/fluxer/src/listeners/messages/messages.listener.ts @@ -6,7 +6,12 @@ import client from "lib/client"; import { logger } from "lib/common-logger"; import { handleCommand } from "./handle-command"; +// https://fluxerjs.blstmo.com/v/latest/docs/classes/DMChannel + client.on(Events.MessageCreate, async (message: Message) => { + const messageAuthor = message.author; + const messageReceiver = message.type; + logger.info("got msg"); if (message.content?.startsWith(config.commandPrefix) && message.channel) { await messagesService.logMessage(message); logger.info( diff --git a/core/src/actions/customMessage/customMessage.service.ts b/core/src/actions/customMessage/customMessage.service.ts index f0821b0..de2f152 100644 --- a/core/src/actions/customMessage/customMessage.service.ts +++ b/core/src/actions/customMessage/customMessage.service.ts @@ -1,133 +1,137 @@ +import { Commands, type CommandsType } from "commands/index.ts"; import config from "config"; +import { + type CacheType, + ChannelType, + type ChatInputCommandInteraction, + Client, + EmbedBuilder, + type GuildMember, + type Interaction, + type Message, + type OmitPartialGroupDMChannel, +} from "discord.js"; +import { time } from "drizzle-orm/mysql-core"; import client from "lib/client"; import { getRandomInt } from "lib/utils"; -import { customContent, createEmbed } from "./customMessage.components.ts"; -import { - Client, - EmbedBuilder, - type Message, - type CacheType, - type GuildMember, - type Interaction, - type OmitPartialGroupDMChannel, - type ChatInputCommandInteraction, - ChannelType, -} from "discord.js"; -import { type CommandsType, Commands } from "commands/index.ts"; -import { time } from "drizzle-orm/mysql-core"; +import { createEmbed, customContent } from "./customMessage.components.ts"; export class CustomMessageService { - async handleInteraction(interaction: Interaction) { - if (interaction.isChatInputCommand()) { - await this.handleChatInputCommand(interaction); - return; - } - } + async handleInteraction(interaction: Interaction) { + if (interaction.isChatInputCommand()) { + await this.handleChatInputCommand(interaction); + return; + } + } - async handleChatInputCommand( - interaction: ChatInputCommandInteraction, - ) { - console.log("accept"); - const commandName = interaction.commandName as CommandsType; - switch (commandName) { - case Commands.Enum.embed: - await this.customEmbed(interaction); - return; - case Commands.Enum.message: - await this.customMessage(interaction); - return; - default: - break; - } - } + async handleChatInputCommand( + interaction: ChatInputCommandInteraction, + ) { + console.log("accept"); + const commandName = interaction.commandName as CommandsType; + switch (commandName) { + case Commands.Enum.embed: + await this.customEmbed(interaction); + return; + case Commands.Enum.message: + await this.customMessage(interaction); + return; + default: + break; + } + } - async checkPermission(interaction: ChatInputCommandInteraction) { - const userIdCommand = interaction.user.id; - if (userIdCommand !== config.discord.myId) { - await interaction.reply({ - content: "you have no permission for that command", - ephemeral: true, - }); - return false; - } - return true; - } + async checkPermission(interaction: ChatInputCommandInteraction) { + const userIdCommand = interaction.user.id; + if (userIdCommand !== config.discord.myId) { + await interaction.reply({ + content: "you have no permission for that command", + ephemeral: true, + }); + return false; + } + return true; + } - // check if command done in server - async checkIfServer(interaction: ChatInputCommandInteraction) { - const guild = interaction.guild; - if (!guild) { - await interaction.reply({ - content: "command can only be used on a server", - ephemeral: true, - }); - return false; - } - return true; - } + // check if command done in server + async checkIfServer(interaction: ChatInputCommandInteraction) { + const guild = interaction.guild; + if (!guild) { + await interaction.reply({ + content: "command can only be used on a server", + ephemeral: true, + }); + return false; + } + return true; + } - async customEmbed(interaction: ChatInputCommandInteraction) { - const title = interaction.options.getString("title") || " "; - const description = interaction.options.getString("description") || " "; - const timestamp = interaction.options.getBoolean("timestamp") || false; - // return the value - console.log(title, description, timestamp); + async customEmbed(interaction: ChatInputCommandInteraction) { + const title = interaction.options.getString("title") || " "; + const description = interaction.options.getString("description") || " "; + const timestamp = interaction.options.getBoolean("timestamp") || false; + // return the value + console.log(title, description, timestamp); - // permission check - // permission check - if (await this.checkPermission(interaction) && await this.checkIfServer(interaction)) - try { - const channels = client.channels; - const channel = channels.cache.get(interaction.channelId); + // permission check + // permission check + if ( + (await this.checkPermission(interaction)) && + (await this.checkIfServer(interaction)) + ) + try { + const channels = client.channels; + const channel = channels.cache.get(interaction.channelId); - if (channel?.isTextBased() && channel?.isSendable()) { - await channel.send({ - embeds: [createEmbed(title, description, timestamp)], - }); - } - await interaction.reply({ - content: "successfully created embed", - ephemeral: true, - }); - } catch (error) { - console.error("error while creating embed:", error); - await interaction.reply({ - content: - "error while creating embed", - ephemeral: true, - }); - } - } + if (channel?.isTextBased() && channel?.isSendable()) { + await channel.send({ + embeds: [createEmbed(title, description, timestamp)], + }); + } + await interaction.reply({ + content: "successfully created embed", + ephemeral: true, + }); + } catch (error) { + console.error("error while creating embed:", error); + await interaction.reply({ + content: "error while creating embed", + ephemeral: true, + }); + } + } - async customMessage(interaction: ChatInputCommandInteraction) { - const input: string = interaction.options.getString("input") || ""; - const result = input.replaceAll(";", "\n"); + async customMessage(interaction: ChatInputCommandInteraction) { + const input: string = interaction.options.getString("input") || ""; + const result = input.replaceAll(";", "\n"); - // return the value - console.log(input); + // return the value + console.log(input); - // permission check && server check - if (await this.checkPermission(interaction) && await this.checkIfServer(interaction)) - try { - const channels = client.channels; - const channel = channels.cache.get(interaction.channelId); + // permission check && server check + if ( + (await this.checkPermission(interaction)) && + (await this.checkIfServer(interaction)) + ) + try { + const channels = client.channels; + const channel = channels.cache.get(interaction.channelId); - if (channel?.isTextBased() && channel?.isSendable()) { - await channel.send({ - content: result, - }); - } - await interaction.reply({ - content: "successfully created message", - ephemeral: true, - }); - } catch (error) { - console.error("error while creating message:", error); - await interaction.reply({ - content: - "error while creating message", - ephemeral: true, - }); - } - } + if (channel?.isTextBased() && channel?.isSendable()) { + await channel.send({ + content: result, + }); + } + await interaction.reply({ + content: "successfully created message", + ephemeral: true, + }); + } catch (error) { + console.error("error while creating message:", error); + await interaction.reply({ + content: "error while creating message", + ephemeral: true, + }); + } + } } diff --git a/core/src/features/accept-user/accept-user.service.ts b/core/src/features/accept-user/accept-user.service.ts index d380352..2b7b9c5 100644 --- a/core/src/features/accept-user/accept-user.service.ts +++ b/core/src/features/accept-user/accept-user.service.ts @@ -22,10 +22,6 @@ export class AcceptUserService { } async sendDmAcceptUser(user: U) { - this.logger.info("test"); - - this.logger.info(`accepting ${user.id}...`); - const acceptUserMessage: string = await this.i18nService.t( "dmAcceptUser", this.lang,