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.type !== ChannelType.GroupDM message.channel.isDMBased() &&
) { message.channel.type !== ChannelType.GroupDM
const channel = message.channel as DMChannel | PartialDMChannel; )
)
return;
const channel = message.channel as DMChannel | PartialDMChannel;
const recipient = channel.recipient?.id;
recipient = channel.recipient;
}
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,39 +5,46 @@ 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); await messagesService.logMessage(message);
logger.info( }
`Command received: ${message.content} from user ${message.author.id}`,
);
const commandWithoutPrefix = message.content if (message.content?.startsWith(config.commandPrefix) && message.channel) {
.slice(config.commandPrefix.length) await recognizedCommand(message);
.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 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) { async function resolveModContext(message: Message) {
const guild = message.guild; const guild = message.guild;
if (!guild) { if (!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,41 +12,60 @@ 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(
`<@${messageAuthor?.id}> wrote to <@${client.user?.id}>\n"${message.content}"`,
);
return;
}
if (message.content?.startsWith(config.commandPrefix) && channel) {
await messagesService.logMessage(message); await messagesService.logMessage(message);
logger.info( //await forward(message /*, channel as DMChannel*/);
`Command received: ${message.content} from user ${message.author.id}`, }
);
const commandWithoutPrefix = message.content if (message.content?.startsWith(config.commandPrefix) && channel) {
.slice(config.commandPrefix.length) await recognizedCommand(message, channel);
.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.`);
}
} }
}); });
//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);
}
}