dm forwarding discord and fluxer implementation

This commit is contained in:
mo
2026-03-10 14:52:58 +01:00
parent 330aabd928
commit fc8f90a4bc
3 changed files with 100 additions and 82 deletions

View File

@@ -26,22 +26,23 @@ export class MessagesService
} }
async logMessage(message: Message): Promise<void> { async logMessage(message: Message): Promise<void> {
let recipient: User | null = null;
if ( if (
!(
message.channel.isDMBased() && message.channel.isDMBased() &&
message.channel.type !== ChannelType.GroupDM message.channel.type !== ChannelType.GroupDM
) { )
)
return;
const channel = message.channel as DMChannel | PartialDMChannel; const channel = message.channel as DMChannel | PartialDMChannel;
recipient = channel.recipient; const recipient = channel.recipient?.id;
}
let logMessage: string; let logMessage: string;
if (recipient) { if (message.author.bot) {
logMessage = `<@${message.author.id}> sent a message to <@${recipient.id}>:\n"${message.content}"`; logMessage = `<@${message.author.id}> hat an <@${recipient}> geschrieben:\n"${message.content}"`;
} else { } else {
logMessage = `<@${message.author.id}> sent a message:\n"${message.content}"`; logMessage = `<@${message.author.id}> hat geschrieben:\n"${message.content}"`;
} }
await logChannelService.sendLogMessage(logMessage); await logChannelService.sendLogMessage(logMessage);

View File

@@ -5,10 +5,18 @@ import { messagesService } from "entities/messages/messages.service";
import client from "lib/client"; import client from "lib/client";
import { logger } from "lib/common-logger"; import { logger } from "lib/common-logger";
client.on("messageCreate", async (message) => { client.on("messageCreate", async (message: Message) => {
if (message.content?.startsWith(config.commandPrefix) && message.channel) { if (message.channel.isDMBased()) {
if (!resolveModContext(message)) return; await messagesService.logMessage(message);
}
if (message.content?.startsWith(config.commandPrefix) && message.channel) {
await recognizedCommand(message);
}
});
async function recognizedCommand(message: Message) {
if (!resolveModContext(message)) return;
await messagesService.logMessage(message); await messagesService.logMessage(message);
logger.info( logger.info(
`Command received: ${message.content} from user ${message.author.id}`, `Command received: ${message.content} from user ${message.author.id}`,
@@ -33,10 +41,9 @@ client.on("messageCreate", async (message) => {
if (result.success) { if (result.success) {
logger.info(`Command ${command} recognized.`); logger.info(`Command ${command} recognized.`);
await handleCommand(result.data, parameters, message.channel.id); handleCommand(result.data, parameters, message.channel.id);
} }
} }
});
async function resolveModContext(message: Message) { async function resolveModContext(message: Message) {
const guild = message.guild; const guild = message.guild;
@@ -45,9 +52,6 @@ async function resolveModContext(message: Message) {
return; return;
} }
const roleMod = guild.roles.cache.get(config.roleMapping.mod); const roleMod = guild.roles.cache.get(config.roleMapping.mod);
// const roleMod = guild.roles.cache.find(
// (r) => r.name === config.roleMapping.mod,
// );
if (!roleMod) { if (!roleMod) {
logger.error(`Role ${roleMod} not found in guild ${guild.name}.`); logger.error(`Role ${roleMod} not found in guild ${guild.name}.`);

View File

@@ -1,13 +1,7 @@
import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema"; import { CommandKeys } from "@avocadi/bot-core/entities/commands/commands.schema";
import { import { type Channel, Events, type Message } from "@fluxerjs/core";
ChannelManager,
type DMChannel,
Events,
type Message,
} from "@fluxerjs/core";
import { config } from "config"; import { config } from "config";
import { messagesService } from "entities/messages/messages.service"; import { messagesService } from "entities/messages/messages.service";
import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client"; import client from "lib/client";
import { logger } from "lib/common-logger"; import { logger } from "lib/common-logger";
import { handleCommand } from "./handle-command"; import { handleCommand } from "./handle-command";
@@ -18,14 +12,36 @@ client.on(Events.MessageCreate, async (message: Message) => {
const channel = const channel =
client.channels.get(message.channelId) ?? client.channels.get(message.channelId) ??
(await client.channels.fetch(message.channelId)); (await client.channels.fetch(message.channelId));
const messageAuthor = message.author;
if (channel?.isDM()) { if (channel?.isDM()) {
await logChannelService.sendLogMessage( await messagesService.logMessage(message);
`<@${messageAuthor?.id}> wrote to <@${client.user?.id}>\n"${message.content}"`, //await forward(message /*, channel as DMChannel*/);
);
return;
} }
if (message.content?.startsWith(config.commandPrefix) && channel) { 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); await messagesService.logMessage(message);
logger.info( logger.info(
`Command received: ${message.content} from user ${message.author.id}`, `Command received: ${message.content} from user ${message.author.id}`,
@@ -49,10 +65,7 @@ client.on(Events.MessageCreate, async (message: Message) => {
const result = CommandKeys.safeParse(command); const result = CommandKeys.safeParse(command);
if (result.success) { if (result.success) {
logger.info(`Command ${command} is valid.`); logger.info(`Command ${command} recognized.`);
await handleCommand(result.data, parameters, message.channel.id); await handleCommand(result.data, parameters, channel.id);
} else {
logger.warn(`Command ${command} is not recognized.`);
} }
} }
});