Skip to content

Commit

Permalink
Re-add logging for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tanndlin committed Dec 29, 2023
1 parent 3195d56 commit 3133e39
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/app/commands/addroles.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const command = {

interaction.followUp(`Successfully added: ${role.name}`);
})
.catch(() => {
.catch((e) => {
container.loggerService.error(`Error adding role: ${e}`);
interaction.followUp(`Failed to add role: ${role.name}`);
});
},
Expand Down
5 changes: 3 additions & 2 deletions src/app/commands/delroles.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const command = {
},
],

async execute({ interaction }) {
async execute({ interaction, container }) {
const member = interaction.member as GuildMember;
if (!member) {
await interaction.reply('Could not resolve you to a member');
Expand All @@ -39,7 +39,8 @@ const command = {
.then(() => {
interaction.followUp(`Successfully removed: ${role.name}`);
})
.catch(() => {
.catch((e) => {
container.loggerService.error(`Error removing role: ${e}`);
interaction.followUp(`Failed to remove role: ${role.name}`);
});
},
Expand Down
36 changes: 28 additions & 8 deletions src/app/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from 'discord.js';
import Constants from '../common/constants';
import { Handler } from '../common/handler';
import { commands, ICommand } from '../common/slash';
import { IContainer, IHandler, IMessage, Mode } from '../common/types';
import { ICommand, commands } from '../common/slash';
import { IContainer, IHandler, IMessage, IPluginEvent, Mode } from '../common/types';

export class Listener {
constructor(public container: IContainer) {
Expand Down Expand Up @@ -93,12 +93,32 @@ export class Listener {

const command = commands.get(interaction.commandName) as ICommand | undefined;

// For future proofing against any new command types we verify the command type
// being run is intentional.
command?.execute({
interaction: interaction,
container: this.container,
});
const pEvent: IPluginEvent = {
status: 'starting',
pluginName: interaction.commandName,
args: interaction.options.data,
user: interaction.user.id,
};

try {
this.container.loggerService.info(JSON.stringify(pEvent));

// For future proofing against any new command types we verify the command type
// being run is intentional.
await command?.execute({
interaction: interaction,
container: this.container,
});

pEvent.status = 'fulfillCommand';
this.container.loggerService.info(JSON.stringify(pEvent));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
pEvent.status = 'error';
pEvent.error = e.message as string;
pEvent.stack = e.stack as string;
this.container.loggerService.error(JSON.stringify(pEvent));
}
});

this.container.clientService.on('messageCreate', async (message: IMessage) => {
Expand Down
4 changes: 2 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ export enum RoleType {
}

export interface IEvent {
status: string;
status: 'starting' | 'fulfillCommand' | 'error' | 'fulfillJob';
error?: string;
stack?: string;
}

export interface IPluginEvent extends IEvent {
args: string[];
args: readonly discord.CommandInteractionOption<discord.CacheType>[];
user: string;
pluginName: string;
}
Expand Down

0 comments on commit 3133e39

Please sign in to comment.