-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
237 lines (212 loc) · 5.94 KB
/
commands.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { discordRequest } from "./utils.js"
export async function hasGuildCommands(appId, guildId, commands) {
if (guildId === "" || appId === "") return
commands.forEach((c) => hasGuildCommand(appId, guildId, c))
}
// Checks for a command
async function hasGuildCommand(appId, guildId, command) {
// API endpoint to get and post guild commands
const endpoint = `applications/${appId}/guilds/${guildId}/commands`
try {
const res = await discordRequest(endpoint, { method: "GET" })
const data = await res.json()
if (data) {
const installedNames = data.map((c) => c["name"])
// This is just matching on the name, so it's not good for updates
if (!installedNames.includes(command["name"])) {
console.log(`Installing "${command["name"]}"`)
installGuildCommand(appId, guildId, command)
} else {
console.log(`"${command["name"]}" command already installed`)
}
}
} catch (err) {
console.error(err)
}
}
// Installs a command
export async function installGuildCommand(appId, guildId, command) {
// API endpoint to get and post guild commands
const endpoint = `applications/${appId}/guilds/${guildId}/commands`
// install command
try {
await discordRequest(endpoint, { method: "POST", body: command })
} catch (err) {
console.error(err)
}
}
//Get all commands
export async function getCommands(appId, guildId) {
// API endpoint to retrieve all commands
const endpoint = `applications/${appId}/guilds/${guildId}/commands`
try {
const res = await discordRequest(endpoint, { method: "GET" })
const cmds = await res.json()
console.log(cmds)
return cmds
} catch (err) {
console.log(err)
}
}
//gets a single guild command
export function getCommand(appId, guildId, commandName) {
const cmd = getCommands(appId, guildId).then((res) =>
res.filter((c) => c.name === commandName)
)
return cmd
}
//delete all commands
export async function deleteGuildCommands(appId, guildId, commands) {
if (guildId === "" || appId === "") return
commands.forEach((c) => deleteGuildCommand(appId, guildId, c))
}
// deletes command
export async function deleteGuildCommand(appId, guildId, commandId) {
// API endpoint to get and post guild commands
const endpoint = `applications/${appId}/guilds/${guildId}/commands/${commandId}`
// install command
try {
await discordRequest(endpoint, { method: "DELETE" })
console.log(`${commandId} has been deleted`)
} catch (err) {
console.error(err)
}
}
export async function getCommandsAttributes(appId, guildId, attribute) {
let attributes = []
const commands = await getCommands(appId, guildId)
commands.forEach((cmd) => {
attributes.push(cmd[attribute])
})
console.log(attributes)
return attributes
}
//assigns a role to a user
export async function assignRole(guildId, userId, roleId) {
const endpoint = `guilds/${guildId}/members/${userId}/roles/${roleId}`
try {
console.log("trying to request role assign")
await discordRequest(endpoint, { method: "PUT" })
} catch (err) {
console.error(err)
}
}
export function waitMs(ms) {
console.log(`waiting ${ms} ms`)
return new Promise((res) => {
setTimeout(() => {
res("resolved")
}, ms)
})
}
export async function assignRolesWait(guildId, userIds, roleId) {
for await (const userId of userIds) {
assignRole(guildId, userId, roleId)
waitMs(5000)
}
}
export async function unAssignRole(guildId, userId, roleId) {
const endpoint = `guilds/${guildId}/members/${userId}/roles/${roleId}`
try {
console.log("trying to request role assign")
await discordRequest(endpoint, { method: "DELETE" })
} catch (err) {
console.error(err)
}
}
export async function unassignRolesWait(guildId, userIds, roleId) {
for await (const userId of userIds) {
unAssignRole(guildId, userId, roleId)
waitMs(5000)
}
}
export async function getGuildMembers(guildId) {
const endpoint = `guilds/${guildId}/members?limit=1000`
try {
let guildMembers = await discordRequest(endpoint, { method: "GET" })
return guildMembers.json()
} catch (err) {
console.log(err)
}
}
export const TEST_COMMAND = {
name: "test",
description: "Basic guild command",
type: 1,
}
export const JOIN_COMMAND = {
name: "join",
description: "Join a faction for the faction war game",
type: 1,
}
export const START_FACTION_COMMAND = {
name: "start_faction",
description: "Create a faction war game.",
type: 1,
}
export const ASSIGN_COMMAND = {
name: "assign",
description: "To assign a specific user to a specific faction.",
options: [
{
type: 6,
name: "viewer",
description: "Pick a user to assign to a faction.",
required: true,
},
{
type: 3,
name: "faction",
description: "Pick a faction to assign the user to.",
required: true,
style: 1,
},
],
}
export const UNASSIGN_COMMAND = {
name: "unassign",
description: "To assign a specific user to a specific faction.",
options: [
{
type: 6,
name: "viewer",
description: "Pick a user to unassign.",
required: true,
},
],
}
export const UNASSIGN_ALL_COMMAND = {
name: "unassign_all",
description: "Unassign all users from factions, retain factions",
type: 1,
}
export const ASSIGN_ALL_COMMAND = {
name: "assign_all",
description: "Assign all queued users to random factions",
type: 1,
}
export const RESET_COMMAND = {
name: "reset",
description: "Reset a faction war game.",
type: 1,
}
export const PRINT_FACTIONS_COMMAND = {
name: "print_factions",
description: "Print all current faction assignments.",
type: 1,
}
export const START_WAR_COMMAND = {
name: "start_war",
description: "Begin the faction war.",
type: 1,
}
export const PRINT_QUEUE_COMMAND = {
name: "print_queue",
description: "Print the list of queued players",
type: 1,
}
export const RESET_ROLES_COMMAND = {
name: "reset_roles",
description: "Reset all the faction roles",
type: 1,
}