Skip to content

Commit c60e1e1

Browse files
committed
Ranking
1 parent ac73b4a commit c60e1e1

File tree

7 files changed

+131
-48
lines changed

7 files changed

+131
-48
lines changed

β€Žsrc/app.tsβ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,23 @@ function isCommand(msg: Discord.Message) {
7171
return msg.content.startsWith(getConfig('PREFIX')) || KARMA_REGEX.test(msg.content);
7272
}
7373

74+
// const ROLE_MUTED_NAME = 'muted' as const;
75+
// const MAX_MENTIONS_PER_MESSAGE = 10;
76+
7477
client.on('message', async (msg) => {
7578
if (msg.author.bot) {
7679
return;
7780
}
7881

82+
// if ((msg.mentions.members?.size ?? 0) > MAX_MENTIONS_PER_MESSAGE) {
83+
// const roleManager = await msg.guild?.roles.fetch();
84+
// const roleMuted = roleManager?.cache.find((v) => v.name === ROLE_MUTED_NAME);
85+
// console.log(roleMuted);
86+
// // if (roleMuted) {
87+
// // msg.member?.roles.add(roleMuted);
88+
// // }
89+
// }
90+
7991
if (msg.content === `(β•―Β°β–‘Β°οΌ‰β•―οΈ΅ ┻━┻`) {
8092
return msg.channel.send(`β”¬β”€β”¬γƒŽ( β—•β—‘β—• γƒŽ)`);
8193
}

β€Žsrc/commands/index.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import mydevil from './mydevil';
1717
import npm from './npm';
1818
import odpowiedz from './odpowiedz';
1919
import prune from './prune';
20+
import ranking from './ranking';
2021
import quiz from './quiz';
2122
import regulamin from './regulamin';
2223
import roll from './roll';
@@ -45,6 +46,7 @@ const allCommands = [
4546
npm,
4647
odpowiedz,
4748
prune,
49+
ranking,
4850
quiz,
4951
regulamin,
5052
roll,

β€Žsrc/commands/karma.tsβ€Ž

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

4+
import { getEmojiForKarmaValue, getKarmaForMember } from '../data/karma';
55
import { getKarmaCollection, initDb } from '../db';
66
import type { Command } from '../types';
77
import { InvalidUsageError } from '../types';
@@ -51,7 +51,7 @@ const addKarma: Command = {
5151
const value = agg?.value ?? 0;
5252

5353
return msg.channel.send(
54-
`${msg.author.toString()} podziΔ™kowaΕ‚(a) ${member.toString()}! Karma ${member.toString()} wynosi ${value} ${getEmojiForValue(
54+
`${msg.author.toString()} podziΔ™kowaΕ‚(a) ${member.toString()}! Karma ${member.toString()} wynosi ${value} ${getEmojiForKarmaValue(
5555
value,
5656
)}`,
5757
);
@@ -75,47 +75,9 @@ const karma: Command = {
7575
const pkt = polishPlurals('punkt', 'punkty', 'punktΓ³w', value);
7676

7777
return msg.channel.send(
78-
`${member.displayName} ma ${value} ${pkt} karmy ${getEmojiForValue(value)}`,
78+
`${member.displayName} ma ${value} ${pkt} karmy ${getEmojiForKarmaValue(value)}`,
7979
);
8080
},
8181
};
8282

8383
export { addKarma, karma };
84-
85-
const getKarmaForMember = async (memberId: string, db: Db) => {
86-
const karmaCollection = getKarmaCollection(db);
87-
88-
type KarmaAgg = {
89-
readonly _id: string;
90-
readonly from: readonly string[];
91-
readonly value: number;
92-
};
93-
94-
const [agg] = await karmaCollection
95-
.aggregate<KarmaAgg | undefined>([
96-
{ $match: { to: memberId } },
97-
{ $group: { _id: '$to', from: { $push: '$from' }, value: { $sum: '$value' } } },
98-
])
99-
.toArray();
100-
return agg;
101-
};
102-
103-
const getEmojiForValue = (value: number) => {
104-
const adjustedValue = Math.floor(Math.sqrt(value + 1) - 1);
105-
const idx = Math.min(karmaEmojis.length, adjustedValue);
106-
return karmaEmojis[idx];
107-
};
108-
const karmaEmojis = [
109-
'πŸ‘‹',
110-
'πŸ‘',
111-
'πŸ‘Œ',
112-
'πŸ’ͺ',
113-
'πŸŽ–',
114-
'πŸ₯‰',
115-
'πŸ₯ˆ',
116-
'πŸ₯‡',
117-
'πŸ…',
118-
'πŸ™Œ',
119-
'πŸ₯°',
120-
'😍',
121-
] as const;

β€Žsrc/commands/ranking.tsβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { KarmaAgg } from '../data/karma';
2+
import { getEmojiForKarmaValue, getKarmaForMembers } from '../data/karma';
3+
import { initDb } from '../db';
4+
import type { Command } from '../types';
5+
6+
const ranking: Command = {
7+
name: 'ranking',
8+
description: 'Ranking karmy',
9+
args: false,
10+
async execute(msg) {
11+
const db = await initDb();
12+
const agg = await getKarmaForMembers(db);
13+
14+
const data = agg.filter((el): el is KarmaAgg => !!el);
15+
16+
await Promise.allSettled(data.map(({ _id: memberId }) => msg.guild?.members.fetch(memberId)));
17+
18+
const messages = [
19+
`**TOP 10 karma**`,
20+
...data.map(({ _id: memberId, value }, index) => {
21+
return `\`${(index + 1).toString().padStart(2, ' ')}\`. ${
22+
msg.guild?.members.cache.get(memberId)?.displayName ?? ''
23+
} – ${value} ${getEmojiForKarmaValue(value)}`;
24+
}),
25+
];
26+
27+
return msg.channel.send(messages.join('\n'));
28+
},
29+
};
30+
export default ranking;

