-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrep.ts
More file actions
182 lines (153 loc) · 4.45 KB
/
rep.ts
File metadata and controls
182 lines (153 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
command,
default as CookiecordClient,
Module,
optional,
listener,
} from 'cookiecord';
import { GuildMember, Message, MessageEmbed, User } from 'discord.js';
import prettyMilliseconds from 'pretty-ms';
import { TS_BLUE } from '../env';
import { RepGive } from '../entities/RepGive';
import { RepUser } from '../entities/RepUser';
import { cannotSendRepToYou, noRepRemain, okHand } from './msg';
export class RepModule extends Module {
constructor(client: CookiecordClient) {
super(client);
}
MAX_REP = 3;
// all messages have to be fully lowercase
THANKS_REGEX = /(?:thanks|thx|cheers|thanx|ty|tks|tkx)\b/i;
async getOrMakeUser(user: User) {
let ru = await RepUser.findOne(
{ id: user.id },
{ relations: ['got', 'given'] },
);
if (!ru) {
ru = await RepUser.create({ id: user.id }).save();
}
return ru;
}
@listener({ event: 'message' })
async onThank(msg: Message) {
const GIVE = '✅';
const PARTIAL_GIVE = '🤔';
const NO_GIVE = '❌';
// Check for thanks messages
const isThanks = this.THANKS_REGEX.test(msg.content);
if (msg.author.bot || !isThanks || !msg.guild) return;
const mentionUsers = msg.mentions.users.array();
if (!mentionUsers.length) return;
const senderRU = await this.getOrMakeUser(msg.author);
// track how much rep the author has sent
// 3 possible outcomes: NO_GIVE, PARTIAL_GIVE and GAVE
let currentSent = await senderRU.sent();
if (currentSent >= this.MAX_REP) return await msg.react(NO_GIVE);
for (const user of mentionUsers) {
if (user.id === msg.member?.id) continue;
if (currentSent >= this.MAX_REP)
return await msg.react(PARTIAL_GIVE);
// give rep
const targetRU = await this.getOrMakeUser(user);
await RepGive.create({
from: senderRU,
to: targetRU,
}).save();
currentSent++;
}
await msg.react(GIVE);
}
@command({
description: 'See how many reputation points you have left to send',
})
async remaining(msg: Message) {
const USED = '✅';
const UNUSED = '⬜';
const ru = await this.getOrMakeUser(msg.author);
const sent = await ru.sent();
await msg.channel.send(
`Rep used: ${
USED.repeat(sent) + UNUSED.repeat(this.MAX_REP - sent)
}`,
);
}
@command({ description: 'Give a different user some reputation points' })
async rep(msg: Message, targetMember: GuildMember) {
if (targetMember.id === msg.member?.id)
return msg.channel.send(cannotSendRepToYou);
const senderRU = await this.getOrMakeUser(msg.author);
const targetRU = await this.getOrMakeUser(targetMember.user);
if ((await senderRU.sent()) >= this.MAX_REP)
return await msg.channel.send(noRepRemain);
await RepGive.create({
from: senderRU,
to: targetRU,
}).save();
await msg.channel.send(
`${okHand} sent \`${targetMember.displayName}\` 1 rep (${
(await senderRU.sent()) + 1
}/${this.MAX_REP} sent)`,
);
}
@command({
aliases: ['history'],
description: "View a user's reputation history",
})
async getrep(msg: Message, @optional user?: User) {
if (!user) user = msg.author;
const targetRU = await this.getOrMakeUser(user);
const embed = new MessageEmbed()
.setColor(TS_BLUE)
.setAuthor(user.tag, user.displayAvatarURL())
.setDescription(
(
await Promise.all(
(await targetRU.got)
.concat(await targetRU.given)
.map(async rg => {
if (rg.from.id == targetRU.id)
return `:white_small_square: Gave 1 rep to <@${
rg.to.id
}> (${prettyMilliseconds(
Date.now() - rg.createdAt.getTime(),
)} ago)`;
else
return `:white_small_square: Got 1 rep from <@${
rg.from.id
}> (${prettyMilliseconds(
Date.now() - rg.createdAt.getTime(),
)} ago)`;
}),
)
).join('\n'),
);
await msg.channel.send(embed);
}
@command({
aliases: ['lb'],
description: 'See who has the most reputation',
})
async leaderboard(msg: Message) {
const data = ((await RepGive.createQueryBuilder('give')
.select(['give.to', 'COUNT(*)'])
.groupBy('give.to')
.orderBy('COUNT(*)', 'DESC')
.limit(10)
.getRawMany()) as { toId: string; count: string }[]).map(x => ({
id: x.toId,
count: parseInt(x.count, 10),
}));
const embed = new MessageEmbed()
.setColor(TS_BLUE)
.setTitle('Top 10 Reputation')
.setDescription(
data
.map(
x =>
`:white_small_square: **<@${x.id}>** with **${x.count}** points.`,
)
.join('\n'),
);
await msg.channel.send(embed);
}
}