-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensei.js
86 lines (66 loc) · 2.11 KB
/
sensei.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
global.Stopwatch = require('@sapphire/stopwatch').Stopwatch;
let startupTime = new Stopwatch();
global.initTime = null;
global.testMode = false;
console.log("starting...");
// Load environment variables
require('dotenv').config();
global.Discord = require('discord.js');
global.OpenAI = require('openai');
global.gptEncode = require('gpt-3-encoder').encode;
global.fs = require('fs');
global.Jimp = require('jimp');
global.mysql = require('mysql');
// Load main variables
require("./mainVars.js");
// OpenAI
global.aiConfig = new OpenAI.Configuration({
apiKey: process.env.OPENAI_API_KEY
});
global.openai = new OpenAI.OpenAIApi(aiConfig);
// Discord
global.client = new Discord.Client({intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.DIRECT_MESSAGES,
Discord.Intents.FLAGS.GUILD_MESSAGES
]});
global.applyGlobalCommands = () => {
if (!client.isReady()) return;
let commandTree = [];
for (let [name, command] of commands) {
commandTree.push(command.structure);
}
client.application.commands.set(commandTree);
}
client.on('ready', () => {
applyGlobalCommands();
initTime = startupTime.stop().toString();
console.log(`Connected to Discord! Took ${initTime}`);
logToServer(`Connected to Discord! Took ${initTime}`);
});
global.loadFile = file => {
if (require.cache[require.resolve(file)])
delete require.cache[require.resolve(file)];
let plugin = require(file);
if (plugin.type === "command") {
commands.set(plugin.structure.name, plugin);
} else if (plugin.type === "devCommand") {
devCommands.set(plugin.name, plugin);
}
return plugin;
}
global.requireAll = dir => {
let required = 0;
fs.readdirSync(dir).forEach(file => {
let path = `${dir}/${file}`;
if (fs.statSync(path).isDirectory()) {
requireAll(path);
} else if (path.endsWith(".js")) {
loadFile(path);
required++;
}
});
return required;
}
requireAll("./plugins");
client.login(testMode ? process.env.TEST_BOT_TOKEN : process.env.DISCORD_BOT_TOKEN);