diff --git a/src/events/mention/MentionDeleteEventHandler.ts b/src/events/mention/MentionDeleteEventHandler.ts index 2e594c43..f0042714 100644 --- a/src/events/mention/MentionDeleteEventHandler.ts +++ b/src/events/mention/MentionDeleteEventHandler.ts @@ -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'; @@ -8,22 +8,35 @@ export default class MentionDeleteEventHandler implements EventHandler<'messageR private logger = log4js.getLogger( 'MentionDeleteEventHandler' ); - public onEvent = async ( { message }: MessageReaction, user: User ): Promise => { + private botUserId: Snowflake; + constructor( botUserId: Snowflake ) { + this.botUserId = botUserId; + } + + public onEvent = async ( reaction: MessageReaction, user: User ): Promise => { + 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(); } }; } diff --git a/src/events/reaction/ReactionAddEventHandler.ts b/src/events/reaction/ReactionAddEventHandler.ts index e2f6b6e6..a6b9ea66 100644 --- a/src/events/reaction/ReactionAddEventHandler.ts +++ b/src/events/reaction/ReactionAddEventHandler.ts @@ -18,7 +18,7 @@ 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, requestLimits: Map ) { this.botUserId = botUserId; @@ -26,6 +26,7 @@ export default class ReactionAddEventHandler implements DiscordEventHandler<'mes 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