forked from Enraa/Gagbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
478 lines (442 loc) · 21.9 KB
/
index.js
File metadata and controls
478 lines (442 loc) · 21.9 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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
const discord = require('discord.js')
const dotenv = require('dotenv')
dotenv.config()
const fs = require('fs');
const path = require('path');
const https = require('https');
const { assignMitten, garbleMessage, setUpGags, modifymessage, loadMittenTypes } = require(`./functions/gagfunctions.js`);
const { handleKeyFinding } = require('./functions/keyfindingfunctions.js');
const { restartChastityTimers } = require('./functions/timelockfunctions.js');
const { loadHeavyTypes } = require('./functions/heavyfunctions.js');
const { loadHeadwearTypes } = require('./functions/headwearfunctions.js')
const { assignCorset, setUpCorsets } = require('./functions/corsetfunctions.js');
const { assignMemeImages, generateListTexts } = require('./functions/interactivefunctions.js');
const { backupsAreAnnoying, saveFiles, processUnlockTimes, processTimedEvents, importFileNames, scavengeUsers } = require('./functions/timefunctions.js');
const { loadEmoji } = require("./functions/messagefunctions.js");
const { loadWearables } = require("./functions/wearablefunctions.js");
const { knownServer, setGlobalCommands, loadWebhooks, getBotOption, getOption } = require('./functions/configfunctions.js');
const { getAllJoinedGuilds } = require('./functions/configfunctions.js');
const { setUpToys } = require('./functions/toyfunctions.js');
const { setUpChastity } = require('./functions/chastityfunctions.js');
const { loadCollarTypes } = require('./functions/collarfunctions.js');
// Prevent node from killing us immediately when we do the next line.
process.stdin.resume();
// I've never considered overriding this before lol
// This catches control+C and other manual ways of killing the application.
process.on('SIGINT', () => {
try {
console.log('Received SIGINT. Performing graceful shutdown...');
saveFiles();
process.exit(0);
}
catch (err) {
console.log(err);
process.exit(1);
}
});
// This should catch a SIGTERM emitted as part of spinning down Docker
process.on('SIGTERM', () => {
try {
console.log('Received SIGTERM. Performing graceful shutdown...');
saveFiles();
process.exit(0);
}
catch (err) {
console.log(err);
process.exit(1);
}
});
// This catches program crashes. Note, we're not stopping the program from
// killing itself, but we will attempt to write out the CURRENT state
// of all process variables to their appropriate files.
// This method runs immediately BEFORE an uncaughtException. Note anything we do here must be sync.
process.on('uncaughtExceptionMonitor', (err,origin) => {
if (GagbotSavedFileDirectory) {
console.error(`Uncaught Exception. Performing "graceful" shutdown...`)
saveFiles();
if (!fs.existsSync(`${process.GagbotSavedFileDirectory}/crashlog.txt`)) {
fs.writeFileSync(`${process.GagbotSavedFileDirectory}/crashlog.txt`, "Start of Crash Log")
}
let exceptionlog = `\n${new Date().toString()} -------------------------------`
exceptionlog = `${exceptionlog}\nUncaught exception:\n${err.stack}`
fs.appendFileSync(`${process.GagbotSavedFileDirectory}/crashlog.txt`, exceptionlog)
}
})
// If they never changed from the default in .env.md, use base directory.
if (process.env.GAGBOTFILEDIRECTORY === "Z:\\Somewhere\\I\\Belong\\") { process.env.GAGBOTFILEDIRECTORY = "." }
let GagbotSavedFileDirectory = process.env.GAGBOTFILEDIRECTORY ? process.env.GAGBOTFILEDIRECTORY : __dirname
process.GagbotSavedFileDirectory = GagbotSavedFileDirectory // Because honestly, I dont know WHY global stuff in index.js can't be accessble everywhere
let processdatatoload = [
{ textname: "gaggedusers.txt", processvar: "gags", default: {} },
{ textname: "mittenedusers.txt", processvar: "mitten", default: {} },
{ textname: "chastityusers.txt", processvar: "chastity", default: {} },
{ textname: "chastitybrausers.txt", processvar: "chastitybra", default: {} },
{ textname: "toyusers.txt", processvar: "toys", default: {} },
{ textname: "collarusers.txt", processvar: "collar", default: {} },
{ textname: "heavyusers.txt", processvar: "heavy", default: {} },
{ textname: "pronounsusers.txt", processvar: "pronouns", default: {} },
{ textname: "usersdata.txt", processvar: "usercontext", default: {} },
{ textname: "consentusers.txt", processvar: "consented", default: {} },
{ textname: "corsetusers.txt", processvar: "corset", default: {} },
{ textname: "arousal.txt", processvar: "arousal", default: {} },
{ textname: "headwearusers.txt", processvar: "headwear", default: {} },
{ textname: "discardedkeys.txt", processvar: "discardedKeys", default: [] },
{ textname: "configs.txt", processvar: "configs", default: {}},
{ textname: "outfits.txt", processvar: "outfits", default: {}},
{ textname: "dollusers.txt", processvar: "dolls", default: {}},
{ textname: "wearables.txt", processvar: "wearable", default: {}},
{ textname: "webhooks.txt", processvar: "webhookstoload", default: {}},
{ textname: "recordedmessages.txt", processvar: "recordedmessages", default: {}},
]
processdatatoload.forEach((s) => {
try {
if (!fs.existsSync(`${process.GagbotSavedFileDirectory}/${s.textname}`)) {
fs.writeFileSync(`${process.GagbotSavedFileDirectory}/${s.textname}`, JSON.stringify(s.default))
}
process[s.processvar] = JSON.parse(fs.readFileSync(`${process.GagbotSavedFileDirectory}/${s.textname}`))
}
catch (err) {
console.log(`Error loading ${s.textname}`)
console.log(err)
}
})
// Later loaders for autocompletes
setUpGags();
loadHeavyTypes();
loadHeadwearTypes();
loadMittenTypes();
loadCollarTypes();
loadWearables();
assignMemeImages();
setUpToys();
setUpChastity();
setUpCorsets();
// Build the Overview
process.helpmodals = {
"Overview": (userid, page) => {
// This is broken down into two pages on purpose, to ensure its not a HUGE block of info.
// We need to handle this appropriately.
let overviewtext = [`## Basic Gagbot Commands Reference:
### Alter Speech:
**/gag**, **/ungag**: Gag someone or yourself, garbling speech in various ways.
**/toy**, **/untoy**: Apply a toy to someone or yourself, causing stuttered speech from arousal.
**/corset**, **/uncorset**: Apply a tightly laced corset, limiting sentence length in each message.
### Restrict access to commands above:
**/mitten**, **/unmitten**: Wear mittens, preventing yourself from changing gags or masks.
**/chastity**, **/unchastity**: Wear chastity, preventing changes to toys and corsets without the key.
**/heavy**, **/unheavy**: Wear heavy bondage, preventing access to most commands. `,`### Others:
**/mask**, **/unmask**: Varying effects, many of these block emotes in speech or inspect. Requires collar access if not on self.
**/collar**, **/uncollar**: Add or remove a collar, which can be set to allow users to /collarequip you, allowing them to do the commands in the above section.
**/collarequip**: Apply a restraint listed in the above section to another person. Requires collar access.
**/key**: Transfer, clone or revoke cloned keys from a restraint you have the primary key for.
**/inspect**: Look at what a user is wearing or keys they're holding.
**/config**: Modify various settings about you on the bot.
**/struggle**: Get a fun little text. Does not have any actual effect on restraint status.
**/letgo**: Clear arousal, with a different text if at orgasm threshold.
**/timelock**: Timelock a keyed restraint.
**/wear**, **/unwear**, **/outfit**: Adjust clothing, outfits and more!
**/item**: Protect or unprotect items from being removed with /unwear.
**/pronouns**: Set your pronouns for the bot's text feedback.`]
if (!overviewtext[page]) page = 0;
overviewtextdisplay = new discord.TextDisplayBuilder().setContent(overviewtext[page])
let optionbuttons = [
// Page Down
new discord.ButtonBuilder()
.setCustomId(`help_Overview_0`)
.setLabel("← Prev Page")
.setStyle(discord.ButtonStyle.Secondary)
.setDisabled(page != 1),
// Page Up
new discord.ButtonBuilder()
.setCustomId(`help_Overview_1`)
.setLabel("Next Page →")
.setStyle(discord.ButtonStyle.Secondary)
.setDisabled(page != 0),
];
return [overviewtextdisplay, new discord.ActionRowBuilder().addComponents(...optionbuttons)];
}
}
// Grab all the command files from the commands directory
const commands = new Map();
const modalHandlers = new Map();
const componentHandlers = new Map();
const autocompletehandlers = new Map();
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const cmd = require(path.join(commandsPath, file));
if ((cmd.execute) && (cmd.data)) {
commands.set(cmd.data.name, cmd);
}
if (cmd.modalexecute) modalHandlers.set(file, cmd);
cmd.componentHandlers?.forEach((handler) => {
componentHandlers.set(handler.key, handler);
});
if (cmd.autoComplete) autocompletehandlers.set(file, cmd);
if (cmd.help) process.helpmodals[file.slice(0,1).toUpperCase() + file.slice(1).replace(".js","")] = cmd.help;
}
// Grab any context menu interactions
const usercontextcommands = new Map();
const usercontextcommandsPath = path.join(__dirname, 'contextcommands', "user");
const usercontextcommandsFiles = fs.readdirSync(usercontextcommandsPath).filter(file => file.endsWith('.js'));
for (const file of usercontextcommandsFiles) {
const cmd = require(path.join(usercontextcommandsPath, file));
if ((cmd.execute) && (cmd.data)) {
usercontextcommands.set(cmd.data.name, cmd);
}
}
// Grab any context menu interactions
const messagecontextcommands = new Map();
const messagecontextcommandsPath = path.join(__dirname, 'contextcommands', "message");
const messagecontextcommandsFiles = fs.readdirSync(messagecontextcommandsPath).filter(file => file.endsWith('.js'));
for (const file of messagecontextcommandsFiles) {
const cmd = require(path.join(messagecontextcommandsPath, file));
if ((cmd.execute) && (cmd.data)) {
messagecontextcommands.set(cmd.data.name, cmd);
}
if (cmd.modalexecute) modalHandlers.set(file, cmd);
}
var gagged = {}
const client = new discord.Client({
intents: [
discord.GatewayIntentBits.Guilds,
discord.GatewayIntentBits.GuildMessages,
discord.GatewayIntentBits.MessageContent,
discord.GatewayIntentBits.GuildMembers
]
})
client.on("clientReady", async () => {
// This is run once we’re logged in!
process.client = client;
console.log(`Logged in as ${client.user.tag}!`)
// Please stop crashing
if (process.webhook == undefined) { process.webhook = {} }
if (process.recentmessages == undefined) { process.recentmessages = {} }
try {
await client.application.fetch();
console.log(`Bot is owned by user ID ${client?.application?.owner.id}`)
console.log(`Executable Functions: [${Array.from(commands.keys()).join(", ")}]`);
console.log(`Modals: [${Array.from(modalHandlers.keys()).join(", ")}]`);
console.log(`Components: [${Array.from(componentHandlers.keys()).join(", ")}]`);
console.log(`Autocompletes: [${Array.from(autocompletehandlers.keys()).join(", ")}]`);
// Load emoji into the application's emoji manager
loadEmoji(client);
// Load the /config function globally, as we can handle that whereever.
setGlobalCommands(client);
// Check which guilds we're in!
getAllJoinedGuilds(client);
// Load webhooks
await loadWebhooks(client);
//console.log(`Webhook Channels: [${Array.from(process.webhook.keys()).join(", ")}]`)
generateListTexts();
scavengeUsers(client);
setInterval(() => {
try {
scavengeUsers(client);
}
catch (err) { console.log(err) }
try {
removeOldMessages();
}
catch (err) { console.log(err) }
}, 3600000);
}
catch (err) {
console.log(err)
}
process.timetick = setInterval(() => {
processTimedEvents()
}, getBotOption("bot-timetickrate") ?? 6000)
//restartChastityTimers(client);
// setInterval(updateArousalValues, Number(process.env.AROUSALSTEPSIZE ?? 6000));
})
client.on("messageCreate", async (msg) => {
// This is called when a message is received.
try {
if (msg.author.bot || msg.webhookId || msg.stickers?.first()) { return };
/*console.log(`${(msg.channel.id != process.env.CHANNELID)}`)
console.log(`${msg.webhookId}`)
console.log(`${msg.author.bot}`)
console.log(`${msg.stickers?.first()}`)
console.log(`${msg.attachments?.first()}`)*/
let channelid = msg.channelId;
let thread = false;
if (msg.channel.isThread()) {
thread = true
channelid = msg.channel.parentId
}
if (process.webhook[channelid]) {
if ((getBotOption("bot-allowkeyfinding") == "Enabled") && (getOption(msg.author.id, "canfindkeys") == "enabled")) {
handleKeyFinding(msg);
}
process.recentmessages[msg.author.id] = msg.channel.id;
modifymessage(msg, thread ? msg.channelId : null);
}
if ((msg.channel.id != process.env.CHANNELID && msg.channel.parentId != process.env.CHANNELID) || (msg.webhookId) || (msg.author.bot) || (msg.stickers?.first())) { return }
//console.log(msg.member.displayAvatarURL())
//console.log(msg.member.displayName)
//handleKeyFinding(msg);
//garbleMessage(msg.channel.isThread() ? msg.channelId : null, msg);
}
catch (err) {
console.log(err);
}
})
client.on('interactionCreate', async (interaction) => {
try {
if (interaction.isUserContextMenuCommand()) {
usercontextcommands.get(`${interaction.commandName}`)?.execute(interaction)
return;
}
if (interaction.isMessageContextMenuCommand()) {
messagecontextcommands.get(`${interaction.commandName}`)?.execute(interaction)
return;
}
if (interaction.isModalSubmit()) {
// We can't pass custom data through the modal except via the ID, so separate out the first part
// as IDs will come in like collar_12451251253 - we want the collar part to query the command.
let interactioncommand = interaction.customId.split("_")[0]
if (interactioncommand == "webhookedit") {
interactioncommand = "Edit Message"
}
else if (interactioncommand == "modalevent") {
if (process.modalexecutefunctions) {
let filecommand = interaction.customId.split("_")[1]
Object.keys(process.modalexecutefunctions).forEach((k) => {
if (process.modalexecutefunctions[k][filecommand]) {
process.modalexecutefunctions[k][filecommand](interaction)
return;
}
})
}
}
console.log(interactioncommand);
modalHandlers.get(`${interactioncommand}.js`)?.modalexecute(interaction);
return;
}
if (interaction.isMessageComponent()) {
// Lazy workaround for config handling, that will probably stand the test of time.
if (interaction.customId.startsWith("config_")) {
let configfunc = require(`./commands/config.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("list_")) {
let configfunc = require(`./commands/list.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("outfitter_")) {
let configfunc = require(`./commands/outfit.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("inspect_")) {
let configfunc = require(`./commands/inspect.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("help_")) {
let configfunc = require(`./commands/help.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("key_")) {
let configfunc = require(`./commands/key.js`)
configfunc.interactionresponse(interaction);
}
else if (interaction.customId.startsWith("extraconfig_")) {
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.gags && process.extraconfigresponsefunctions.gags[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.gags[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.headwear && process.extraconfigresponsefunctions.headwear[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.headwear[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.mitten && process.extraconfigresponsefunctions.mitten[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.mitten[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.heavy && process.extraconfigresponsefunctions.heavy[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.heavy[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.chastity && process.extraconfigresponsefunctions.chastity[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.chastity[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.chastitybra && process.extraconfigresponsefunctions.chastitybra[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.chastitybra[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.wearable && process.extraconfigresponsefunctions.wearable[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.wearable[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.toys && process.extraconfigresponsefunctions.toys[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.toys[interaction.customId.split("_")[1]](interaction);
}
if (process.extraconfigresponsefunctions && process.extraconfigresponsefunctions.collar && process.extraconfigresponsefunctions.collar[interaction.customId.split("_")[1]]) {
process.extraconfigresponsefunctions.collar[interaction.customId.split("_")[1]](interaction);
}
}
const [key, ...args] = interaction.customId.split("-");
componentHandlers.get(key)?.handle(interaction, ...args);
return;
}
if (interaction.isAutocomplete()) {
try {
autocompletehandlers.get(`${interaction.commandName}.js`)?.autoComplete(interaction)
}
catch (err) {
console.log(err);
}
return;
}
if (interaction.commandName === "config") {
commands.get(interaction.commandName)?.execute(interaction);
return;
}
let channelid = interaction.channelId;
let thread = false;
if (interaction.channel.isThread()) {
thread = true
channelid = interaction.channel.parentId
}
if (process.webhook[channelid]) {
commands.get(interaction.commandName)?.execute(interaction);
return;
}
else {
interaction.reply({ content: `Please use this command in a channel that's setup for it.`, flags: discord.MessageFlags.Ephemeral })
return;
}
}
catch (err) {
console.log(err);
}
})
client.on(`guildDelete`, async (guild) => {
try {
if (process.joinedguilds.includes(guild.id)) {
process.joinedguilds.splice(process.joinedguilds.indexOf(guild.id, 1))
}
}
catch (err) {
console.log(err);
}
try {
if (process.configs.servers[guild.id]) {
delete process.configs.servers[guild.id]
}
}
catch (err) {
console.log(err);
}
})
client.on(`guildCreate`, async (guild) => {
getAllJoinedGuilds(client) // Rebuild the list!
})
// I refuse to use a proper database with backups.
// This is a solution to backup the terrible database.
backupsAreAnnoying();
let backupset = setInterval(() => {
backupsAreAnnoying()
}, parseInt(process.env.BACKUPDELAY ?? 3600000)) // Backups every one hour, or time specified in .env
let savefileset = setInterval(() => {
saveFiles();
}, parseInt(process.env.SAVEDELAY ?? 60000)) // Backups every one hour, or time specified in .env
if (process.webhook) {
process.webhook = {};
}
importFileNames();
client.login(process.env.DISCORDBOTTOKEN)