Skip to content

Commit 4bc3d83

Browse files
committed
feat: Allow giving karma to multiple people
1 parent df05d3a commit 4bc3d83

40 files changed

+99
-89
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
"rules": {
5151
"import/no-default-export": 0
5252
}
53+
},
54+
{
55+
"files": ["**/*.spec.ts"],
56+
"rules": {
57+
"no-magic-numbers": 0,
58+
"no-implicit-dependencies": 0
59+
}
5360
}
5461
]
5562
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"author": "Michał Miszczyszyn <[email protected]> (https://typeofweb.com/)",
2424
"license": "ISC",
2525
"dependencies": {
26+
"@types/bluebird": "3.5.36",
2627
"algoliasearch": "4.12.0",
28+
"bluebird": "3.7.2",
2729
"bufferutil": "4.0.3",
2830
"discord.js": "12.5.3",
2931
"dotenv": "10.0.0",

src/commands/co.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* eslint no-implicit-dependencies: "off" */
2-
/* eslint no-magic-numbers: "off" */
3-
41
import co from './co';
52
import { getMessageMock } from '../../test/mocks';
63
import { expect } from 'chai';

src/commands/co.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Command } from '../types';
33
const co: Command = {
44
name: 'co',
55
description: 'co?',
6-
args: false,
6+
args: 'prohibited',
77
cooldown: 60,
88
execute(msg) {
99
return msg.channel.send({

src/commands/execute.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* eslint no-implicit-dependencies: "off" */
2-
/* eslint no-magic-numbers: "off" */
3-
41
import { expect } from 'chai';
52
import 'mocha';
63
import * as execute from './execute';

src/commands/execute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const errorMessage = (error: Error | string) =>
174174
const execute: Command = {
175175
name: 'execute',
176176
description: 'Wykonuje kod JS/TS',
177-
args: false,
177+
args: 'prohibited',
178178
cooldown: COOLDOWN,
179179
async execute(msg: Message) {
180180
try {

src/commands/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,17 @@ function processCommand(msg: Discord.Message, command: Command, rest: string | n
173173
}
174174

175175
verifyCooldown(msg, command);
176+
const args = rest ? rest.split(/\s+/g) : [];
176177

177-
if (command.args === false) {
178-
return command.execute(msg);
178+
if (command.args === 'optional') {
179+
return command.execute(msg, args);
179180
}
180-
181-
const args = rest ? rest.split(/\s+/g) : [];
182-
if (!args.length && command.args === true) {
181+
if (!args.length && command.args === 'required') {
183182
throw new InvalidUsageError(`nie podano argumentów!`);
184183
}
184+
if (args.length && command.args === 'prohibited') {
185+
throw new InvalidUsageError(`argumenty niedozwolone!`);
186+
}
185187

186188
return command.execute(msg, args);
187189
}

src/commands/karma.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,75 @@
11
import Discord from 'discord.js';
22
import { polishPlurals } from 'polish-plurals';
3+
import Bluebird from 'bluebird';
34

45
import {
56
getEmojiForKarmaValue,
67
getKarmaForMember,
7-
getKarmaForMembers,
8+
getKarmaForAllMembers,
89
KarmaAgg,
10+
getKarmaForMembers,
911
} from '../data/karma';
1012
import { getKarmaCollection, initDb } from '../db';
1113
import type { Command } from '../types';
1214

1315
export const KARMA_REGEX = new RegExp(
14-
`^${Discord.MessageMentions.USERS_PATTERN.source}\\s*\\+\\+\\s*(?<description>.*)$`,
16+
`^(${Discord.MessageMentions.USERS_PATTERN.source}\\s*)+\\+\\+\\s*$`,
1517
);
1618

1719
const addKarma: Command = {
1820
name: '++',
1921
description: 'Podziękuj użytkownikom wpisując `@nazwa ++`',
20-
args: false,
21-
cooldown: 10,
22+
args: 'optional',
23+
cooldown: 5,
2224
async execute(msg) {
23-
const member = await msg.mentions.members?.first()?.fetch();
24-
if (!member) {
25+
if (!msg.mentions.members?.size) {
2526
return null;
2627
}
2728

29+
const db = await initDb();
30+
const karmaCollection = getKarmaCollection(db);
31+
2832
const from = msg.author.id;
29-
const to = member.id;
3033

31-
if (from === to) {
34+
const membersToReward = await Bluebird.resolve(Array.from(msg.mentions.members.values()))
35+
.map((m) => m.fetch())
36+
.filter((m) => m && m.id !== from);
37+
38+
if (membersToReward.length === 0) {
3239
return null;
3340
}
3441

35-
const db = await initDb();
36-
const karmaCollection = getKarmaCollection(db);
37-
38-
await karmaCollection.insertOne({
39-
from,
40-
to,
41-
createdAt: new Date(),
42-
value: 1,
43-
});
42+
await karmaCollection.insertMany(
43+
membersToReward.map((m) => {
44+
return {
45+
from,
46+
to: m.id,
47+
createdAt: new Date(),
48+
value: 1,
49+
};
50+
}),
51+
);
4452

45-
const agg = await getKarmaForMember(to, db);
46-
const value = agg?.value ?? 0;
53+
const membersKarma = await getKarmaForMembers(
54+
db,
55+
membersToReward.map((m) => m.id),
56+
);
4757

48-
return msg.channel.send(
49-
`${msg.author.toString()} podziękował(a) ${member.toString()}! Karma ${member.toString()} wynosi ${value.toFixed(
58+
const messages = membersKarma.map(({ value, _id }) => {
59+
const member = membersToReward.find((m) => m.id === _id);
60+
return `${msg.author.toString()} podziękował(a) ${member?.toString()}! Karma ${member?.toString()} wynosi ${value.toFixed(
5061
2,
51-
)} ${getEmojiForKarmaValue(value)}`,
52-
);
62+
)} ${getEmojiForKarmaValue(value)}`;
63+
});
64+
65+
return msg.channel.send(messages.join('\n'));
5366
},
5467
};
5568

5669
const karma: Command = {
5770
name: 'karma',
5871
description: 'Sprawdź ile kto ma pkt. karmy.',
59-
args: false,
72+
args: 'optional',
6073
async execute(msg) {
6174
const member = await msg.mentions.members?.first()?.fetch();
6275

@@ -72,7 +85,7 @@ const karma: Command = {
7285
`${member.displayName} ma ${value.toFixed(2)} ${pkt} karmy ${getEmojiForKarmaValue(value)}`,
7386
);
7487
} else {
75-
const agg = await getKarmaForMembers(db);
88+
const agg = await getKarmaForAllMembers(db);
7689
const data = agg.filter((el): el is KarmaAgg => !!el);
7790
await Promise.allSettled(data.map(({ _id: memberId }) => msg.guild?.members.fetch(memberId)));
7891

src/commands/link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Command } from '../types';
55
const link: Command = {
66
name: 'link',
77
description: 'Wyświetla link do zapraszania.',
8-
args: false,
8+
args: 'prohibited',
99
execute(msg: Discord.Message) {
1010
return msg.channel.send(`Link do zapraszania: https://discord.typeofweb.com/`);
1111
},

src/commands/m1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Command } from '../types';
33
const m1: Command = {
44
name: 'm1',
55
description: 'Apple silicon m1',
6-
args: false,
6+
args: 'prohibited',
77
cooldown: 10,
88
execute(msg) {
99
return msg.channel.send([

0 commit comments

Comments
 (0)