1
1
import { Message , MessageEmbed , User } from 'discord.js' ;
2
+ import { LimitedSizeMap } from './limitedSizeMap' ;
2
3
3
- const MAX_TRACKED_MESSAGES = 1000 ;
4
-
5
- const messageToUserId = new Map < string , string > ( ) ;
4
+ const messageToUserId = new LimitedSizeMap < string , string > ( 1000 ) ;
6
5
7
6
export const DELETE_EMOJI = '🗑️' ;
8
7
@@ -11,18 +10,13 @@ export async function sendWithMessageOwnership(
11
10
toSend : string | { embed : MessageEmbed } ,
12
11
) {
13
12
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 ) ;
17
14
}
18
15
19
- export function addMessageOwnership ( message : Message , user : User ) {
16
+ export async function addMessageOwnership ( message : Message , user : User ) {
17
+ await message . react ( DELETE_EMOJI ) ;
18
+
20
19
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
- }
26
20
}
27
21
28
22
export function ownsBotMessage ( message : Message , userId : string ) {
0 commit comments