Skip to content

Commit 054ff8a

Browse files
committed
Fix thx
1 parent 33cb9ac commit 054ff8a

File tree

17 files changed

+116
-54
lines changed

17 files changed

+116
-54
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"eslint.run": "onSave",
77
"editor.codeActionsOnSave": {
88
// "source.fixAll": true
9-
}
9+
},
10+
"typescript.preferences.importModuleSpecifier": "relative"
1011
}

src/app.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import { InvalidUsageError } from './types';
1111
import { getWeekNumber } from './utils';
1212
import { getStatsCollection, initDb } from './db';
1313
import { messageToReflinks } from './commands/reflink';
14-
import { updateKarmaRoles } from './cron/karma';
15-
import { updateStatsRoles } from './cron/stats';
14+
import { updateKarmaRoles } from './cron/roles/karma';
15+
import { updateStatsRoles } from './cron/roles/stats';
16+
import { thx } from './thx';
1617

1718
const MESSAGE_COLLECTOR_CACHE_S = 60 * 60;
1819
const messageCollectorCache = new Cache({ stdTTL: MESSAGE_COLLECTOR_CACHE_S });
1920

20-
const THX_TIMEOUT_S = 15 * 60;
21-
const thxTimeoutCache = new Cache({ stdTTL: THX_TIMEOUT_S });
22-
2321
const client = new Discord.Client();
2422

2523
const settings: { readonly setPresence: boolean; readonly config: ClientConfig } = {
@@ -125,15 +123,7 @@ client.on('message', async (msg) => {
125123
return msg.reply(maybeReflinks);
126124
}
127125

128-
if (/thx|thank|dzięki|dziękuję|dzieki|dziekuje/i.test(msg.content)) {
129-
if (
130-
(thxTimeoutCache.get<Date>(msg.channel.id)?.getTime() ?? 0) <
131-
Date.now() - THX_TIMEOUT_S * 1000
132-
) {
133-
thxTimeoutCache.set(msg.channel.id, new Date());
134-
return msg.reply('protip: napisz `@nazwa ++`, żeby komuś podziękować!');
135-
}
136-
}
126+
await thx(msg);
137127

138128
return;
139129
});
@@ -216,13 +206,14 @@ init().catch((err) => errors.push(err));
216206

217207
const httpServer = createHttpServer(client, errors, warnings, debugs);
218208

