-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
80 lines (66 loc) · 2.38 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
const { Client, GatewayIntentBits, PermissionsBitField } = require('discord.js');
const http = require("http");
const config = require("./core/config");
const ChatHandler = require("./core/chathandler");
const CommandsLib = require("./core/commands");
const { exit } = require('process');
const fs = require('fs');
const motd = fs.readFileSync('./config/motd.txt', 'utf8') || "Bot ready!";
// Create an instance of a Discord client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
console.log('Starting Bot...');
//Load the token from config
if (!config.load()) {
console.log("Since the bot failed to load the config, the bot will now terminate.");
exit(1);
}
// The token of your bot - https://discordapp.com/developers/applications/me
const token = config.getToken();
// The ready event is vital, it means that your bot will only start reacting to information
var instance = this;
// from Discord _after_ ready is emitted
client.on('ready', async () => {
let link = client.generateInvite({
permissions: PermissionsBitField.All,
scopes: ['bot'],
});
console.log(`Generated bot invite link: ${link}`);
ChatHandler.setParent(instance);
CommandsLib.loadCommands();
require("./core/modulehandler"); //Load modules after load
console.log("Loaded " + config.getOps().length + " op(s) and " + config.getBarredUsers().length + " barred user(s)");
});
// Create an event listener for messages
client.on('messageCreate', async message => {
if (client.user.id != message.author.id) {
await ChatHandler.handle(message.content, message.author, message.channel, message);
}
});
process.on('uncaughtException', handleException);
process.on('unhandledRejection', handleException);
async function handleException(e) {
console.error(e);
if (client.lastUsedChannel !== undefined) {
try {
await client.lastUsedChannel.send("An error has occured! `" + e + "`");
} catch(e) {
console.warn("Couldn't log error to lastUsedChannel: ", e);
}
} else console.warn("No last used channel to log the error to!");
}
// Log our bot in
client.login(token).then(() => {
console.log(motd);
});
module.exports = {
client,
ChatHandler,
CommandsLib
}