Skip to content

Commit

Permalink
Fix deletion of bot messages via 🗑
Browse files Browse the repository at this point in the history
  • Loading branch information
violine1101 committed Jun 25, 2023
1 parent e8cada1 commit 437f7b5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/events/mention/MentionDeleteEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageReaction, User } from 'discord.js';
import { MessageReaction, Snowflake, User } from 'discord.js';
import log4js from 'log4js';
import EventHandler from '../EventHandler.js';
import DiscordUtil from '../../util/DiscordUtil.js';
Expand All @@ -8,22 +8,35 @@ export default class MentionDeleteEventHandler implements EventHandler<'messageR

private logger = log4js.getLogger( 'MentionDeleteEventHandler' );

public onEvent = async ( { message }: MessageReaction, user: User ): Promise<void> => {
private botUserId: Snowflake;
constructor( botUserId: Snowflake ) {
this.botUserId = botUserId;
}

public onEvent = async ( reaction: MessageReaction, user: User ): Promise<void> => {
const message = reaction.message;

this.logger.info(
`User ${ DiscordUtil.getUserHandle( user ) } is attempting to delete message '${ message.id }'`
);

// Only delete own messages
if ( message.author?.id !== this.botUserId ) return;

// Check whether the footer of the message's embed matches the user's handle
const footer = message.embeds[0]?.footer?.text;
if ( footer === undefined ) return;

const userHandle = footer.match( /.{3,32}#[0-9]{4}/ );

if ( userHandle !== null && DiscordUtil.getUserHandle( user ) === userHandle[0] ) {
if ( DiscordUtil.getUserHandle( user ) === footer ) {
try {
this.logger.info( `Removing message '${ message.id }'` );
await message.delete();
} catch ( error ) {
this.logger.error( error );
}
} else {
this.logger.info( `Message '${ message.id }' was not removed; no permission` );
await reaction.remove();
}
};
}
3 changes: 2 additions & 1 deletion src/events/reaction/ReactionAddEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export default class ReactionAddEventHandler implements DiscordEventHandler<'mes
private readonly requestResolveEventHandler: RequestResolveEventHandler;
private readonly requestReactionRemovalEventHandler = new RequestReactionRemovalEventHandler();
private readonly requestReopenEventHandler: RequestReopenEventHandler;
private readonly mentionDeleteEventHandler = new MentionDeleteEventHandler();
private readonly mentionDeleteEventHandler: MentionDeleteEventHandler;

constructor( botUserId: Snowflake, internalChannels: Map<Snowflake, Snowflake>, requestLimits: Map<Snowflake, number> ) {
this.botUserId = botUserId;

const requestEventHandler = new RequestEventHandler( internalChannels, requestLimits );
this.requestResolveEventHandler = new RequestResolveEventHandler( botUserId );
this.requestReopenEventHandler = new RequestReopenEventHandler( botUserId, requestEventHandler );
this.mentionDeleteEventHandler = new MentionDeleteEventHandler( botUserId );
}

// This syntax is used to ensure that `this` refers to the `ReactionAddEventHandler` object
Expand Down

0 comments on commit 437f7b5

Please sign in to comment.