This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
438 lines (417 loc) · 14.2 KB
/
bot.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
let colors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
const fs = require("fs");
/* The Data, We Need It! */ let data = JSON.parse(
fs.readFileSync("./AppData/data.json"),
);
try {
const discord = require("oceanic.js");
const {
ApplicationCommandOptionTypes,
ApplicationCommandTypes,
InteractionTypes,
CommandInteraction,
PermissionNames,
} = require("oceanic.js");
const client = new discord.Client({
auth: `Bot ${data.btk}`,
gateway: {
intents: ["ALL"],
},
});
client.connect();
let eventStorage = {}
/* Project Startup */ console.log(
`${colors.BgWhite}${colors.FgBlack}${data.name}${colors.Reset}${colors.FgCyan} is starting up...${colors.Reset}`,
);
client.on("ready", async () => {
/* Project Start */ console.log(
`${colors.FgGreen}Studio Bot Maker V3.1.1 Project, started successfully!${colors.Reset}`,
);
await client.application.bulkEditGlobalCommands([]);
console.log(
`${colors.BgGreen + colors.FgBlack}Purged All Slash Commands${
colors.Reset
}`,
);
registerCommands();
});
let IO /* In / Out */ = {
write: (newIO) => {
try {
let dir = data.prjSrc;
fs.writeFileSync(`${dir}/AppData/Toolkit/storedData.json`, JSON.stringify(newIO))
} catch (err) {
console.log(`${colors.BgRed}${colors.FgWhite}Something Went Wrong Whilst Trying To Write To The Data System, Ensure You've Exported Your Bot!${colors.Reset}`)
}
},
get: () => {
try {
let dir = data.prjSrc;
return JSON.parse(fs.readFileSync(`${dir}/AppData/Toolkit/storedData.json`, 'utf8'));
} catch (err) {
console.log(`${colors.BgRed}${colors.FgWhite}Something Went Wrong Whilst Trying To Access The Data System, Ensure You've Exported Your Bot!${colors.Reset}`)
}
}
}
/* Used For Running Action Arrays - Universal Action Array Runner */
const runActionArray = async (at, interaction, client, actionBridge) => {
new Promise(async (resolve) => {
let cmdActions;
let cmdName = "Inbuilt";
let cmdAt = "Inbuilt";
if (typeof at == "string") {
cmdActions = data.commands[at].actions;
cmdName = data.commands[at].name;
cmdAt = at;
} else {
cmdActions = at;
}
let globalActionCache = {};
if (typeof actionBridge == 'object' && !actionBridge.globalActionCache) {
globalActionCache = {globalActionCache: {}}
}
let actionContextBridge = {
guild: interaction.guild || null,
stopActionRun: false,
variables: typeof actionBridge == "object" ? {...actionBridge, ...globalActionCache} : {globalActionCache: {}},
data: {
ranAt: cmdAt,
nodeName: cmdName,
allActions: cmdActions,
IO: IO
},
runner: runActionArray,
fs: fs,
toMember: async (user, guild) => {
if (user.guild) return user
return await guild.getMember(user.id)
},
toUser: async (member) => {
if (member.createDM) return member
return client.users.get(member.id) || await client.rest.users.get(member.id)
}
};
for (let action in cmdActions) {
if (cmdActions[action] != undefined) {
/* See If The Thing Is Meant To Keep Going! */
if (actionContextBridge.stopActionRun == false) {
try {
/* Run The Action, Make It Happen! */
await require(`./AppData/Actions/${cmdActions[action].file}`).run(
cmdActions[action].data,
interaction,
client,
actionContextBridge,
);
} catch (err) {
/* Alert The User Of The Error */
console.log(
`${colors.BgRed}${colors.FgBlack}${cmdName} ${
colors.FgBlack + colors.BgWhite
}(@#${cmdAt})${
colors.Reset + colors.BgRed + colors.FgBlack
} >>> ${
require(`./AppData/Actions/${cmdActions[action].file}`).data
.name
} ${colors.FgBlack + colors.BgWhite}(@#${action})${
colors.Reset + colors.BgRed + colors.FgBlack
} >>> Error: ${err}${colors.Reset}`,
);
console.log(err);
}
} else {
resolve();
return;
}
}
}
resolve();
});
};
client.on("messageCreate", async (msg) => {
if (msg.author.bot) return;
for (let i in data.commands) {
if (data.commands[i].type == "action") {
let command = data.commands[i];
let commandName = data.commands[i].name;
if (command.trigger == "textCommand") {
if (`${data.prefix}${commandName}`.toLowerCase() == msg.content.split(" ")[0].toLowerCase()) {
let matchesPermissions = true;
if (command.boundary) {
if (command.boundary.worksIn == "guild") {
if (!msg.guild) {
matchesPermissions = false;
}
}
if (command.boundary.worksIn == "dms") {
if (msg.guild) {matchesPermissions = false}
}
for (let permission in command.boundary.limits) {
if (msg.member.permissions.has(command.boundary.limits[permission]) == false) {
matchesPermissions = false;
}
}
}
if (matchesPermissions == true) {
runActionArray(i, msg, client);
}
}
} else {
if (command.trigger == "messageContent") {
let messageContent = `${msg.content}`;
if (messageContent.toLowerCase().split(" ").includes(command.name.toLowerCase()) &&`${msg.content}`.toLowerCase().startsWith(data.prefix) == false) {
let matchesPermissions = true;
if (command.boundary) {
if (command.boundary.worksIn == "guild") {
if (!msg.guild) {
matchesPermissions = false;
}
}
if (command.boundary.worksIn == "dms") {
if (msg.guild) matchesPermissions = false;
}
for (let permission in command.boundary.limits) {
if (msg.member.permissions.has(command.boundary.limits[permission]) == false) {matchesPermissions = false;}
}
}
if (matchesPermissions == true) {
runActionArray(i, msg, client);
}
}
}
}
}
}
});
let commands = [];
for (let i in data.commands) {
if (
data.commands[i].trigger == "slashCommand" &&
data.commands[i].type == "action"
) {
/* Soon To Be Added In V3 */
let description = "No Description";
/* Soon To Be Added In V3 */
let parameterDefaultDescription = "No Description";
let commandParameters = [];
if (
description == undefined ||
description == null ||
description.replace(/\s/g, "").length == 0
) {
description = "No Description!";
} else {
description = data.commands[i].description;
}
for (let parameter of data.commands[i].parameters) {
let parameterType;
let isRequired;
let parameterDescription = parameterDefaultDescription;
switch (parameter.type.toLowerCase()) {
case "string":
parameterType = ApplicationCommandOptionTypes.STRING;
break;
case "boolean":
parameterType = ApplicationCommandOptionTypes.BOOLEAN;
break;
case "user":
parameterType = ApplicationCommandOptionTypes.USER;
break;
case "channel":
parameterType = ApplicationCommandOptionTypes.CHANNEL;
break;
case "integer":
parameterType = ApplicationCommandOptionTypes.INTEGER;
break;
case "role":
parameterType = ApplicationCommandOptionTypes.ROLE;
break;
}
if (parameter.description.trim() != "") {
parameterDescription = parameter.description;
}
/* Create & Push The Parameter */
let endParameter = {
name: parameter.name.toLowerCase(),
type: parameterType,
required: parameter.required,
description: parameter.description,
};
commandParameters.push(endParameter);
}
/* Moving On To The Command In Itself */
if (commandParameters != [] && commandParameters[0]) {
let commandName = data.commands[i].name.trim().toLowerCase();
const command = {
name: commandName,
description: description,
options: commandParameters,
};
commands.push(command);
} else {
let commandName = data.commands[i].name.trim().toLowerCase();
const command = {
name: commandName,
description: description,
};
commands.push(command);
}
}
if (data.commands[i].type == "event") {
let event = require(`./AppData/Events/${data.commands[i].eventFile}`);
/* Initialize The Event */
// eventData: Array made of 2 elements; Based on the event, only one or two of them will be used.
// client: client
// fs: bot.js file system
// runActionArray: action runner
// i: where the command's at
// eventStorage: shared event storage
event.run(data.commands[i].eventData, client, fs, runActionArray, i, eventStorage);
}
}
client.on("interactionCreate", async (interaction) => {
/* Check If The Interaction Was A Slash Command Or Not */
if (interaction.data.type != ApplicationCommandTypes.CHAT_INPUT) return;
/* Ability To Change These Soon To Be Added In V3.1 */
let defaultStringReturn = "-";
let defaultBooleanReturn = false;
let defaultUserReturn = null;
let defaultRoleReturn = null;
let defaultChannelReturn = null;
let defaultIntegerReturn = 0;
let defaultMentionableReturn = null;
/* Storage For Parameters */
let commandParametersStorage = {};
for (let i in data.commands) {
if (
data.commands[i].trigger == "slashCommand" &&
data.commands[i].type == "action" &&
interaction.data.name == data.commands[i].name
) {
if (
data.commands[i].parameters != undefined &&
data.commands[i].parameters[0] != undefined
) {
for (let e in data.commands[i].parameters) {
let parameterType = data.commands[i].parameters[e].type;
let values = data.commands[i].parameters[e].name;
let option;
switch (parameterType) {
case "string":
option =
interaction.data.options.getString(values, false) ||
defaultStringReturn;
break;
case "boolean":
option =
interaction.data.options.getBoolean(values, false) ||
defaultBooleanReturn;
break;
case "user":
option =
interaction.data.options.getUser(values, false) ||
defaultUserReturn;
break;
case "role":
option =
interaction.data.options.getRole(values, false) ||
defaultRoleReturn;
break;
case "channel":
option =
interaction.data.options.getChannel(values, false) ||
defaultChannelReturn;
break;
case "integer":
option =
interaction.data.options.getInteger(values, false) ||
defaultIntegerReturn;
break;
}
commandParametersStorage[data.commands[i].parameters[e].storeAs] = option;
}
}
let command = data.commands[i];
let matchesPermissions = true;
if (command.boundary) {
if (command.boundary.worksIn == "guild") {
if (!interaction.guild) {
matchesPermissions = false;
}
}
if (command.boundary.worksIn == "dms") {
if (interaction.guild) matchesPermissions = false;
}
for (let permission in command.boundary.limits) {
if (interaction.member.permissions.has(command.boundary.limits[permission]) == false) {matchesPermissions = false;}
}
}
interaction.author = interaction.user;
if (matchesPermissions) {
runActionArray(i, interaction, client, commandParametersStorage);
}
}
}
});
function registerCommands() {
for (let command of commands) {
if (!command.options) {
client.application.createGlobalCommand({
type: ApplicationCommandTypes.CHAT_INPUT,
name: command.name,
description: command.description || 'No Description',
dmPermission: false
});
} else {
client.application.createGlobalCommand({
type: ApplicationCommandTypes.CHAT_INPUT,
name: command.name,
description: command.description || 'No Description',
options: command.options,
dmPermission: false
});
}
console.log(
`${colors.Dim}(Slash Commands)${colors.Reset} - ${colors.BgGreen}${command.name}${colors.Reset} Has Been Released Into The Open!`,
);
}
console.log(
`${colors.FgGreen}All Slash Commands Have Been Registered!${colors.Reset}`,
);
}
} catch (err) {
console.log(
colors.Reset,
colors.FgRed,
colors.Underscore,
`Oops! An error has occured!`,
err,
colors.Reset,
);
}