April/functions.mjs

234 lines
7.2 KiB
JavaScript
Raw Permalink Normal View History

2023-10-13 15:04:11 +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";
2023-10-13 15:04:11 +02:00
import config from "./config.json" assert { type: "json" };
const { token, clientId, guildId, sequelizeCredentials, errorWebhookURL } =
config;
2023-10-13 15:04:11 +02:00
const rest = new REST({ version: "10" }).setToken(token);
2023-09-13 21:12:45 +02:00
async function deploy_commands(client, loadcommands) {
2023-10-13 15:04:11 +02:00
if (typeof loadcommands != "boolean" && loadcommands != null)
throw "loadcommands argument needs to be boolean or null";
const commands = [];
client.commands = new Collection();
2023-10-13 15:04:11 +02:00
const commandCategories = fs
.readdirSync("./commands")
.filter((file) => !file.includes("."));
console.log(`Loading ${commandCategories.toString()} commands...`);
for (const category of commandCategories) {
2023-10-13 15:04:11 +02:00
const commandFiles = fs
.readdirSync(`./commands/${category}`)
.filter((file) => file.endsWith(".mjs"));
console.log(`Loading ${commandFiles.toString()}...`);
for (const file of commandFiles) {
2023-10-13 15:04:11 +02:00
const { default: command } = await import(
`./commands/${category}/${file}`
);
commands.push(command.data);
client.commands.set(command.data.name, command);
console.log(`${category}/${command.data.name} chargé !`);
}
}
2023-10-13 15:04:11 +02:00
if (loadcommands == true) {
slashCommandLoad(client, commands);
console.log("Refreshed slash commands !");
2023-10-13 15:04:11 +02:00
} else if (loadcommands == false) {
//Deletes slash commands
slashCommandLoad(client, []);
console.log("Deleted slash commands !");
2023-10-13 15:04:11 +02:00
} else {
console.log("Kept old commands");
}
2023-10-13 15:04:11 +02:00
}
async function slashCommandLoad(client, commands) {
try {
2023-10-13 15:04:11 +02:00
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-10-13 15:04:11 +02:00
}
function loadDatabase(client) {
console.log("Starting to load database...");
2023-10-13 15:04:11 +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,
2023-10-13 15:04:11 +02:00
moderatorid: Sequelize.STRING,
});
2023-10-13 15:04:11 +02:00
const modlog = sequelize.define("sanctions", {
name: Sequelize.STRING, //id
username: Sequelize.STRING,
type: Sequelize.STRING,
reason: Sequelize.TEXT,
timestamp: Sequelize.STRING,
2023-10-13 15:04:11 +02:00
moderatorid: Sequelize.STRING,
});
2023-10-13 15:04:11 +02:00
const artists = sequelize.define("artists", {
name: Sequelize.STRING, //id
emoji: Sequelize.STRING,
});
2023-10-13 15:04:11 +02:00
client.database = {
sequelize: sequelize,
modlog: modlog,
blacklistdb: blacklistdb,
artists: artists,
};
blacklistdb.sync();
modlog.sync();
artists.sync();
console.log("Database loaded !");
}
function loadErrorCatcher(client) {
console.log("Loading error catcher...");
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")
2023-10-13 15:04:11 +02:00
.setURL(
"https://nodejs.org/api/process.html#event-unhandledrejection",
)
.addFields(
{
name: "Reason",
2023-10-13 15:04:11 +02:00
value: `\`\`\`${inspect(reason, { depth: 0 }).slice(
0,
1000,
)}\`\`\``,
},
{
name: "Promise",
2023-10-13 15:04:11 +02:00
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")
2023-10-13 15:04:11 +02:00
.setURL(
"https://nodejs.org/api/process.html#event-uncaughtexception",
)
.addFields(
{
name: "Error",
2023-10-13 15:04:11 +02:00
value: `\`\`\`${inspect(err, { depth: 0 }).slice(
0,
1000,
)}\`\`\``,
},
{
name: "Origin",
2023-10-13 15:04:11 +02:00
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(
2023-10-13 15:04:11 +02:00
"https://nodejs.org/api/process.html#event-uncaughtexceptionmonitor",
)
.addFields(
{
name: "Error",
2023-10-13 15:04:11 +02:00
value: `\`\`\`${inspect(err, { depth: 0 }).slice(
0,
1000,
)}\`\`\``,
},
{
name: "Origin",
2023-10-13 15:04:11 +02:00
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",
2023-10-13 15:04:11 +02:00
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-10-13 15:04:11 +02:00
export { deploy_commands, loadDatabase, loadErrorCatcher };