β€Žsrc/commands/stats.tsβ€Ž

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,32 @@ const stats: Command = {
2626
const d1 = formatDate(getDateForWeekNumber(year2, week2 + 1));
2727
const d2 = formatDate(getDateForWeekNumber(year1, week1 + 1));
2828

29+
const ids = [
30+
...new Set([
31+
...totalStats.map(({ memberId }) => memberId),
32+
...statsChangeThisWeek.map(({ memberId }) => memberId),
33+
]),
34+
];
35+
36+
await Promise.allSettled(ids.map((memberId) => msg.guild?.members.fetch(memberId)));
37+
2938
return msg.channel.send(
3039
[
31-
format(`Najbardziej aktywne w tym tygodniu (${d1} – ${d2}):`, statsChangeThisWeek),
40+
format(
41+
`Najbardziej aktywne w tym tygodniu (${d1} – ${d2}):`,
42+
statsChangeThisWeek.map((data) => ({
43+
...data,
44+
displayName: msg.guild?.members.cache.get(data.memberId)?.displayName,
45+
})),
46+
),
3247
'\n',
33-
format('Najbardziej aktywne osoby od poczΔ…tku istnienia serwera:', totalStats),
48+
format(
49+
'Najbardziej aktywne osoby od poczΔ…tku istnienia serwera:',
50+
totalStats.map((data) => ({
51+
...data,
52+
displayName: msg.guild?.members.cache.get(data.memberId)?.displayName,
53+
})),
54+
),
3455
].join('\n'),
3556
);
3657
},
@@ -41,6 +62,7 @@ export default stats;
4162
type Stats = {
4263
readonly memberId: string;
4364
readonly messagesCount: number;
65+
readonly displayName?: string;
4466
};
4567

4668
function format(title: string, stats: readonly Stats[]) {
@@ -49,8 +71,10 @@ function format(title: string, stats: readonly Stats[]) {
4971
...stats
5072
.slice(0, 10)
5173
.map(
52-
({ messagesCount, memberId }, index) =>
53-
`\`${(index + 1).toString().padStart(2, ' ')}\`. <@${memberId}> – ${messagesCount ?? 0}`,
74+
({ messagesCount, displayName }, index) =>
75+
`\`${(index + 1).toString().padStart(2, ' ')}\`. ${displayName ?? ''} – ${
76+
messagesCount ?? 0
77+
}`,
5478
),
5579
];
5680
return messages.join('\n');
@@ -100,8 +124,8 @@ async function getStatsChangeThisWeek(db: Db) {
100124
.sort((a, b) => b.messagesCount - a.messagesCount);
101125

102126
return {
103-
statsChangeThisWeek,
104-
totalStats: totalStats as readonly Stats[],
127+
statsChangeThisWeek: statsChangeThisWeek.slice(0, 10),
128+
totalStats: totalStats.slice(0, 10) as readonly Stats[],
105129
year1,
106130
year2,
107131
week1,

β€Žsrc/data/karma.tsβ€Ž

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Db } from 'mongodb';
2+
3+
import { getKarmaCollection } from '../db';
4+
5+
export type KarmaAgg = {
6+
readonly _id: string;
7+
readonly from: readonly string[];
8+
readonly value: number;
9+
};
10+
11+
const karmaAggregateGroup = {
12+
$group: { _id: '$to', from: { $push: '$from' }, value: { $sum: '$value' } },
13+
} as const;
14+
15+
export const getKarmaForMember = async (memberId: string, db: Db) => {
16+
const karmaCollection = getKarmaCollection(db);
17+
18+
const [agg] = await karmaCollection
19+
.aggregate<KarmaAgg | undefined>([{ $match: { to: memberId } }, karmaAggregateGroup])
20+
.toArray();
21+
return agg;
22+
};
23+
24+
export const getKarmaForMembers = async (db: Db) => {
25+
const karmaCollection = getKarmaCollection(db);
26+
27+
const agg = await karmaCollection
28+
.aggregate<KarmaAgg | undefined>([karmaAggregateGroup])
29+
.sort({ value: -1 })
30+
.limit(10)
31+
.toArray();
32+
return agg;
33+
};
34+
35+
export const getEmojiForKarmaValue = (value: number) => {
36+
const adjustedValue = Math.floor(Math.sqrt(value + 1) - 1);
37+
const idx = Math.min(karmaEmojis.length, adjustedValue);
38+
return karmaEmojis[idx];
39+
};
40+
const karmaEmojis = [
41+
'πŸ‘‹',
42+
'πŸ‘',
43+
'πŸ‘Œ',
44+
'πŸ’ͺ',
45+
'πŸŽ–',
46+
'πŸ₯‰',
47+
'πŸ₯ˆ',
48+
'πŸ₯‡',
49+
'πŸ…',
50+
'πŸ™Œ',
51+
'πŸ₯°',
52+
'😍',
53+
] as const;

β€Žtsconfig.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"emitDecoratorMetadata": true,
66
"esModuleInterop": true,
77
"experimentalDecorators": true,
8-
"lib": ["es2019"],
8+
"lib": ["es2020"],
99
"module": "commonjs",
1010
"noFallthroughCasesInSwitch": true,
1111
"noImplicitReturns": true,

0 commit comments

Comments
Β (0)