import { Injectable } from '@nestjs/common' ; import { On } from 'discord-nestjs' ; import { Message } from 'discord.js' ; import { AlexService } from './alex.service' ; interface Notifications { messageId : string ; channelId : string ; notificationId : string ; } @ Injectable ( ) export class AlexHandler { private savedNotifications : Notifications [ ] = [ ] ; constructor ( private readonly alexService ) { } @ On ( { event : 'message' } ) async onMessage ( message ) { if ( message . author . bot ) { return ; } const notifications = this . alexService . check ( message ) ; if ( notifications . length ) { const sent = await message . channel . send ( notifications [ 0 ] ) ; this . savedNotifications . push ( { messageId : message . id , channelId : message . channel . id , notificationId : sent . id , } ) ; } return ; } @ On ( { event : 'messageUpdate' } ) async onMessageUpdate ( oldMessage , newMessage ) { if ( newMessage . author . bot ) { return ; } const notifications = this . alexService . check ( newMessage ) ; const targetNotification = this . savedNotifications . find ( ( el ) => el . messageId === newMessage . id && el . channelId === newMessage . channel . id ) ; if ( ! targetNotification && notifications . length ) { const sent = await newMessage . channel . send ( notifications [ 0 ] ) ; this . savedNotifications . push ( { messageId : newMessage . id , channelId : newMessage . channel . id , notificationId : sent . id , } ) ; return ; } if ( targetNotification && ! notifications . length ) { const notificationMessage = await newMessage . channel . messages . fetch ( targetNotification . notificationId ) ; if ( notificationMessage && notificationMessage . deletable ) { try { await notificationMessage . delete ( ) ; } catch ( error ) { console . error ( error ) ; } this . savedNotifications = this . savedNotifications . filter ( ( el ) => el . messageId !== newMessage . id || el . channelId !== newMessage . channel . id ) ; return ; } } if ( targetNotification && notifications . length ) { const notificationMessage = await newMessage . channel . messages . fetch ( targetNotification . notificationId ) ; if ( notificationMessage ) { await notificationMessage . edit ( notifications [ 0 ] ) ; return ; } } return ; } @ On ( { event : 'messageDelete' } ) async onMessageDelete ( deletedMessage ) { const deletedNotification = this . savedNotifications . find ( ( message ) => message . messageId === deletedMessage . id && message . channelId === deletedMessage . channel . id ) ; if ( deletedNotification ) { this . savedNotifications = this . savedNotifications . filter ( ( notification ) => notification . channelId !== deletedNotification . channelId && notification . messageId !== deletedNotification . messageId ) ; const notificationMessage = await deletedMessage . channel . messages . fetch ( deletedNotification . notificationId ) ; if ( notificationMessage && notificationMessage . deletable ) { try { await notificationMessage . delete ( ) ; } catch ( error ) { console . error ( error ) ; } } } return ; } }