-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
170 lines (130 loc) · 5 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
import fs from "fs";
import { Client, GatewayIntentBits, Partials } from "discord.js";
import colors from "colors";
import { output } from "./src/functions.js";
import cronEvents from "./modules/cronEvents.js";
// Yes Node, I know I'm using an experimental feature, stop telling me about it
const originalEmit = process.emit;
process.emit = function(name, data, ...args){
if(name === `warning` && typeof data === `object` && data.name === `ExperimentalWarning`){
return false;
}
return originalEmit.apply(process, arguments);
};
const defaultConfig = {
prefix: "!",
ownerID: "your discord id",
token: "your bots token",
weatherLoc: "Paris",
bookUpdatesChannel: "channel id",
bookUpdateURL: "jnovel json feed url"
};
// Check if the config file exists
if(!fs.existsSync("./config.json")){
output("error", "No config file found!");
try {
fs.writeFileSync("./config.json", JSON.stringify(defaultConfig, null, 4));
} catch (err){
throw new Error(`Failed to create config file! ${err.message}`);
}
output("warn", `A new config file has been created! Please edit the config to continue!`);
process.exit(1);
}
// Load the config
const configFile = fs.readFileSync("./config.json", "utf8");
const config = JSON.parse(configFile);
if(!config.token || config.token === "your bots token"){
output("error", "No token found in config! Please add your bots token to the config file!");
process.exit(1);
}
if(!config.prefix){
output("warn", "No prefix found in config! Using default prefix.");
config.prefix = defaultConfig.prefix;
}
if(!config.ownerID || config.ownerID === "your discord id"){
output("warn", "No owner ID found in config! Setting 'Clyde' as the owner");
config.ownerID = 1;
}
// Handle the unhandled things
process.on("uncaughtException", (err) => {
const errorMsg = err?.stack?.replace(new RegExp(`${__dirname}/`, "g"), "./");
output("error", `Uncaught Exception: ${errorMsg}`);
});
process.on("unhandledRejection", (err) => {
output("error", `Unhandled rejection: ${err}`);
});
console.log("Starting Bot...");
const intentFlags = [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.MessageContent
];
const client = new Client({
disableEveryone: true,
intents: intentFlags,
partials: [Partials.Message, Partials.Reaction]
});
client.config = config;
client.commands = new Map();
client.altNames = new Map();
client.slashCommands = [];
/**
* @name main
* @description Starts the discord bot
**/
const main = async () => {
// Load the events
output("misc", "Loading events...");
// Find the event with the longest name
const eventList = fs.readdirSync("./events").filter(file => file.endsWith(".js")).sort((a, b) => a.length - b.length);
let longestName = eventList[eventList.length - 1].length - 3;
for(const file of eventList){
try {
// Try loading the event file
const event = await import(`./events/${file}`);
if(!event.info.enabled) continue;
// Add the event to the event listener
client.on(event.info.name, event.run.bind(null, client));
const paddedName = event.info.name.padEnd(longestName, " ");
output("good", `Loaded event: ${paddedName} > ${event.info.description}`);
// delete require.cache[require.resolve(`./events/${file}`)];
} catch (err){
// Warn if the event failed to load
output("warn", `Failed to load event: ${file}!`);
output("error", err);
}
}
// Load the commands
output("misc", "Loading commands...");
// Find the command with the longest name
const commandList = fs.readdirSync("./commands").filter(file => file.endsWith(".js") || file.endsWith(".cjs")).sort((a, b) => a.length - b.length);
longestName = commandList[commandList.length - 1].length - 3;
for(const file of commandList){
try {
// Try loading the command file
const command = await import(`./commands/${file}`);
// Set the command file name for reloading later
command.info.fileName = file;
// Set the command name and aliases
client.commands.set(command.info.name, command);
command.info.altNames.forEach(alias => client.altNames.set(alias, command.info.name));
// If the command is a slash command
if(command.slash?.(client)?.data && command.info.enabled) client.slashCommands.push(command);
const paddedName = command.info.name.padEnd(longestName, " ");
const disableString = command.info.enabled ? "" : ` ${colors.red("(disabled)")}`;
output("good", `Loaded command: ${paddedName} > ${command.info.description}${disableString}`);
} catch (err){
// Warn if the command failed to load
output("warn", `Failed to load command: ${file}!\n${err}`);
}
}
client.login(client.config.token);
cronEvents(client);
};
main();