Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (117 loc) 路 5.82 KB
/
Copy pathindex.js
File metadata and controls
130 lines (117 loc) 路 5.82 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
require("dotenv").config();
const fs = require("fs");
const { REST } = require("@discordjs/rest");
const { Client, GatewayIntentBits, Partials, Collection, Routes } = require("discord.js");
const { Player } = require("discord-player");
const { DefaultExtractors } = require("@discord-player/extractor");
const { YoutubeiExtractor } = require("discord-player-youtubei");
client = new Client({
intents: [
//Sets the necessary intents which discord requires
GatewayIntentBits.Guilds,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.AutoModerationExecution,
],
partials: [Partials.GuildMember, Partials.User, Partials.Message, Partials.Channel, Partials.Reaction],
});
//Added logging for exceptions and rejection
process.on("uncaughtException", async function (err) {
var date = new Date();
console.log(`Caught Exception: ${err.stack}\n`);
fs.appendFileSync("exception.txt", `${date.toGMTString()}: ${err.stack}\n`);
});
process.on("unhandledRejection", async function (err) {
var date = new Date();
console.log(`Caught Rejection: ${err.stack}\n`);
fs.appendFileSync("rejection.txt", `${date.toGMTString()}: ${err.stack}\n`);
});
//Discord-Player initialisation
const defaultConsts = require(`./utils/defaultConsts`);
const player = new Player(client, {
ytdlOptions: defaultConsts.ytdlOptions,
});
player.extractors.loadMulti(DefaultExtractors);
player.extractors.register(YoutubeiExtractor, {
authentication: process.env.YT_CREDENTIALS ? process.env.YT_CREDENTIALS : null,
});
//Initialise commands through JSON
const commands = [];
client.commands = new Collection(); //Creates new command collection
const { createI18n, FALLBACK_LOCALE } = require("./utils/i18n");
const { localizeSlashCommands } = require("./utils/slashLocalizations");
fs.readdirSync("./commands/").forEach((dir) => {
const commandFiles = fs.readdirSync(`./commands/${dir}`).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
//For each file, retrieve name/desc and push it as JSON
const command = require(`./commands/${dir}/${file}`);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
});
//Register all of the commands
client.once("clientReady", async function () {
console.log(`[ELITE_CONFIG] Loading Configuration... (Config Version: ${process.env.CFG_VERSION || "N/A"})`);
const rest = new REST({ version: "10" }).setToken(process.env.TOKEN);
const primaryLocale = process.env.PRIMARY_LOCALE || FALLBACK_LOCALE;
const localeMode = String(process.env.LOCALE_MODE || "global").toLowerCase();
const resolvedLocaleMode = ["global", "user"].includes(localeMode) ? localeMode : "global";
const i18n = createI18n({ fallbackLocale: primaryLocale });
const localizedCommands = localizeSlashCommands(commands, i18n, primaryLocale, resolvedLocaleMode);
try {
await rest.put(Routes.applicationCommands(client.user.id), { body: localizedCommands });
console.log("[ELITE_CMDS] Commands registered (production)!");
} catch (err) {
console.error(err);
}
});
const eventFiles = fs.readdirSync("./events").filter((file) => file.endsWith(".js")); //Searches all .js files
for (const file of eventFiles) {
//For each file, check if the event is .once or .on and execute it as specified within the event file itself
const event = require(`./events/${file}`);
if (event.skipDiscordEventRegistration) continue;
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, commands));
} else {
client.on(event.name, (...args) => event.execute(...args, commands));
}
}
//Authenticate with Discord via .env passed token
if (!process.env.TOKEN) {
console.log(
`[ELITE_ERROR] The .env file could not be found/doesn't exist. Have you followed the setup instructions correctly (https://github.com/ThatGuyJacobee/Elite-Music) to ensure that you have configured your environment correctly?`,
);
process.exit(0);
}
client.login(process.env.TOKEN).catch((err) => {
console.log(
`[ELITE_ERROR] Bot could not login and authenticate with Discord. Have you populated your .env file with your bot token and copied it over correctly? (Using token: ${process.env.TOKEN})\nError Trace: ${err}`,
);
});
//Verbose logging for debugging purposes
const verbose = process.env.VERBOSE ? process.env.VERBOSE.toLocaleLowerCase() : "none";
if (verbose == "full" || verbose == "normal") {
//Both normal and full verbose logging will log unhandled rejects, uncaught exceptions and warnings to the console
process.on("unhandledRejection", (reason) => console.error(reason));
process.on("uncaughtException", (error) => console.error(error));
process.on("warning", (warning) => console.error(warning));
if (verbose == "full") {
console.log(
`[ELITE_CONFIG] Verbose logging enabled and set to full. This will log everything to the console, including: discord-player debugging, unhandled rejections, uncaught exceptions and warnings to the console.`,
);
//Full verbose logging will also log everything from discord-player to the console
console.log(player.scanDeps());
player.on("debug", console.log).events.on("debug", (_, m) => console.log(m));
} else if (verbose == "normal") {
console.log(
`[ELITE_CONFIG] Verbose logging enabled and set to normal. This will log unhandled rejections, uncaught exceptions and warnings to the console.`,
);
}
} else {
console.log(`[ELITE_CONFIG] Verbose logging is disabled.`);
}