-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rating.ts
98 lines (89 loc) · 2.63 KB
/
rating.ts
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
import * as app from "#app"
import rating from "#tables/rating.ts"
export default new app.Command({
name: "rating",
aliases: ["note", "rate"],
description: "Rate a user or a bot",
channelType: "guild",
positional: [
app.positional({
name: "member",
description: "The rated member",
type: "member",
validate: (value, message) => {
return (
(value !== message.member && !!value) || "You can't target yourself."
)
},
}),
app.positional({
name: "rating",
description: "Rating from 0 to 5",
type: "number",
validate: (rating) =>
rating >= 0 && rating <= 5 && Number.isInteger(rating),
}),
],
async run(message) {
if (message.args.member) {
if (message.args.rating !== null) {
const value = message.args.rating as 0 | 1 | 2 | 3 | 4 | 5
const fromUser = await app.getUser(message.author, true)
const toUser = await app.getUser(message.args.member, true)
const guild = await app.getGuild(message.guild, { forceExists: true })
const pack = {
guild_id: guild._id,
from_id: fromUser._id,
to_id: toUser._id,
}
if (await rating.query.where(pack).first()) {
await rating.query.update({ value }).where(pack)
} else {
await rating.query.insert({ value, ...pack })
}
return message.channel.send(
`${app.emote(message, "CheckMark")} Successfully rated.`,
)
}
return message.channel.send({
embeds: [await app.ratingEmbed(message.args.member)],
})
}
return message.channel.send({
embeds: [await app.ratingEmbed(message.member)],
})
},
subs: [
new app.Command({
name: "leaderboard",
description: `Show the leaderboard of Rating`,
channelType: "guild",
aliases: ["ladder", "lb", "top", "rank"],
options: [
app.option({
name: "lines",
description: "Number of lines to show per page",
type: "number",
default: 15,
aliases: ["line", "count"],
validate: (value) => value > 0 && value <= 50,
}),
],
flags: [
app.flag({
name: "global",
flag: "g",
description: "Show the global leaderboard of Rating",
}),
],
run: async (message) => {
const guild = message.args.global
? undefined
: await app.getGuild(message.guild, { forceExists: true })
app.ratingLadder(guild?._id).send(message.channel, {
pageLineCount: message.args.lines,
})
},
}),
],
})