219-
const updateRoles = (guild: Discord.Guild) => {
220-
updateKarmaRoles(guild)
221-
.then(() => console.log(`Successfully updated roles`))
222-
.catch((err) => errors.push(err));
223-
updateStatsRoles(guild)
224-
.then(() => console.log(`Successfully updated roles`))
225-
.catch((err) => errors.push(err));
209+
const updateRoles = async (guild: Discord.Guild) => {
210+
try {
211+
await updateStatsRoles(guild);
212+
await updateKarmaRoles(guild);
213+
console.log(`Updated roles`);
214+
} catch (err) {
215+
errors.push(err);
216+
}
226217
};
227218

228219
httpServer.listen(getConfig('PORT'), () => {

src/commands/co.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import co from './co';
65
import { getMessageMock } from '../../test/mocks';
@@ -11,7 +10,7 @@ describe('co', () => {
1110
it('it should send a file', async () => {
1211
const msg = getMessageMock('msg');
1312

14-
await co.execute((msg as unknown) as Discord.Message, []);
13+
await co.execute(msg as unknown as Discord.Message, []);
1514

1615
await expect(msg.channel.send).to.have.been.calledOnce;
1716
});

src/commands/execute.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import { expect } from 'chai';
65
import 'mocha';

src/commands/prune.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import prune from './prune';
65
import { getMessageMock } from '../../test/mocks';
@@ -13,15 +12,15 @@ describe('prune', () => {
1312
const msg = getMessageMock('msg');
1413

1514
return expect(
16-
prune.execute((msg as unknown) as Discord.Message, ['0']),
15+
prune.execute(msg as unknown as Discord.Message, ['0']),
1716
).to.be.eventually.rejectedWith('Musisz podać przynajmniej 1.');
1817
});
1918

2019
it('should show an error when you try to delete more than 10 messages', async () => {
2120
const msg = getMessageMock('msg');
2221

2322
return expect(
24-
prune.execute((msg as unknown) as Discord.Message, ['11']),
23+
prune.execute(msg as unknown as Discord.Message, ['11']),
2524
).to.be.eventually.rejectedWith(
2625
'Ze względów bezpieczeństwa, możesz usunąć tylko 10 wiadomości na raz.',
2726
);
@@ -31,7 +30,7 @@ describe('prune', () => {
3130
const msg = getMessageMock('msg');
3231

3332
return expect(
34-
prune.execute((msg as unknown) as Discord.Message, ['adsads']),
33+
prune.execute(msg as unknown as Discord.Message, ['adsads']),
3534
).to.be.eventually.rejectedWith('Parametr musi być liczbą wiadomości.');
3635
});
3736

@@ -45,7 +44,7 @@ describe('prune', () => {
4544
msg.channel.messages.fetch.resolves(messagesCollectionMock);
4645
msg.guild.member.returns(memberMock);
4746

48-
await expect(prune.execute((msg as unknown) as Discord.Message, ['2'])).to.be.fulfilled;
47+
await expect(prune.execute(msg as unknown as Discord.Message, ['2'])).to.be.fulfilled;
4948
await expect(msg.channel.messages.fetch).to.have.been.calledOnceWithExactly({ limit: 2 });
5049
await expect(msg.channel.bulkDelete).to.have.been.calledOnceWithExactly(messagesCollectionMock);
5150
});
@@ -56,7 +55,7 @@ describe('prune', () => {
5655
const messagesCollectionMock = { clear: Sinon.spy() } as any;
5756
msg.channel.messages.fetch.resolves(messagesCollectionMock);
5857

59-
await prune.execute((msg as unknown) as Discord.Message, ['2']);
58+
await prune.execute(msg as unknown as Discord.Message, ['2']);
6059

6160
await expect(msg.delete).to.have.been.calledOnce;
6261
});

src/commands/reflink.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import { expect } from 'chai';
65
import { helionReflink, xKomReflink } from './reflink';

src/commands/roll.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43
import { expect } from 'chai';
54
import 'mocha';
65

@@ -53,12 +52,12 @@ describe('roll', () => {
5352
describe('handleCommand', () => {
5453
it('should reply', async () => {
5554
const msg = getMessageMock('msg');
56-
await roll.execute((msg as unknown) as Discord.Message, ['4d6']);
55+
await roll.execute(msg as unknown as Discord.Message, ['4d6']);
5756
await expect(msg.channel.send).to.have.been.calledOnce;
5857
});
5958
it('reply instruction on fail', async () => {
6059
const msg = getMessageMock('msg');
61-
await roll.execute((msg as unknown) as Discord.Message, ['3k5']);
60+
await roll.execute(msg as unknown as Discord.Message, ['3k5']);
6261
await expect(msg.channel.send).to.have.been.calledOnceWith(instruction);
6362
});
6463
});

src/commands/wiki.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import wiki from './wiki';
65
import { getMessageMock } from '../../test/mocks';
@@ -16,7 +15,7 @@ describe('wiki', () => {
1615

1716
const msg = getMessageMock('msg');
1817

19-
await wiki.execute((msg as unknown) as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
18+
await wiki.execute(msg as unknown as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
2019

2120
await expect(msg.channel.send).to.have.been.calledOnce.and.calledWithMatch('Nic nie znalazłam');
2221
});
@@ -33,7 +32,7 @@ describe('wiki', () => {
3332

3433
const msg = getMessageMock('msg');
3534

36-
await wiki.execute((msg as unknown) as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
35+
await wiki.execute(msg as unknown as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
3736

3837
await expect(msg.channel.send).to.have.been.calledOnceWith(
3938
'Pod hasłem: moja ulubiona piosenka\nZnalazłam artykuł: Moja ulubiona piosenka\nDostępny tutaj: https://pl.wikipedia.org/wiki/Moja_ulubiona_piosenka',

src/commands/youtube.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint no-implicit-dependencies: "off" */
22
/* eslint no-magic-numbers: "off" */
3-
/* tslint:disable:no-implicit-dependencies no-magic-numbers */
43

54
import youtube from './youtube';
65
import { getMessageMock } from '../../test/mocks';
@@ -29,7 +28,7 @@ describe('youtube', () => {
2928

3029
const msg = getMessageMock('msg');
3130

32-
await youtube.execute((msg as unknown) as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
31+
await youtube.execute(msg as unknown as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
3332

3433
await expect(msg.channel.send).to.have.been.calledOnce.and.calledWithMatch('Niestety nic');
3534
});
@@ -43,7 +42,7 @@ describe('youtube', () => {
4342

4443
const msg = getMessageMock('msg');
4544

46-
await youtube.execute((msg as unknown) as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
45+
await youtube.execute(msg as unknown as Discord.Message, ['moja', 'ulubiona', 'piosenka']);
4746

4847
await expect(msg.channel.send).to.have.been.calledOnceWith(
4948
'https://www.youtube.com/watch?v=aaa123',
File renamed without changes.

0 commit comments

Comments
 (0)