Skip to content

Commit

Permalink
chore: better logging (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 authored Aug 7, 2024
1 parent 313a284 commit 53791c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/discord/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { MessageFlags } from 'discord.js';
import { ArgsOf, Discord, On, Once } from 'discordx';
import { ArgsOf, Discord, On, Once, RestArgsOf } from 'discordx';
import _ from 'lodash';
import { DiscordService } from 'src/services/discord.service';

Expand All @@ -19,6 +19,11 @@ export class DiscordEvents {

constructor(private service: DiscordService) {}

@On.rest({ event: 'restDebug' })
onDebug([message]: RestArgsOf<'restDebug'>) {
this.logger.debug(message);
}

@Once({ event: 'ready' })
async onReady() {
await this.service.onReady();
Expand All @@ -31,9 +36,6 @@ export class DiscordEvents {

@On({ event: 'messageCreate' })
async onMessageCreate([message]: ArgsOf<'messageCreate'>) {
this.logger.verbose(
`DiscordBot.onMessageCreate [${message.author.username}] ${shorten(message.content)} ${message.embeds.length} - embed(s)`,
);
if (message.author.bot) {
return;
}
Expand Down
32 changes: 31 additions & 1 deletion src/repositories/discord.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { Logger } from '@nestjs/common';
import { IntentsBitField, MessageCreateOptions, Partials } from 'discord.js';
import { Client } from 'discordx';
import { DiscordChannel, IDiscordInterface } from 'src/interfaces/discord.interface';

class DiscordLogger extends Logger {
constructor() {
super('DiscordBot');
}

info(...messages: string[]) {
super.debug(messages.join('\n'));
}

log(...messages: string[]) {
super.log(messages.join('\n'));
}

warn(...messages: string[]) {
super.warn(messages.join('\n'));
}

error(...messages: string[]) {
super.error(messages.join('\n'));
}
}

const bot = new Client({
// Discord intents
intents: [
Expand All @@ -15,6 +38,8 @@ const bot = new Client({
// Debug logs are disabled in silent mode
silent: false,

logger: new DiscordLogger(),

// Configuration for @SimpleCommand
simpleCommand: {
prefix: '/',
Expand All @@ -24,9 +49,14 @@ const bot = new Client({
});

export class DiscordRepository implements IDiscordInterface {
private logger = new Logger(DiscordRepository.name);

constructor() {
bot
.once('ready', async () => await bot.initApplicationCommands())
.once('ready', async () => {
// await bot.clearApplicationCommands();
await bot.initApplicationCommands();
})
.on('interactionCreate', (interaction) => bot.executeInteraction(interaction) as Promise<void>)
.on('messageCreate', (message) => bot.executeCommand(message) as Promise<void>);
}
Expand Down

0 comments on commit 53791c8

Please sign in to comment.