11import Discord from 'discord.js' ;
22import MonitoRSS from 'monitorss' ;
3+ import type { ClientConfig } from 'monitorss' ;
34import Cache from 'node-cache' ;
45
56import { handleCommand } from './commands' ;
@@ -8,12 +9,15 @@ import { getConfig } from './config';
89import { createHttpServer } from './http-server' ;
910import { InvalidUsageError } from './types' ;
1011
11- const ONE_HOUR_S = 3600 ;
12- const cache = new Cache ( { stdTTL : ONE_HOUR_S } ) ;
12+ const MESSAGE_COLLECTOR_CACHE_S = 60 * 60 ;
13+ const messageCollectorCache = new Cache ( { stdTTL : MESSAGE_COLLECTOR_CACHE_S } ) ;
14+
15+ const THX_TIMEOUT_S = 15 * 60 ;
16+ const thxTimeoutCache = new Cache ( { stdTTL : THX_TIMEOUT_S } ) ;
1317
1418const client = new Discord . Client ( ) ;
1519
16- const settings = {
20+ const settings : { readonly setPresence : boolean ; readonly config : ClientConfig } = {
1721 setPresence : true ,
1822 config : {
1923 bot : {
@@ -67,8 +71,6 @@ function isCommand(msg: Discord.Message) {
6771 return msg . content . startsWith ( getConfig ( 'PREFIX' ) ) || KARMA_REGEX . test ( msg . content ) ;
6872}
6973
70- const thxTimeoutRef = { ref : Date . now ( ) } ;
71-
7274client . on ( 'message' , async ( msg ) => {
7375 if ( msg . author . bot ) {
7476 return ;
@@ -85,7 +87,7 @@ client.on('message', async (msg) => {
8587 ) ;
8688 await handleCommand ( msg ) ;
8789 const ids = collector . collected . map ( ( m ) => m . id ) ;
88- cache . set ( msg . id , ids ) ;
90+ messageCollectorCache . set ( msg . id , ids ) ;
8991 collector . stop ( ) ;
9092 } catch ( err ) {
9193 if ( err instanceof InvalidUsageError ) {
@@ -99,9 +101,12 @@ client.on('message', async (msg) => {
99101 }
100102 }
101103
102- if ( / t h x | t h a n k s | d z i ę k i | d z i ę k u j ę | d z i e k i | d z i e k u j e / i. test ( msg . content ) ) {
103- if ( Date . now ( ) > thxTimeoutRef . ref ) {
104- thxTimeoutRef . ref = Date . now ( ) + 15 * 60 * 1000 ;
104+ if ( / t h x | t h a n k | d z i ę k i | d z i ę k u j ę | d z i e k i | d z i e k u j e / i. test ( msg . content ) ) {
105+ if (
106+ ( thxTimeoutCache . get < Date > ( msg . channel . id ) ?. getTime ( ) ?? 0 ) <
107+ Date . now ( ) - THX_TIMEOUT_S * 1000
108+ ) {
109+ thxTimeoutCache . set ( msg . channel . id , new Date ( ) ) ;
105110 return msg . reply ( 'protip: napisz `@nazwa ++`, żeby komuś podziękować!' ) ;
106111 }
107112 }
@@ -110,11 +115,11 @@ client.on('message', async (msg) => {
110115} ) ;
111116
112117function revertCommand ( msg : Discord . Message ) {
113- if ( ! cache . has ( msg . id ) || msg . channel . type === 'dm' ) {
118+ if ( ! messageCollectorCache . has ( msg . id ) || msg . channel . type === 'dm' ) {
114119 return undefined ;
115120 }
116121 // eslint-disable-next-line functional/prefer-readonly-type
117- const messagesToDelete = cache . get < string [ ] > ( msg . id ) ! ;
122+ const messagesToDelete = messageCollectorCache . get < string [ ] > ( msg . id ) ! ;
118123 return msg . channel . bulkDelete ( messagesToDelete ) ;
119124}
120125
@@ -139,7 +144,8 @@ client.on('messageDelete', async (msg) => {
139144async function init ( ) {
140145 await client . login ( getConfig ( 'DISCORD_BOT_TOKEN' ) ) ;
141146 const rssClient = new MonitoRSS . ClientManager ( settings ) ;
142- rssClient . start ( ) ;
147+ await new Promise ( ( resolve ) => rssClient . start ( ( ) => resolve ( undefined ) ) ) ;
148+ console . log ( 'MonitoRSS started!' ) ;
143149}
144150
145151init ( ) . catch ( ( err ) => errors . push ( err ) ) ;
0 commit comments