Skip to content

Commit

Permalink
✨ Apache CouchDB - Nano example (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico105 authored Aug 29, 2021
1 parent 1a7ecca commit f3c49dc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ You can use your custom database to save giveaways, instead of the json files (t
- MongoDB
- [Mongoose](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/mongoose.js)
- [QuickMongo](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/quickmongo.js) ⚠️ Not recommended for high giveaway usage, use the `mongoose` example instead
- [Apache CouchDB - Nano](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/nano.js)
- Replit Database ⚠️ Only usable if your bot is hosted on [Replit](https://replit.com/)
- [@replit/database](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/replit.js)
- [Quick.Replit](https://github.com/Androz2091/discord-giveaways/blob/master/examples/custom-databases/quick.replit.js)
Expand Down
80 changes: 80 additions & 0 deletions examples/custom-databases/nano.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const Discord = require('discord.js'),
client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS
]
}),
settings = {
prefix: 'g!',
token: 'Your Discord Bot Token'
};

// Load nano
const nano = require('nano')('http://admin:mypassword@localhost:5984');
let giveawayDB;

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, {
updateCountdownEvery: 10000,
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;

// 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();
})();

client.on('ready', () => {
console.log('I\'m ready!');
});

client.login(settings.token);

0 comments on commit f3c49dc

Please sign in to comment.