Holana/Database Examples/index4mongo.js

106 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-10-08 16:58:57 -07:00
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
});
const config = require('./config.json');
client.config = config;
// Load quickmongo
const { Database } = require('quickmongo');
const db = new Database(`${config.mongo_url}`);
// check the DB when it's ready
db.once('ready', async () => {
if (!Array.isArray(await db.get('giveaways')) === null) await db.set('giveaways', []);
// start the manager only if the db got checked to prevent an error
client.giveawaysManager._init();
console.log('SUCCESS!');
});
// Init discord giveaways
const { GiveawaysManager } = require('discord-giveaways');
class GiveawayManagerWithOwnDatabase extends GiveawaysManager {
// This funtion is called when the manager need to get all the giveaways stored in the database
async getAllGiveaways() {
return await db.get('giveaways');
}
// This function is called when a giveaway needs to be saved in the database (when a giveaway is created or when a giveaway is edited).
async saveGiveaway(messageID, giveawayData) {
// Add the nw one
await db.push('giveaways', giveawayData);
// Can't forget to return something.
return true;
}
async editGiveaway(messageID, giveawayData) {
// Gets all the current giveaways
const giveaways = await db.get('giveaways');
// Remove the old giveaway from the current giveaways ID
const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageID !== messageID);
// Push the new giveaway to the array
newGiveawaysArray.push(giveawayData);
// Save the updated array
await db.set('giveaways', newGiveawaysArray);
// Can't forget to return something.
return true;
}
async deleteGiveaway(messageID) {
// Gets all the current giveaways
const data = await db.get('giveaways');
// Remove the giveaway from the array
const newGiveawaysArray = data.filter((giveaway) => giveaway.messageID !== messageID);
// Save the updated array
await db.set('giveaways', newGiveawaysArray);
// Can't forget to return something.
return true;
}
}
// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
updateCountdownEvery: 10000,
default: {
botsCanWin: false,
embedColor: "#FF0000",
embedColorEnd: '#000000',
reaction: "🎉"
}
}, false)
client.giveawaysManager = manager;
/* 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.commands = new Discord.Collection();
/* Load all commands */
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, props);
console.log(`👌 Command loaded: ${commandName}`);
});
});
// Login
client.login(config.token);