2023-09-13 21:12:45 +02:00
|
|
|
import { REST } from "@discordjs/rest";
|
|
|
|
import { Routes } from "discord-api-types/v10";
|
|
|
|
import { Collection, WebhookClient, EmbedBuilder, Client } from 'discord.js';
|
|
|
|
import { inspect } from "util";
|
|
|
|
import fs from "fs";
|
|
|
|
import Sequelize from "sequelize";
|
|
|
|
|
|
|
|
import config from './config.json' assert { type: 'json' };
|
|
|
|
const { token, clientId, guildId, sequelizeCredentials, errorWebhookURL } = config;
|
|
|
|
|
2023-09-04 10:51:57 +02:00
|
|
|
|
2022-11-09 14:44:05 +01:00
|
|
|
const rest = new REST({ version: '10' }).setToken(token);
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-13 21:12:45 +02:00
|
|
|
|
|
|
|
async function deploy_commands(client, loadcommands) {
|
2023-04-24 20:44:17 +02:00
|
|
|
if (typeof loadcommands !="boolean" && loadcommands != null) throw "loadcommands argument needs to be boolean or null";
|
2022-11-09 14:44:05 +01:00
|
|
|
|
|
|
|
const commands = [];
|
|
|
|
client.commands = new Collection();
|
|
|
|
const commandCategories = fs.readdirSync('./commands').filter(file => !file.includes('.'));
|
2023-09-13 21:12:45 +02:00
|
|
|
console.log(`Loading ${commandCategories.toString()} commands...`)
|
2022-11-09 14:44:05 +01:00
|
|
|
for (const category of commandCategories) {
|
2023-09-13 21:12:45 +02:00
|
|
|
const commandFiles = fs.readdirSync(`./commands/${category}`).filter(file => file.endsWith('.mjs'));
|
|
|
|
console.log(`Loading ${commandFiles.toString()}...`)
|
2022-11-09 14:44:05 +01:00
|
|
|
for (const file of commandFiles) {
|
2023-09-13 21:12:45 +02:00
|
|
|
const { default: command } = await import(`./commands/${category}/${file}`);
|
2022-11-09 14:44:05 +01:00
|
|
|
commands.push(command.data);
|
|
|
|
client.commands.set(command.data.name, command);
|
|
|
|
|
|
|
|
console.log(`${category}/${command.data.name} chargé !`);
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 20:44:17 +02:00
|
|
|
if (loadcommands==true){
|
2022-11-09 14:44:05 +01:00
|
|
|
slashCommandLoad(client, commands);
|
2023-04-24 20:44:17 +02:00
|
|
|
console.log("Refreshed slash commands !");
|
2022-11-09 14:44:05 +01:00
|
|
|
}
|
2023-04-24 20:44:17 +02:00
|
|
|
else if(loadcommands==false){//Deletes slash commands
|
|
|
|
slashCommandLoad(client, []);
|
|
|
|
console.log("Deleted slash commands !");
|
2022-11-09 14:44:05 +01:00
|
|
|
}
|
2023-04-24 20:44:17 +02:00
|
|
|
else{
|
|
|
|
console.log("Kept old commands");
|
|
|
|
}
|
|
|
|
};
|
2022-11-09 14:44:05 +01:00
|
|
|
|
2023-09-04 00:30:25 +02:00
|
|
|
|
2022-11-09 14:44:05 +01:00
|
|
|
async function slashCommandLoad(client, commands) {
|
|
|
|
try {
|
|
|
|
console.log('Je commence à actualiser les commandes slash.');
|
|
|
|
await rest.put(
|
|
|
|
Routes.applicationGuildCommands(clientId, guildId),
|
|
|
|
{ body: commands },
|
|
|
|
);
|
|
|
|
console.log('Je viens de terminer de charger les commandes slash.');
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
return client.commands;
|
|
|
|
};
|
|
|
|
|
2023-09-04 00:30:25 +02:00
|
|
|
|
|
|
|
function loadDatabase(client) {
|
2023-09-13 21:12:45 +02:00
|
|
|
console.log("Starting to load database...")
|
2023-09-04 00:30:25 +02:00
|
|
|
const sequelize = new Sequelize('database', sequelizeCredentials.username, sequelizeCredentials.password, {
|
|
|
|
host: 'localhost',
|
|
|
|
dialect: 'sqlite',
|
|
|
|
logging: false,
|
|
|
|
storage: 'database.sqlite',
|
|
|
|
});
|
|
|
|
const blacklistdb = sequelize.define('blacklist', {
|
|
|
|
name: {//id
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
unique: true,
|
|
|
|
},
|
|
|
|
username: Sequelize.STRING,
|
|
|
|
reason: Sequelize.TEXT,
|
|
|
|
timestamp: Sequelize.STRING,
|
|
|
|
moderatorid: Sequelize.STRING
|
|
|
|
});
|
|
|
|
|
|
|
|
const modlog = sequelize.define('sanctions', {
|
|
|
|
name: Sequelize.STRING,//id
|
|
|
|
username: Sequelize.STRING,
|
|
|
|
type: Sequelize.STRING,
|
|
|
|
reason: Sequelize.TEXT,
|
|
|
|
timestamp: Sequelize.STRING,
|
|
|
|
moderatorid: Sequelize.STRING
|
|
|
|
});
|
|
|
|
|
|
|
|
const artists = sequelize.define('artists', {
|
|
|
|
name: Sequelize.STRING,//id
|
|
|
|
emoji: Sequelize.STRING,
|
|
|
|
});
|
|
|
|
|
|
|
|
client.database = {
|
|
|
|
sequelize: sequelize,
|
|
|
|
modlog: modlog,
|
|
|
|
blacklistdb: blacklistdb,
|
|
|
|
artists: artists,
|
|
|
|
};
|
|
|
|
blacklistdb.sync();
|
|
|
|
modlog.sync();
|
|
|
|
artists.sync();
|
2023-09-13 21:12:45 +02:00
|
|
|
console.log("Database loaded !")
|
2023-09-04 00:30:25 +02:00
|
|
|
}
|
|
|
|
|
2023-09-04 10:51:57 +02:00
|
|
|
function loadErrorCatcher(client) {
|
2023-09-13 21:12:45 +02:00
|
|
|
console.log("Loading error catcher...")
|
2023-09-04 10:51:57 +02:00
|
|
|
client.errorCatcherWebhook = new WebhookClient({
|
|
|
|
url: errorWebhookURL,
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
|
|
console.log(reason, "\n", promise);
|
|
|
|
|
|
|
|
const errorEmbed = new EmbedBuilder()
|
|
|
|
.setColor("Red")
|
|
|
|
.setTitle("Unhandled Rejection/Catch")
|
|
|
|
.setURL("https://nodejs.org/api/process.html#event-unhandledrejection")
|
|
|
|
.addFields(
|
|
|
|
{
|
|
|
|
name: "Reason",
|
|
|
|
value: `\`\`\`${inspect(reason, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Promise",
|
|
|
|
value: `\`\`\`${inspect(promise, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.setTimestamp();
|
|
|
|
|
|
|
|
return client.errorCatcherWebhook.send({ embeds: [errorEmbed] });
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("uncaughtException", (err, origin) => {
|
|
|
|
console.log(err, "\n", origin);
|
|
|
|
|
|
|
|
const errorEmbed = new EmbedBuilder()
|
|
|
|
.setColor("Red")
|
|
|
|
.setTitle("Uncaught Exception/Catch")
|
|
|
|
.setURL("https://nodejs.org/api/process.html#event-uncaughtexception")
|
|
|
|
.addFields(
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
value: `\`\`\`${inspect(err, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Origin",
|
|
|
|
value: `\`\`\`${inspect(origin, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.setTimestamp();
|
|
|
|
|
|
|
|
return client.errorCatcherWebhook.send({ embeds: [errorEmbed] });
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("uncaughtExceptionMonitor", (err, origin) => {
|
|
|
|
console.log(err, "\n", origin);
|
|
|
|
|
|
|
|
const errorEmbed = new EmbedBuilder()
|
|
|
|
.setColor("Red")
|
|
|
|
.setTitle("Uncaught Exception Monitor")
|
|
|
|
.setURL(
|
|
|
|
"https://nodejs.org/api/process.html#event-uncaughtexceptionmonitor"
|
|
|
|
)
|
|
|
|
.addFields(
|
|
|
|
{
|
|
|
|
name: "Error",
|
|
|
|
value: `\`\`\`${inspect(err, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Origin",
|
|
|
|
value: `\`\`\`${inspect(origin, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.setTimestamp();
|
|
|
|
|
|
|
|
return client.errorCatcherWebhook.send({ embeds: [errorEmbed] });
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("warning", (warn) => {
|
|
|
|
console.log(warn);
|
|
|
|
|
|
|
|
const errorEmbed = new EmbedBuilder()
|
|
|
|
.setColor("Red")
|
|
|
|
.setTitle("Uncaught Exception Monitor Warning")
|
|
|
|
.setURL("https://nodejs.org/api/process.html#event-warning")
|
|
|
|
.addFields({
|
|
|
|
name: "Warning",
|
|
|
|
value: `\`\`\`${inspect(warn, { depth: 0 }).slice(0, 1000)}\`\`\``,
|
|
|
|
})
|
|
|
|
.setTimestamp();
|
|
|
|
|
|
|
|
return client.errorCatcherWebhook.send({ embeds: [errorEmbed] });
|
|
|
|
});
|
2023-09-13 21:12:45 +02:00
|
|
|
|
|
|
|
console.log("Error catcher loaded !")
|
2023-09-04 10:51:57 +02:00
|
|
|
}
|
|
|
|
|
2023-09-13 21:12:45 +02:00
|
|
|
export { deploy_commands, loadDatabase, loadErrorCatcher }
|