diff --git a/adapters/discord/src/entities/messages/messages.service.ts b/adapters/discord/src/entities/messages/messages.service.ts index afdea26..7d6ecab 100644 --- a/adapters/discord/src/entities/messages/messages.service.ts +++ b/adapters/discord/src/entities/messages/messages.service.ts @@ -26,22 +26,23 @@ export class MessagesService } async logMessage(message: Message): Promise { - let recipient: User | null = null; - if ( - message.channel.isDMBased() && - message.channel.type !== ChannelType.GroupDM - ) { - const channel = message.channel as DMChannel | PartialDMChannel; + !( + message.channel.isDMBased() && + message.channel.type !== ChannelType.GroupDM + ) + ) + return; + const channel = message.channel as DMChannel | PartialDMChannel; + + const recipient = channel.recipient?.id; - recipient = channel.recipient; - } let logMessage: string; - if (recipient) { - logMessage = `<@${message.author.id}> sent a message to <@${recipient.id}>:\n"${message.content}"`; + if (message.author.bot) { + logMessage = `<@${message.author.id}> hat an <@${recipient}> geschrieben:\n"${message.content}"`; } else { - logMessage = `<@${message.author.id}> sent a message:\n"${message.content}"`; + logMessage = `<@${message.author.id}> hat geschrieben:\n"${message.content}"`; } await logChannelService.sendLogMessage(logMessage); diff --git a/adapters/discord/src/listeners/messages/messages.listener.ts b/adapters/discord/src/listeners/messages/messages.listener.ts index 24213dc..51af406 100644 --- a/adapters/discord/src/listeners/messages/messages.listener.ts +++ b/adapters/discord/src/listeners/messages/messages.listener.ts @@ -5,39 +5,46 @@ 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.content?.startsWith(config.commandPrefix) && message.channel) { - if (!resolveModContext(message)) return; - +client.on("messageCreate", async (message: Message) => { + if (message.channel.isDMBased()) { 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); - } + if (message.content?.startsWith(config.commandPrefix) && message.channel) { + await recognizedCommand(message); } }); +async function recognizedCommand(message: Message) { + 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.`); + handleCommand(result.data, parameters, message.channel.id); + } +} + async function resolveModContext(message: Message) { const guild = message.guild; if (!guild) { @@ -45,9 +52,6 @@ async function resolveModContext(message: Message) { 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}.`); diff --git a/adapters/fluxer/src/listeners/messages/messages.listener.ts b/adapters/fluxer/src/listeners/messages/messages.listener.ts index 41c80a8..eac9e1b 100644 --- a/adapters/fluxer/src/listeners/messages/messages.listener.ts +++ b/adapters/fluxer/src/listeners/messages/messages.listener.ts @@ -1,13 +1,7 @@ import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema"; -import { - ChannelManager, - type DMChannel, - Events, - type Message, -} from "@fluxerjs/core"; +import { type Channel, Events, type Message } from "@fluxerjs/core"; import { config } from "config"; import { messagesService } from "entities/messages/messages.service"; -import { logChannelService } from "features/log-channel/log-channel.service"; import client from "lib/client"; import { logger } from "lib/common-logger"; import { handleCommand } from "./handle-command"; @@ -18,41 +12,60 @@ client.on(Events.MessageCreate, async (message: Message) => { const channel = client.channels.get(message.channelId) ?? (await client.channels.fetch(message.channelId)); - const messageAuthor = message.author; if (channel?.isDM()) { - await logChannelService.sendLogMessage( - `<@${messageAuthor?.id}> wrote to <@${client.user?.id}>\n"${message.content}"`, - ); - return; - } - if (message.content?.startsWith(config.commandPrefix) && channel) { await messagesService.logMessage(message); - logger.info( - `Command received: ${message.content} from user ${message.author.id}`, - ); + //await forward(message /*, channel as DMChannel*/); + } - 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} is valid.`); - await handleCommand(result.data, parameters, message.channel.id); - } else { - logger.warn(`Command ${command} is not recognized.`); - } + if (message.content?.startsWith(config.commandPrefix) && channel) { + await recognizedCommand(message, channel); } }); + +//async function forward(message: Message /*, channel: DMChannel*/) { +// const author = message.author; +// console.log(`forward message from ${author.username}`); +// let context: string; + +// getting the recipient is currently not supported in fluxer.js +/*if (message.author.bot) { + const channelId = channel.id; + const recipient = channel.recipients[0]?.id; + context = `<@${author}> hat an <@${recipient}> geschrieben:\n"${message.content}"`; + } else {*/ +//context = `<@${author.id}> wrote:\n"${message.content}"`; +//} + +//await logChannelService.sendLogMessage(context); +// `<@${messageAuthor?.id}> wrote to <@${client.user?.id}>\n"${message.content}"`, +//} + +async function recognizedCommand(message: Message, channel: Channel) { + if (!channel) 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, channel.id); + } +}