Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Connect to the database
- const mongoose = require('mongoose');
- mongoose.connect('mongodb://localhost/giveaways', { useFindAndModify: false });
- 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,
- inviteToParticipate: String,
- timeRemaining: String,
- winMessage: String,
- embedFooter: String,
- noWinner: String,
- winners: String,
- endedAt: String,
- hostedBy: String,
- units: {
- seconds: String,
- minutes: String,
- hours: String,
- days: String,
- pluralS: Boolean,
- },
- },
- hostedBy: String,
- winnerIDs: [String],
- reaction: mongoose.Mixed,
- botsCanWin: Boolean,
- embedColor: mongoose.Mixed,
- embedColorEnd: mongoose.Mixed,
- exemptPermissions: [],
- exemptMembers: String,
- bonusEntries: String,
- extraData: mongoose.Mixed,
- lastChance: {
- enabled: Boolean,
- content: String,
- threshold: Number,
- embedColor: mongoose.Mixed
- }
- });
- // 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({});
- }
- // 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.findOneAndUpdate({ messageID: 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.findOneAndDelete({ messageID: messageID }).exec();
- // Don't forget to return something!
- return true;
- }
- generateMainEmbed(giveaway) {
- const embed = new Discord.MessageEmbed();
- embed
- .setAuthor(giveaway.prize)
- .setColor(giveaway.embedColor)
- .setFooter(`${giveaway.winnerCount} ${giveaway.messages.winners}`)
- .setDescription(
- giveaway.messages.inviteToParticipate +
- '\n' +
- giveaway.remainingTimeText +
- '\n' +
- (giveaway.hostedBy ? giveaway.messages.hostedBy.replace('{user}', giveaway.hostedBy) : '')
- )
- .setTimestamp(new Date(giveaway.endAt).toISOString());
- return embed;
- }
- };
- const manager = new GiveawaysManagerWithOwnDatabase(client, {
- storage: './giveaways.json',
- updateCountdownEvery: 10000,
- hasGuildMembersIntent: false,
- default: {
- botsCanWin: false,
- embedColor: '#F1C40F',
- embedColorEnd: '#FF0000',
- reaction: '🎉'
- }
- })
- client.giveawaysManager = manager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement