Advertisement
Nico105

manager example

May 4th, 2021
1,534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Connect to the database
  2. const mongoose = require('mongoose');
  3. mongoose.connect('mongodb://localhost/giveaways', { useFindAndModify: false });
  4. const db = mongoose.connection;
  5.  
  6. // Check the connection
  7. db.on('error', console.error.bind(console, 'Connection error:'));
  8. db.once('open', () => {
  9.     console.log('Connected to MongoDB.');
  10. });
  11.  
  12. // Create the schema for giveaways
  13. const giveawaySchema = new mongoose.Schema({
  14.     messageID: String,
  15.     channelID: String,
  16.     guildID: String,
  17.     startAt: Number,
  18.     endAt: Number,
  19.     ended: Boolean,
  20.     winnerCount: Number,
  21.     prize: String,
  22.     messages: {
  23.         giveaway: String,
  24.         giveawayEnded: String,
  25.         inviteToParticipate: String,
  26.         timeRemaining: String,
  27.         winMessage: String,
  28.         embedFooter: String,
  29.         noWinner: String,
  30.         winners: String,
  31.         endedAt: String,
  32.         hostedBy: String,
  33.         units: {
  34.             seconds: String,
  35.             minutes: String,
  36.             hours: String,
  37.             days: String,
  38.             pluralS: Boolean,
  39.         },
  40.     },
  41.     hostedBy: String,
  42.     winnerIDs: [String],
  43.     reaction: mongoose.Mixed,
  44.     botsCanWin: Boolean,
  45.     embedColor: mongoose.Mixed,
  46.     embedColorEnd: mongoose.Mixed,
  47.     exemptPermissions: [],
  48.     exemptMembers: String,
  49.     bonusEntries: String,
  50.     extraData: mongoose.Mixed,
  51.     lastChance: {
  52.         enabled: Boolean,
  53.         content: String,
  54.         threshold: Number,
  55.         embedColor: mongoose.Mixed
  56.     }
  57. });
  58.  
  59. // Create the model
  60. const giveawayModel = mongoose.model('giveaways', giveawaySchema);
  61.  
  62. const { GiveawaysManager } = require('discord-giveaways');
  63. const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
  64.     // This function is called when the manager needs to get all giveaways which are stored in the database.
  65.     async getAllGiveaways() {
  66.         // Get all giveaways from the database. We fetch all documents by passing an empty condition.
  67.         return await giveawayModel.find({});
  68.     }
  69.  
  70.     // This function is called when a giveaway needs to be saved in the database.
  71.     async saveGiveaway(messageID, giveawayData) {
  72.         // Add the new giveaway to the database
  73.         await giveawayModel.create(giveawayData);
  74.         // Don't forget to return something!
  75.         return true;
  76.     }
  77.  
  78.     // This function is called when a giveaway needs to be edited in the database.
  79.     async editGiveaway(messageID, giveawayData) {
  80.         // Find by messageID and update it
  81.         await giveawayModel.findOneAndUpdate({ messageID: messageID }, giveawayData).exec();
  82.         // Don't forget to return something!
  83.         return true;
  84.     }
  85.  
  86.     // This function is called when a giveaway needs to be deleted from the database.
  87.     async deleteGiveaway(messageID) {
  88.         // Find by messageID and delete it
  89.         await giveawayModel.findOneAndDelete({ messageID: messageID }).exec();
  90.         // Don't forget to return something!
  91.         return true;
  92.     }
  93.  
  94.     generateMainEmbed(giveaway) {
  95.         const embed = new Discord.MessageEmbed();
  96.         embed
  97.             .setAuthor(giveaway.prize)
  98.             .setColor(giveaway.embedColor)
  99.             .setFooter(`${giveaway.winnerCount} ${giveaway.messages.winners}`)
  100.             .setDescription(
  101.                 giveaway.messages.inviteToParticipate +
  102.                     '\n' +
  103.                     giveaway.remainingTimeText +
  104.                     '\n' +
  105.                     (giveaway.hostedBy ? giveaway.messages.hostedBy.replace('{user}', giveaway.hostedBy) : '')
  106.             )
  107.             .setTimestamp(new Date(giveaway.endAt).toISOString());
  108.         return embed;
  109.     }
  110. };
  111.  
  112. const manager = new GiveawaysManagerWithOwnDatabase(client, {
  113.       storage: './giveaways.json',
  114.       updateCountdownEvery: 10000,
  115.       hasGuildMembersIntent: false,
  116.       default: {
  117.           botsCanWin: false,
  118.           embedColor: '#F1C40F',
  119.           embedColorEnd: '#FF0000',
  120.           reaction: '🎉'
  121.       }
  122.   })
  123.  
  124.  
  125. client.giveawaysManager = manager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement