Skip to content

Commit ac73b4a

Browse files
committed
Fixes to karma
1 parent b669eae commit ac73b4a

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/app.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Discord from 'discord.js';
22
import MonitoRSS from 'monitorss';
3+
import type { ClientConfig } from 'monitorss';
34
import Cache from 'node-cache';
45

56
import { handleCommand } from './commands';
@@ -8,12 +9,15 @@ import { getConfig } from './config';
89
import { createHttpServer } from './http-server';
910
import { 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

1418
const 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-
7274
client.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 (/thx|thanks|dzięki|dziękuję|dzieki|dziekuje/i.test(msg.content)) {
103-
if (Date.now() > thxTimeoutRef.ref) {
104-
thxTimeoutRef.ref = Date.now() + 15 * 60 * 1000;
104+
if (/thx|thank|dzięki|dziękuję|dzieki|dziekuje/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

112117
function 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) => {
139144
async 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

145151
init().catch((err) => errors.push(err));

src/commands/karma.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Discord from 'discord.js';
22
import type { Db } from 'mongodb';
3+
import { polishPlurals } from 'polish-plurals';
34

45
import { getKarmaCollection, initDb } from '../db';
56
import type { Command } from '../types';
@@ -50,7 +51,7 @@ const addKarma: Command = {
5051
const value = agg?.value ?? 0;
5152

5253
return msg.channel.send(
53-
`${msg.author.toString()} podziękował ${member.toString()}! Karma ${member.toString()} wynosi ${value} ${getEmojiForValue(
54+
`${msg.author.toString()} podziękował(a) ${member.toString()}! Karma ${member.toString()} wynosi ${value} ${getEmojiForValue(
5455
value,
5556
)}`,
5657
);
@@ -71,8 +72,10 @@ const karma: Command = {
7172
const agg = await getKarmaForMember(member.id, db);
7273
const value = agg?.value ?? 0;
7374

75+
const pkt = polishPlurals('punkt', 'punkty', 'punktów', value);
76+
7477
return msg.channel.send(
75-
`${member.displayName} ma ${value} punktów karmy ${getEmojiForValue(value)}`,
78+
`${member.displayName} ma ${value} ${pkt} karmy ${getEmojiForValue(value)}`,
7679
);
7780
},
7881
};

0 commit comments

Comments
 (0)