11import { Message , MessageEmbed , User } from 'discord.js' ;
2+ import { LimitedSizeMap } from './limitedSizeMap' ;
23
3- const MAX_TRACKED_MESSAGES = 1000 ;
4-
5- const messageToUserId = new Map < string , string > ( ) ;
4+ const messageToUserId = new LimitedSizeMap < string , string > ( 1000 ) ;
65
76export const DELETE_EMOJI = '🗑️' ;
87
@@ -11,18 +10,13 @@ export async function sendWithMessageOwnership(
1110 toSend : string | { embed : MessageEmbed } ,
1211) {
1312 const sent = await message . channel . send ( toSend ) ;
14- await sent . react ( DELETE_EMOJI ) ;
15-
16- addMessageOwnership ( sent , message . author ) ;
13+ await addMessageOwnership ( sent , message . author ) ;
1714}
1815
19- export function addMessageOwnership ( message : Message , user : User ) {
16+ export async function addMessageOwnership ( message : Message , user : User ) {
17+ await message . react ( DELETE_EMOJI ) ;
18+
2019 messageToUserId . set ( message . id , user . id ) ;
21- // Without this memory grows unboundedly... very slowly, but better to avoid the issue.
22- if ( messageToUserId . size > MAX_TRACKED_MESSAGES ) {
23- // Keys returns an iterable in insertion order, so we remove the oldest message from the map.
24- messageToUserId . delete ( messageToUserId . keys ( ) . next ( ) . value ) ;
25- }
2620}
2721
2822export function ownsBotMessage ( message : Message , userId : string ) {
0 commit comments