-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
192 lines (162 loc) · 4.37 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
import { getRPSChoices } from './game.js';
import { capitalize, DiscordRequest } from './utils.js';
export async function HasGlobalCommands(appId, commands) {
if (appId === '') return;
commands.forEach((c) => HasGlobalCommand(appId, c));
}
export async function HasGuildCommands(appId, guildId, commands) {
if (guildId === '' || appId === '') return;
commands.forEach((c) => HasGuildCommand(appId, guildId, c));
}
// Checks for a global command
async function HasGlobalCommand(appId, command) {
// API endpoint to get and post guild commands
const endpoint = `applications/${appId}/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']}"`);
InstallGlobalCommand(appId, command);
} else {
console.log(`"${command['name']}" command already installed`);
}
}
} catch (err) {
console.error(err);
}
}
// Checks for a guild 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 guild 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);
}
}
//Installs a global command
export async function InstallGlobalCommand(appId, command) {
// API endpoint to get and post guild commands
const endpoint = `applications/${appId}/commands`;
// install command
try {
await DiscordRequest(endpoint, { method: 'POST', body: command });
} catch (err) {
console.error(err);
}
}
// Get the game choices from game.js
function createCommandChoices() {
const choices = getRPSChoices();
const commandChoices = [];
for (let choice of choices) {
commandChoices.push({
name: capitalize(choice),
value: choice.toLowerCase(),
});
}
return commandChoices;
}
//CREATE COMMAND OBJECT
// Simple test command
export const TEST_COMMAND = {
name: 'test',
description: 'Basic guild command',
type: 1,
};
//coinflip command
export const COINFLIP_COMMAND = {
name: 'coinflip',
description: 'flips a coin',
type: 1,
};
//dice command
export const DICE_COMMAND = {
name: 'dice',
description: 'Rolls a dice',
options: [
{
type: 4,
name: 'count',
description: 'The number of dice you want to roll.',
required: true,
},
{
type: 4,
name: 'sides',
description: 'The number of sides per dice.',
required: true,
},
],
type: 1,
};
//win command
export const WIN_COMMAND = {
name: 'win',
description: 'pings the person calling the command with w',
options: [
{
type: 6,
name: 'user',
description: 'Pick your desired winner',
required: true,
},
],
type: 1,
};
//lose command
export const LOSE_COMMAND = {
name: 'lose',
description: 'pings the person specified with L',
options: [
{
type: 6,
name: 'user',
description: 'Pick your desired loser',
required: true,
},
],
type: 1,
};
// Command containing options
export const CHALLENGE_COMMAND = {
name: 'challenge',
description: 'Challenge to a match of rock paper scissors',
options: [
{
type: 3,
name: 'object',
description: 'Pick your object',
required: true,
choices: createCommandChoices(),
},
],
type: 1,
};