159 lines
5.2 KiB
JavaScript
159 lines
5.2 KiB
JavaScript
|
const fs = require('fs');
|
||
|
|
||
|
const { Client, Intents } = require('discord.js');
|
||
|
const client = new Client({
|
||
|
intents: [
|
||
|
Intents.FLAGS.GUILDS,
|
||
|
Intents.FLAGS.GUILD_MEMBERS,
|
||
|
Intents.FLAGS.GUILD_MESSAGE_REACTIONS
|
||
|
]
|
||
|
});
|
||
|
const config = require('./config.json');
|
||
|
client.config = config;
|
||
|
|
||
|
// Load MySQL
|
||
|
const MySQL = require('mysql');
|
||
|
const sql = MySQL.createConnection({
|
||
|
host: 'localhost',
|
||
|
user: 'Your MySQL user',
|
||
|
password: 'Your MySQL password',
|
||
|
database: 'Your MySQL database',
|
||
|
charset: 'utf8mb4' // In order to save emojis in the database correctly
|
||
|
});
|
||
|
sql.connect((err) => {
|
||
|
if (err) {
|
||
|
// Stop the process if we can't connect to the MySQL server
|
||
|
throw new Error('Impossible to connect to MySQL server. Code: ' + err.code);
|
||
|
} else {
|
||
|
console.log('[SQL] Connection to the MySQL server! Connection ID: ' + sql.threadId);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
// Create giveaways table
|
||
|
sql.query(`
|
||
|
CREATE TABLE IF NOT EXISTS \`giveaways\`
|
||
|
(
|
||
|
\`id\` INT(1) NOT NULL AUTO_INCREMENT,
|
||
|
\`message_id\` VARCHAR(20) NOT NULL,
|
||
|
\`data\` JSON NOT NULL,
|
||
|
PRIMARY KEY (\`id\`)
|
||
|
);
|
||
|
`, (err) => {
|
||
|
if (err) console.error(err);
|
||
|
console.log('[SQL] Created table `giveaways`');
|
||
|
});
|
||
|
|
||
|
const { GiveawaysManager } = require('discord-giveaways');
|
||
|
const GiveawaysManagerWithOwnDatabase = class extends GiveawaysManager {
|
||
|
// This function is called when the manager needs to get all giveaways which are stored in the database
|
||
|
async getAllGiveaways() {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
sql.query('SELECT `data` FROM `giveaways`', (err, res) => {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
return reject(err);
|
||
|
}
|
||
|
const giveaways = res.map((row) =>
|
||
|
JSON.parse(row.data, (_, v) => (typeof v === 'string' && /BigInt\("(-?\d+)"\)/.test(v)) ? eval(v) : v)
|
||
|
);
|
||
|
resolve(giveaways);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// This function is called when a giveaway needs to be saved in the database.
|
||
|
async saveGiveaway(messageId, giveawayData) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
sql.query(
|
||
|
'INSERT INTO `giveaways` (`message_id`, `data`) VALUES (?,?)',
|
||
|
[messageId, JSON.stringify(giveawayData, (_, v) => typeof v === 'bigint' ? `BigInt("${v}")` : v)],
|
||
|
(err, res) => {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
return reject(err);
|
||
|
}
|
||
|
resolve(true);
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// This function is called when a giveaway needs to be edited in the database.
|
||
|
async editGiveaway(messageId, giveawayData) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
sql.query(
|
||
|
'UPDATE `giveaways` SET `data` = ? WHERE `message_id` = ?',
|
||
|
[JSON.stringify(giveawayData, (_, v) => typeof v === 'bigint' ? `BigInt("${v}")` : v), messageId],
|
||
|
(err, res) => {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
return reject(err);
|
||
|
}
|
||
|
resolve(true);
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// This function is called when a giveaway needs to be deleted from the database.
|
||
|
async deleteGiveaway(messageId) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
sql.query('DELETE FROM `giveaways` WHERE `message_id` = ?', messageId, (err, res) => {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
return reject(err);
|
||
|
}
|
||
|
resolve(true);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a new instance of your class
|
||
|
const manager = new GiveawaysManagerWithOwnDatabase(cclient, {
|
||
|
default: {
|
||
|
botsCanWin: false,
|
||
|
embedColor: '#FF0000',
|
||
|
embedColorEnd: '#000000',
|
||
|
reaction: '🎉',
|
||
|
}
|
||
|
});
|
||
|
// We now have a giveawaysManager property to access the manager everywhere!
|
||
|
client.giveawaysManager = manager;
|
||
|
|
||
|
/* Load all commands */
|
||
|
client.commands = new Discord.Collection();
|
||
|
fs.readdir('./commands/', (_err, files) => {
|
||
|
files.forEach((file) => {
|
||
|
if (!file.endsWith('.js')) return;
|
||
|
let props = require(`./commands/${file}`);
|
||
|
let commandName = file.split('.')[0];
|
||
|
client.commands.set(commandName, {
|
||
|
name: commandName,
|
||
|
...props
|
||
|
});
|
||
|
client.log(`👌 Command loaded: ${commandName}`);
|
||
|
});
|
||
|
syncroniseSlashCommands(client, client.commands.map((c) => ({
|
||
|
name: c.name,
|
||
|
description: c.description,
|
||
|
options: c.options,
|
||
|
type: 'CHAT_INPUT'
|
||
|
})), {
|
||
|
debug: true
|
||
|
});
|
||
|
});
|
||
|
|
||
|
/* Load all events */
|
||
|
fs.readdir('./events/', (_err, files) => {
|
||
|
files.forEach((file) => {
|
||
|
if (!file.endsWith('.js')) return;
|
||
|
const event = require(`./events/${file}`);
|
||
|
let eventName = file.split('.')[0];
|
||
|
console.log(`👌 Event loaded: ${eventName}`);
|
||
|
client.on(eventName, event.bind(null, client));
|
||
|
delete require.cache[require.resolve(`./events/${file}`)];
|
||
|
});
|
||
|
});
|
||
|
|
||
|
client.login(config.token);
|