-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoRole.ts
199 lines (183 loc) · 5.3 KB
/
autoRole.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as app from "#app"
import autoRole from "#tables/autoRole.ts"
export default new app.Command({
name: "autoRole",
aliases: ["ar", "autorole"],
description: "Manage the auto roles",
channelType: "all",
async run(message) {
return app.sendCommandDetails(message, this)
},
subs: [
new app.Command({
name: "set",
description: "Set auto role list",
channelType: "guild",
middlewares: [app.staffOnly],
positional: [
{
name: "roles",
type: "array",
required: true,
description: "Role list",
},
],
flags: [
{
name: "bot",
aliases: ["for-bot", "bot-only"],
description: "Is auto roles for bots",
flag: "b",
},
],
async run(message) {
const guild = await app.getGuild(message.guild, { forceExists: true })
await autoRole.query.delete().where("guild_id", guild._id)
await autoRole.query.insert(
message.args.roles.map((roleId: string) => {
return {
guild_id: guild._id,
role_id: roleId,
bot: message.args.bot,
}
}),
)
return message.channel.send(
`${app.emote(message, "CheckMark")} Auto-roles are successfully pushed.`,
)
},
}),
new app.Command({
name: "add",
description: "Add auto role",
channelType: "guild",
middlewares: [app.staffOnly],
positional: [
{
name: "role",
type: "role",
required: true,
description: "Role to add",
},
],
flags: [
{
name: "bot",
aliases: ["for-bot", "bot-only"],
description: "Is bot auto-role",
flag: "b",
},
],
async run(message) {
const guild = await app.getGuild(message.guild, { forceExists: true })
await autoRole.query.insert({
guild_id: guild._id,
role_id: message.args.role.id,
bot: message.args.bot,
})
return message.channel.send(
`${app.emote(message, "CheckMark")} Auto-role is successfully pushed.`,
)
},
}),
new app.Command({
name: "list",
aliases: ["ls"],
description: "List auto roles",
channelType: "guild",
async run(message) {
const guild = await app.getGuild(message.guild, { forceExists: true })
const autoRoles = await autoRole.query.where("guild_id", guild._id)
return message.channel.send({
embeds: [
new app.EmbedBuilder()
.setColor("Blurple")
.setTitle("Auto-role list")
.addFields([
{
name: "Member auto roles",
value:
autoRoles
.filter((ar) => !ar.bot)
.map((ar) => `<@&${ar.role_id}>`)
.join(" ") || "No role setup here.",
},
{
name: "Bot auto roles",
value:
autoRoles
.filter((ar) => !!ar.bot)
.map((ar) => `<@&${ar.role_id}>`)
.join(" ") || "No role setup here.",
},
]),
],
})
},
}),
new app.Command({
name: "apply",
description: "Apply auto-roles to member",
middlewares: [app.staffOnly],
channelType: "guild",
positional: [
{
name: "target",
description: "Target member",
required: true,
type: "member",
},
],
async run(message) {
const target: app.GuildMember = message.args.target
await app.applyAutoRoles(target)
return message.channel.send(
`${app.emote(
message,
"CheckMark",
)} Auto-roles are successfully applied to **${
target.user.username
}**.`,
)
},
subs: [
new app.Command({
name: "all",
description: "Apply auto-roles to all guild members",
aliases: ["*"],
channelType: "guild",
async run(message) {
const waiting = await message.channel.send(
`${app.emote(message, "Loading")} Fetching members...`,
)
const members = Array.from(
(await message.guild.members.fetch()).values(),
)
message.guild.members.cache.clear()
await waiting.edit(
`${app.emote(message, "Loading")} Applying auto-roles to members...`,
)
for (const member of members) {
await app.applyAutoRoles(member)
const index = members.indexOf(member)
await app.sendProgress(
waiting,
index,
members.length,
"Applying auto-roles to members... (`$%` %)",
)
}
return message.channel.send(
`${app.emote(
message,
"CheckMark",
)} Auto-roles are successfully applied to **${
members.length
}** members.`,
)
},
}),
],
}),
],
})