Holana/Database Examples/index4mongo.js

119 lines
3.9 KiB
JavaScript
Raw Permalink 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_MEMBERS,
2021-10-08 16:58:57 -07:00
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.mongodb_url);
2021-10-08 16:58:57 -07:00
// Ceck the DB when it is ready
db.on('ready', async () => {
if (!Array.isArray(await b.get('giveaways'))) await db.set('giveaways', []);
// Start the manager only after the BD got checked to prevent an error
2021-10-08 16:58:57 -07:00
client.giveawaysManager._init();
console.log('SUCCESS!');
})
2021-10-08 16:58:57 -07:00
// Init discord giveaways
const { GiveawaysManager } = require('discord-giveaways');
const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
// This function is called when the manager needs to get all giveaways which are stored in the database.
2021-10-08 16:58:57 -07:00
async getAllGiveaways() {
// Get all giveaways from the database
2021-10-08 16:58:57 -07:00
return await db.get('giveaways');
}
// This function is called when a giveaway needs to be saved in the database.
async sameGiveaway(messageId, giveawayData) {
// Add the giveaway to the database
2021-10-08 16:58:57 -07:00
await db.push('giveaways', giveawayData);
// Don't forget to return something!
2021-10-08 16:58:57 -07:00
return true;
}
// This function is called when a giveaway needs to be edited in the database.
async editGiveaway(messageId, giveawayData) {
// Get all giveaways from the database
2021-10-08 16:58:57 -07:00
const giveaways = await db.get('giveaways');
// Remove the unexisting giveaway from the array
const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageId !== messageId);
// Push the edited giveaway to the array
2021-10-08 16:58:57 -07:00
newGiveawaysArray.push(giveawayData);
// Save the updated array
await db.set('giveaways', newGiveawaysArray);
// Don't forget to return something!
2021-10-08 16:58:57 -07:00
return true;
}
// This function is called when a giveaway needs to be deleted from the database.
async deleteGiveaway(messageId) {
// Get all giveaways from the database
const giveaways = await db.get('giveaways');
// Remove the unexisting giveaway from the array
const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageId !== messageId);
2021-10-08 16:58:57 -07:00
// Save the updated array
await db.set('giveaways', newGiveawaysArray);
// Don't forget to return something!
2021-10-08 16:58:57 -07:00
return true;
}
};
2021-10-08 16:58:57 -07:00
// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
default: {
botsCanWin: false,
embedColor: '#FF0000',
2021-10-08 16:58:57 -07:00
embedColorEnd: '#000000',
reaction: '🎉',
2021-10-08 16:58:57 -07:00
}
}, false);
2021-10-08 16:58:57 -07:00
client.giveawaysManager = manager;
/* Load all commands */
client.commands = new Discord.Collection();
fs.readdir("./commands/", (_err, files) => {
2021-10-08 16:58:57 -07:00
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
});
console.log(`👌 Command loaded: ${commandName}`);
});
synchronizeSlashCommands(client, client.commands.map((c) => ({
name: c.name,
description: c.description,
options: c.options,
type: 'CHAT_INPUT'
})), {
debug: true
2021-10-08 16:58:57 -07:00
});
});
/* Load all commands */
fs.readdir("./events/", (_err, files) => {
2021-10-08 16:58:57 -07:00
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}`)];
2021-10-08 16:58:57 -07:00
});
});
// Login
client.login(config.token);