-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
198 lines (149 loc) · 5.41 KB
/
client.js
File metadata and controls
198 lines (149 loc) · 5.41 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Forked from https://github.com/MatteZ02/discord-interactions
const apiUrl = "https://discord.com/api/v8";
// Class
class InteractionsClient {
// Constructor
constructor(data) {
// Get Config
const _ = require('lodash');
// Create Settings
const tinyCfg = _.defaultsDeep({}, data, {
bot_token: '',
client_id: '',
user_token: ''
});
// Token
if (typeof tinyCfg.bot_token === "string") {
this.token = { value: tinyCfg.bot_token, type: 'Bot' };
}
// Nope
else {
// Client Secret
if (typeof tinyCfg.user_token === "string") {
this.token = { value: tinyCfg.user_token, type: 'Bearer' };
}
// Nope
else {
throw new Error("discord-slash-commands-client | No bot token or user token provided.");
}
}
// ID
if (typeof tinyCfg.client_id === "string") { this.clientID = tinyCfg.client_id; }
// Nope
else {
throw new Error("discord-slash-commands-client | No clientID provided.");
}
// Return
return this;
}
// Get Commands
async getCommands(options = {}) {
// Prepare Module
const objType = require('@tinypudding/puddy-lib/get/objType');
// Prepare Options
if (!objType(options, 'object')) {
throw "options must be of type object. Received: " + typeof options;
}
// No Command ID
if (options.commandID && typeof options.commandID !== "string") {
throw (
"commandID received but wasn't of type string. received: " +
typeof options.commandID
);
}
// No Guild
if (options.guildID && typeof options.guildID !== "string") {
throw (
"guildID received but wasn't of type string. received: " +
typeof options.guildID
);
}
// URL
let url = options.guildID
? `${apiUrl}/applications/${this.clientID}/guilds/${options.guildID}/commands`
: `${apiUrl}/applications/${this.clientID}/commands`;
// Option
if (options.commandID) { url += `/${options.commandID}`; };
// Send Command
const res = await require("axios").get(url, {
headers: { Authorization: `${this.token.type} ${this.token.value}` }
});
// Complete
return res.data;
}
// Create Command
async createCommand(options, guildID) {
// Prepare Module
const objType = require('@tinypudding/puddy-lib/get/objType');
// No Options
if (!objType(options, 'object')) {
throw "options must be of type object. Received: " + typeof options;
}
// No Description
if (!options.name || !options.description) {
throw "options is missing name or description property!";
}
// URL
const url = guildID
? `${apiUrl}/applications/${this.clientID}/guilds/${guildID}/commands`
: `${apiUrl}/applications/${this.clientID}/commands`;
// Send Command
const res = await require("axios").post(url, options, {
headers: { Authorization: `${this.token.type} ${this.token.value}` },
});
// Complete
return res.data;
}
// Edit Command
async editCommand(options, commandID, guildID) {
// Prepare Module
const objType = require('@tinypudding/puddy-lib/get/objType');
// No Options
if (!objType(options, 'object')) {
throw "options must be of type object. Received: " + typeof options;
}
// No Command ID
if (typeof commandID !== "string") {
throw "commandID must be of type string. Received: " + typeof commandID;
}
// No Name
if (!options.name || !options.description) {
throw "options is missing name or description property!";
}
// No Guild Name
if (guildID && typeof guildID !== "string") {
throw (
"guildID received but wasn't of type string. received: " +
typeof guildID
);
}
// URL
const url = guildID
? `${apiUrl}/applications/${this.clientID}/guilds/${guildID}/commands/${commandID}`
: `${apiUrl}/applications/${this.clientID}/commands/${commandID}`;
// Send Command
const res = await require("axios").patch(url, options, {
headers: { Authorization: `${this.token.type} ${this.token.value}` },
});
// Complete
return res.data;
}
// Delete Command
async deleteCommand(commandID, guildID) {
// No String
if (typeof commandID !== "string") {
throw "commandID must be of type string. Received: " + typeof commandID;
}
// URL
const url = guildID
? `${apiUrl}/applications/${this.clientID}/guilds/${guildID}/commands/${commandID}`
: `${apiUrl}/applications/${this.clientID}/commands/${commandID}`;
// Delete
const res = await require("axios").delete(url, {
headers: { Authorization: `${this.token.type} ${this.token.value}` },
});
// Complete
return res.data;
}
}
module.exports = InteractionsClient;