extend messages service for logging sent messages

This commit is contained in:
2026-02-17 23:30:24 +01:00
parent aa88d30244
commit cb7235fd69
6 changed files with 67 additions and 50 deletions

View File

@@ -12,42 +12,6 @@ export default class DiscordController extends EventEmitter {
super();
let channelListeners = new Map();
process.on("exit", async () => {
const channels = client.channels;
const logChannel = channels.cache.get(config.discord.channelIdLog);
await this.handleShutdown(logChannel);
process.exit(0);
});
process.on("SIGINT", async () => {
const channels = client.channels;
const logChannel = channels.cache.get(config.discord.channelIdLog);
await this.handleShutdown(logChannel);
process.exit(0);
});
process.on("SIGTERM", async () => {
const channels = client.channels;
const logChannel = channels.cache.get(config.discord.channelIdLog);
await this.handleShutdown(logChannel);
process.exit(0);
});
// 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) => {

View File

@@ -1,9 +1,18 @@
import type { MessagesServiceInterface } from "@avocadi/bot-core/entities/messages/messages.service";
import { createLogger } from "@avocadi/bot-core/lib/logger";
import type { User } from "discord.js";
import {
ChannelType,
type DMChannel,
type Message,
type PartialDMChannel,
type User,
} from "discord.js";
import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client";
export class MessagesService implements MessagesServiceInterface<User> {
export class MessagesService
implements MessagesServiceInterface<User, Message>
{
private logger = createLogger("MessagesService");
async sendToUser(userInput: User, message: string): Promise<void> {
@@ -15,6 +24,29 @@ export class MessagesService implements MessagesServiceInterface<User> {
this.logger.error(`User with ID ${userInput.id} not found.`);
}
}
async logMessage(message: Message): Promise<void> {
let recipient: User | null = null;
if (
message.channel.isDMBased() &&
message.channel.type !== ChannelType.GroupDM
) {
const channel = message.channel as DMChannel | PartialDMChannel;
recipient = channel.recipient;
}
let logMessage: string;
if (recipient) {
logMessage = `<@${message.author.id}> sent a message to <@${recipient.id}>:\n"${message.content}"`;
} else {
logMessage = `<@${message.author.id}> sent a message:\n"${message.content}"`;
}
await logChannelService.sendLogMessage(logMessage);
}
}
export const messagesService = new MessagesService();

View File

@@ -1,4 +1,5 @@
import { greetingsService } from "features/greeting/greetings.service";
import { logChannelService } from "features/log-channel/log-channel.service";
import client from "lib/client";
client.on("guildMemberAdd", async (member) => {
@@ -9,4 +10,7 @@ client.on("guildMemberAdd", async (member) => {
}
greetingsService.sendGreeting(member.user, member.user.username);
logChannelService.sendLogMessage(
`Neues Mitglied: <@${member.user.id}> (${member.user.tag}) ist dem Server beigetreten.`,
);
});

View File

@@ -0,0 +1,6 @@
import { messagesService } from "entitites/messages/messages.service";
import client from "lib/client";
client.on("messageCreate", async (message) => {
messagesService.logMessage(message);
});