diff --git a/Database Examples/README.md b/Database Examples/README.md deleted file mode 100644 index 7ecfc1e..0000000 --- a/Database Examples/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# Custom/External Database -you can use the pre-built available database example in your bot as your need. Read the following carefully to understand the Custom Databases. - -## Database Setup -```md -SQL Database Setup -- Copy the code from the given file from: ./Database Examples/index4mysql.js. -- Install the mysql package: `npm i mysql`. -- Insert your DB values. -and that's it start the bot and it should work. -``` -```md -MongoDB Setup -- Copy the code from the given file from: ./Database Examples/index4mongo.js. -- Install the quickmongo package: npm i quickmongo -- Add your connection string in the config file with the following variable: "mongo_url": "your-mongo-connection-string" -and that's it start the bot and it should work. -``` \ No newline at end of file diff --git a/Database Examples/index4mongo.js b/Database Examples/index4mongo.js deleted file mode 100644 index 10f9702..0000000 --- a/Database Examples/index4mongo.js +++ /dev/null @@ -1,119 +0,0 @@ -const fs = require('fs'); - -const Discord = require('discord.js'); -const client = new Discord.Client({ - intents: [ - Discord.Intents.FLAGS.GUILDS, - Discord.Intents.FLAGS.GUILD_MEMBERS, - 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); - -// 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 - client.giveawaysManager._init(); - console.log('SUCCESS!'); -}) - -// 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. - async getAllGiveaways() { - // Get all giveaways from the database - 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 - await db.push('giveaways', giveawayData); - // Don't forget to return something! - 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 - 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 - newGiveawaysArray.push(giveawayData); - // Save the updated array - await db.set('giveaways', newGiveawaysArray); - // Don't forget to return something! - 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); - // Save the updated array - await db.set('giveaways', newGiveawaysArray); - // Don't forget to return something! - return true; - } -}; - -// Create a new instance of your new class -const manager = new GiveawayManagerWithOwnDatabase(client, { - default: { - botsCanWin: false, - embedColor: '#FF0000', - embedColorEnd: '#000000', - reaction: '🎉', - } -}, false); - -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 - }); - 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 - }); -}); - -/* Load all commands */ -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}`)]; - }); -}); - -// Login -client.login(config.token); \ No newline at end of file diff --git a/Database Examples/index4mysql.js b/Database Examples/index4mysql.js deleted file mode 100644 index 09a78d5..0000000 --- a/Database Examples/index4mysql.js +++ /dev/null @@ -1,159 +0,0 @@ -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); \ No newline at end of file diff --git a/examples/custom-databases/.eslintrc.json b/examples/custom-databases/.eslintrc.json new file mode 100644 index 0000000..4ea2dac --- /dev/null +++ b/examples/custom-databases/.eslintrc.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-restricted-globals": "off" + } +} \ No newline at end of file diff --git a/examples/custom-databases/enmap.js b/examples/custom-databases/enmap.js new file mode 100644 index 0000000..80cf5f2 --- /dev/null +++ b/examples/custom-databases/enmap.js @@ -0,0 +1,64 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load Enmap +const Enmap = require("enmap"); + +// Create giveaways table +const giveawayDB = new Enmap({ name: "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. + async getAllGiveaways() { + // Get all giveaways from the database + return giveawayDB.fetchEverything().array(); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + giveawayDB.set(messageId, giveawayData); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be edited in the database. + async editGiveaway(messageId, giveawayData) { + // Replace the unedited giveaway with the edited giveaway + giveawayDB.set(messageId, giveawayData); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be deleted from the database. + async deleteGiveaway(messageId) { + // Remove the giveaway from the database + giveawayDB.delete(messageId); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/mongoose.js b/examples/custom-databases/mongoose.js new file mode 100644 index 0000000..3d5c89f --- /dev/null +++ b/examples/custom-databases/mongoose.js @@ -0,0 +1,133 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Connect to the database +const mongoose = require("mongoose"); +mongoose.connect("mongodb://localhost/database"); +const db = mongoose.connection; + +// Check the connection +db.on("error", console.error.bind(console, "Connection error:")); +db.once("open", () => { + console.log("Connected to MongoDB."); +}); + +// Create the schema for giveaways +const giveawaySchema = new mongoose.Schema( + { + messageId: String, + channelId: String, + guildId: String, + startAt: Number, + endAt: Number, + ended: Boolean, + winnerCount: Number, + prize: String, + messages: { + giveaway: String, + giveawayEnded: String, + title: String, + inviteToParticipate: String, + drawing: String, + dropMessage: String, + winMessage: mongoose.Mixed, + embedFooter: mongoose.Mixed, + noWinner: String, + winners: String, + endedAt: String, + hostedBy: String, + }, + thumbnail: String, + image: String, + hostedBy: String, + winnerIds: { type: [String], default: undefined }, + reaction: mongoose.Mixed, + botsCanWin: Boolean, + embedColor: mongoose.Mixed, + embedColorEnd: mongoose.Mixed, + exemptPermissions: { type: [], default: undefined }, + exemptMembers: String, + bonusEntries: String, + extraData: mongoose.Mixed, + lastChance: { + enabled: Boolean, + content: String, + threshold: Number, + embedColor: mongoose.Mixed, + }, + pauseOptions: { + isPaused: Boolean, + content: String, + unPauseAfter: Number, + embedColor: mongoose.Mixed, + durationAfterPause: Number, + infiniteDurationText: String, + }, + isDrop: Boolean, + allowedMentions: { + parse: { type: [String], default: undefined }, + users: { type: [String], default: undefined }, + roles: { type: [String], default: undefined }, + }, + }, + { id: false } +); + +// Create the model +const giveawayModel = mongoose.model("giveaways", giveawaySchema); + +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. + async getAllGiveaways() { + // Get all giveaways from the database. We fetch all documents by passing an empty condition. + return await giveawayModel.find().lean().exec(); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + await giveawayModel.create(giveawayData); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be edited in the database. + async editGiveaway(messageId, giveawayData) { + // Find by messageId and update it + await giveawayModel.updateOne({ messageId }, giveawayData).exec(); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be deleted from the database. + async deleteGiveaway(messageId) { + // Find by messageId and delete it + await giveawayModel.deleteOne({ messageId }).exec(); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/mysql.js b/examples/custom-databases/mysql.js new file mode 100644 index 0000000..2f44e83 --- /dev/null +++ b/examples/custom-databases/mysql.js @@ -0,0 +1,144 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load mysql +const MySQL = require("mysql"); +const sql = MySQL.createConnection({ + host: "localhost", + user: "Your MySQL user", + password: "Your MySQL password", + database: "Your MySQL database name", + charset: "utf8mb4", // In order to save emojis 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] Connected 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 GiveawayManagerWithOwnDatabase = 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 new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/nano.js b/examples/custom-databases/nano.js new file mode 100644 index 0000000..a9969d2 --- /dev/null +++ b/examples/custom-databases/nano.js @@ -0,0 +1,81 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load nano +const nano = require("nano")("http://admin:mypassword@localhost:5984"); +let giveawayDB; + +// Check the DB +(async () => { + if (!(await nano.db.list()).includes("giveaways")) + await nano.db.create("giveaways"); + giveawayDB = nano.use("giveaways"); + // Start the manager only after the DB got checked to prevent an error + client.giveawaysManager._init(); +})(); + +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. + async getAllGiveaways() { + // Get all giveaways from the database + return (await giveawayDB.list({ include_docs: true })).rows.map( + (r) => r.doc + ); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + await giveawayDB.insert(giveawayData, messageId); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be edited in the database. + async editGiveaway(messageId, giveawayData) { + // Get the unedited giveaway from the database + const giveaway = await giveawayDB.get(messageId); + // Edit the giveaway + await giveawayDB.insert({ ...giveaway, ...giveawayData }); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be deleted from the database. + async deleteGiveaway(messageId) { + // Get the giveaway from the database + const giveaway = await giveawayDB.get(messageId); + // Remove the giveaway from the database + await giveawayDB.destroy(messageId, giveaway._rev); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase( + client, + { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, + }, + false +); // ATTENTION: Add "false" in order to not start the manager until the DB got checked, see below +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/quick.db.js b/examples/custom-databases/quick.db.js new file mode 100644 index 0000000..b81acaa --- /dev/null +++ b/examples/custom-databases/quick.db.js @@ -0,0 +1,76 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load quick.db +const db = require("quick.db"); +if (!Array.isArray(db.get("giveaways"))) db.set("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. + async getAllGiveaways() { + // Get all giveaways from the database + return db.get("giveaways"); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + db.push("giveaways", giveawayData); + // Don't forget to return something! + 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 + const giveaways = db.get("giveaways"); + // Remove the unedited giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Push the edited giveaway into the array + newGiveawaysArray.push(giveawayData); + // Save the updated array + db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + 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 = db.get("giveaways"); + // Remove the giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Save the updated array + db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/quick.replit.js b/examples/custom-databases/quick.replit.js new file mode 100644 index 0000000..6eadf2f --- /dev/null +++ b/examples/custom-databases/quick.replit.js @@ -0,0 +1,87 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load quick.replit +const { Database } = require("quick.replit"); +const db = new Database(); + +// Check the DB when it is ready +db.once("ready", async () => { + if (!Array.isArray(await db.get("giveaways"))) await db.set("giveaways", []); + // Start the manager only after the DB got checked to prevent an error + client.giveawaysManager._init(); +}); + +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. + async getAllGiveaways() { + // Get all giveaways from the database + return await db.get("giveaways"); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + await db.push("giveaways", giveawayData); + // Don't forget to return something! + 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 + const giveaways = await db.get("giveaways"); + // Remove the unedited giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Push the edited giveaway into the array + newGiveawaysArray.push(giveawayData); + // Save the updated array + await db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + 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 giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Save the updated array + await db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase( + client, + { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, + }, + false +); // ATTENTION: Add "false" in order to not start the manager until the DB got checked, see below +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/quickmongo.js b/examples/custom-databases/quickmongo.js new file mode 100644 index 0000000..53f23c5 --- /dev/null +++ b/examples/custom-databases/quickmongo.js @@ -0,0 +1,71 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load quickmongo +const { Database } = require("quickmongo"); +const giveawayDB = new Database("mongodb://localhost/database", { + collectionName: "giveaways", +}); + +// Start the manager only after the DB turned ready to prevent an error +giveawayDB.once("ready", () => client.giveawaysManager._init()); + +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. + async getAllGiveaways() { + // Get all giveaways from the database + return (await giveawayDB.all()).map((element) => element.data); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Add the new giveaway to the database + await giveawayDB.set(messageId, giveawayData); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be edited in the database. + async editGiveaway(messageId, giveawayData) { + // Replace the unedited giveaway with the edited giveaway + await giveawayDB.set(messageId, giveawayData); + // Don't forget to return something! + return true; + } + + // This function is called when a giveaway needs to be deleted from the database. + async deleteGiveaway(messageId) { + // Remove the giveaway from the database + await giveawayDB.delete(messageId); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase( + client, + { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, + }, + false +); // ATTENTION: Add "false" in order to not start the manager until the DB got checked, see below +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/custom-databases/replit.js b/examples/custom-databases/replit.js new file mode 100644 index 0000000..6dd8c84 --- /dev/null +++ b/examples/custom-databases/replit.js @@ -0,0 +1,83 @@ +const Discord = require("discord.js"); +const client = new Discord.Client({ + intents: [ + Discord.IntentsBitField.Flags.Guilds, + Discord.IntentsBitField.Flags.GuildMessageReactions, + ], +}); + +// Load Replit Database +const Database = require("@replit/database"); +const db = new Database(); +(async () => { + if (!Array.isArray(await db.get("giveaways"))) await db.set("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. + async getAllGiveaways() { + // Get all giveaways from the database + return await db.get("giveaways"); + } + + // This function is called when a giveaway needs to be saved in the database. + async saveGiveaway(messageId, giveawayData) { + // Get all giveaways from the database + const giveawaysArray = await db.get("giveaways"); + // Push the new giveaway into the array + giveawaysArray.push(giveawayData); + // Save the updated array + await db.set("giveaways", giveawaysArray); + // Don't forget to return something! + 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 + const giveaways = await db.get("giveaways"); + // Remove the unedited giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Push the edited giveaway into the array + newGiveawaysArray.push(giveawayData); + // Save the updated array + await db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + 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 giveaway from the array + const newGiveawaysArray = giveaways.filter( + (giveaway) => giveaway.messageId !== messageId + ); + // Save the updated array + await db.set("giveaways", newGiveawaysArray); + // Don't forget to return something! + return true; + } +}; + +// Create a new instance of your new class +const manager = new GiveawayManagerWithOwnDatabase(client, { + default: { + botsCanWin: false, + embedColor: "#FF0000", + embedColorEnd: "#000000", + reaction: "🎉", + }, +}); +// We now have a giveawaysManager property to access the manager everywhere! +client.giveawaysManager = manager; + +client.on("ready", () => { + console.log("Bot is ready!"); +}); + +client.login(process.env.DISCORD_BOT_TOKEN); \ No newline at end of file diff --git a/examples/simple.js b/examples/simple.js new file mode 100644 index 0000000..e69de29