All Projects β†’ Androz2091 β†’ Discord Giveaways

Androz2091 / Discord Giveaways

Licence: mit
πŸŽ‰ Complete framework to facilitate the creation of giveaways using discord.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Discord Giveaways

Weakauras2
World of Warcraft addon that provides a powerful framework to display customizable graphics on your screen.
Stars: ✭ 731 (+377.78%)
Mutual labels:  framework, discord
Commando
Official command framework for discord.js
Stars: ✭ 434 (+183.66%)
Mutual labels:  framework, discord
Discord Player
🎧 Complete framework to simplify the implementation of music commands using discords.js v12
Stars: ✭ 161 (+5.23%)
Mutual labels:  framework, discord
Discord.js Menu
πŸ’¬ Easily create Discord.js v12 embed menus with reactions and unlimited customizable pages.
Stars: ✭ 89 (-41.83%)
Mutual labels:  framework, discord
Komada
Komada: Croatian for `pieces`, is a modular bot system including reloading modules and easy to use custom commands.
Stars: ✭ 67 (-56.21%)
Mutual labels:  framework, discord
Core
The core YAMDBF Framework
Stars: ✭ 63 (-58.82%)
Mutual labels:  framework, discord
Discord Backup
πŸ“¦ Complete framework to facilitate server backup using discord.js v12
Stars: ✭ 172 (+12.42%)
Mutual labels:  framework, discord
Discord Plus
A sleek, customizable Discord theme.
Stars: ✭ 65 (-57.52%)
Mutual labels:  discord, customizable
Create Discord Bot
Create Discord bots using a simple widget-based framework.
Stars: ✭ 70 (-54.25%)
Mutual labels:  framework, discord
Arikawa
A Golang library and framework for the Discord API.
Stars: ✭ 123 (-19.61%)
Mutual labels:  framework, discord
Discord Musicbot
Very simple discord music bot with the discord.js with Song Name playing. It can able to play music with the song name
Stars: ✭ 148 (-3.27%)
Mutual labels:  discord
Tentacle
A multiplexed p2p network framework that supports custom protocols
Stars: ✭ 150 (-1.96%)
Mutual labels:  framework
Chicagoboss
Erlang web MVC, now featuring Comet
Stars: ✭ 1,825 (+1092.81%)
Mutual labels:  framework
Aerogameframework
AeroGameFramework is a Roblox game framework that makes development easy and fun. The framework is designed to simplify the communication between modules and seamlessly bridge the gap between the server and client.
Stars: ✭ 150 (-1.96%)
Mutual labels:  framework
Carry
ClojureScript application framework.
Stars: ✭ 149 (-2.61%)
Mutual labels:  framework
Discordcrypt
End-To-End File & Message Encryption For Discord
Stars: ✭ 150 (-1.96%)
Mutual labels:  discord
Discord Soundbot
A Soundboard Bot for Discord
Stars: ✭ 148 (-3.27%)
Mutual labels:  discord
Core
CatLib lightweight dependency injection container
Stars: ✭ 148 (-3.27%)
Mutual labels:  framework
Discord
Discord notification channel for Laravel
Stars: ✭ 148 (-3.27%)
Mutual labels:  discord
Ymple Ecommerce
Node js E-commerce Framework powered with Sails.js & Node.js as an Ecommerce Platform Shop Solution
Stars: ✭ 152 (-0.65%)
Mutual labels:  framework

Discord Giveaways

downloadsBadge versionBadge doc

Discord Giveaways is a powerful Node.js module that allows you to easily create giveaways!

Features

  • ⏱️ Easy to use!
  • πŸ”„ Automatic restart after bot crash!
  • πŸ‡«πŸ‡· Support for translations: adapt the strings for your own language!
  • πŸ“ Support for all databases! (default is json)
  • βš™οΈ Very customizable! (prize, duration, winners, ignored permissions, etc...)
  • πŸš€ Super-powerful: start, edit, reroll, end, delete giveaways!
  • πŸ’₯ Events: giveawayEnded, giveawayRerolled, giveawayDeleted, giveawayReactionAdded, giveawayReactionRemoved, endedGiveawayReactionAdded
  • πŸ•ΈοΈ Support for shards!
  • and much more!

Installation

npm install --save discord-giveaways

Examples

You can read this example bot on Github: giveaways-bot

Launch of the module

const Discord = require('discord.js'),
    client = new Discord.Client(),
    settings = {
        prefix: 'g!',
        token: 'Your Discord Token'
    };

// Requires Manager from discord-giveaways
const { GiveawaysManager } = require('discord-giveaways');
// Starts updating currents giveaways
const manager = new GiveawaysManager(client, {
    storage: './giveaways.json',
    updateCountdownEvery: 10000,
    hasGuildMembersIntent: false,
    default: {
        botsCanWin: false,
        exemptPermissions: ['MANAGE_MESSAGES', 'ADMINISTRATOR'],
        embedColor: '#FF0000',
        reaction: 'πŸŽ‰'
    }
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;

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

client.login(settings.token);

After that, giveaways that are not yet completed will start to be updated again and new giveaways can be started. You can pass an options object to customize the giveaways. Here is a list of them:

  • client: the discord client (your discord bot instance)
  • options.storage: the json file that will be used to store giveaways
  • options.updateCountdownEvery: the number of milliseconds it will take to update the timers
  • options.endedGiveawaysLifetime: the number of milliseconds that a giveaway has been ended after which it can get delted from the DB
  • options.hasGuildMembersIntent: whether the bot has access to the GUILD_MEMBERS intent. It works without, but it will be faster with
  • options.default.botsCanWin: whether the bots can win a giveaway
  • options.default.exemptPermissions: an array of discord permissions. Members who have at least one of these permissions will not be able to win a giveaway even if they react to it.
  • options.default.embedColor: a hexadecimal color for the embeds of giveaways.
  • options.default.embedColorEnd: a hexadecimal color the embeds of giveaways when they are ended.
  • options.default.reaction: the reaction that users will have to react to in order to participate!

Start a giveaway

client.on('message', (message) => {
    const ms = require('ms'); // npm install ms
    const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === 'start-giveaway') {
        // g!start-giveaway 2d 1 Awesome prize!
        // will create a giveaway with a duration of two days, with one winner and the prize will be "Awesome prize!"

        client.giveawaysManager.start(message.channel, {
            time: ms(args[0]),
            prize: args.slice(2).join(' '),
            winnerCount: parseInt(args[1])
        }).then((gData) => {
            console.log(gData); // {...} (messageid, end date and more)
        });
        // And the giveaway has started!
    }
});
  • options.time: the giveaway duration.
  • options.prize: the giveaway prize.
  • options.hostedBy: the user who hosts the giveaway.
  • options.winnerCount: the number of giveaway winners.
  • options.winnerIDs: the IDs of the giveaway winners. ⚠ You do not have to and would not even be able to set this as a start option! The array only gets filled when the giveaway ends or is rerolled!
  • options.botsCanWin: whether the bots can win a giveaway.
  • options.exemptPermissions: an array of discord permissions. Members who have at least one of these permissions will not be able to win a giveaway even if they react to it.
  • options.embedColor: a hexadecimal color for the embeds of giveaways.
  • options.embedColorEnd: a hexadecimal color the embeds of giveaways when they are ended.
  • options.reaction: the reaction that users will have to react to in order to participate.
  • options.extraData: Extra data which you want to save regarding this giveaway. You can access it from the giveaway object using giveaway.extraData.

This allows you to start a new giveaway. Once the start() function is called, the giveaway starts and you only have to observe the result, the package does the rest!

Fetch the giveaways

// The list of all the giveaways
let allGiveaways = client.giveawaysManager.giveaways; // [ {Giveaway}, {Giveaway} ]

// The list of all the giveaways on the server with ID "1909282092"
let onServer = client.giveawaysManager.giveaways.filter((g) => g.guildID === '1909282092');

// The list of the current giveaways (not ended)
let notEnded = client.giveawaysManager.giveaways.filter((g) => !g.ended);

Reroll a giveaway

client.on('message', (message) => {
    const ms = require('ms'); // npm install ms
    const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === 'reroll') {
        let messageID = args[0];
        client.giveawaysManager.reroll(messageID).then(() => {
            message.channel.send('Success! Giveaway rerolled!');
        }).catch((err) => {
            message.channel.send('No giveaway found for ' + messageID + ', please check and try again');
        });
    }
});

options.winnerCount: the number of winners to pick.

Edit a giveaway

client.on('message', (message) => {
    const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === 'edit') {
        let messageID = args[0];
        client.giveawaysManager.edit(messageID, {
            newWinnerCount: 3,
            newPrize: 'New Prize!',
            addTime: 5000
        }).then(() => {
            // here, we can calculate the time after which we are sure that the lib will update the giveaway
            const numberOfSecondsMax = client.giveawaysManager.options.updateCountdownEvery / 1000;
            message.channel.send('Success! Giveaway will updated in less than ' + numberOfSecondsMax + ' seconds.');
        }).catch((err) => {
            message.channel.send('No giveaway found for ' + messageID + ', please check and try again');
        });
    }
});

options.newWinnerCount: the new number of winners.
options.newPrize: the new prize.
options.addTime: the number of milliseconds to add to the giveaway duration.
options.setEndTimestamp: the timestamp of the new end date. Date.now()+1000.

⚠️ Tips: to reduce giveaway time, define addTime with a negative number! For example addTime: -5000 will reduce giveaway time by 5 seconds!

Delete a giveaway

client.on('message', (message) => {
    const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === 'delete') {
        let messageID = args[0];
        client.giveawaysManager.delete(messageID).then(() => {
            message.channel.send('Success! Giveaway deleted!');
        })
        .catch((err) => {
            message.channel.send('No giveaway found for ' + messageID + ', please check and try again');
        });
    }
});

When you use the delete function, the giveaway data and the message of the giveaway are deleted. You cannot restore a giveaway once you have deleted it.

πŸ‡«πŸ‡· Translation

You can also pass a messages parameter for start() function, if you want to translate the bot text :

  • options.messages.giveaway: the message that will be displayed above the embeds.
  • options.messages.giveawayEnded: the message that will be displayed above the embeds when the giveaway is ended.
  • options.messages.timeRemaining: the message that displays the remaining time (the timer).
  • options.messages.inviteToParticipate: the message that invites users to participate.
  • options.messages.winMessage: the message that will be displayed to congratulate the winner(s) when the giveaway is ended.
  • options.messages.embedFooter: the message displayed at the bottom of the embeds.
  • options.messages.noWinner: the message that is displayed if no winner can be drawn.
  • options.messages.winners: simply the word "winner" in your language.
  • options.messages.endedAt: simply the words "Ended at" in your language.
  • options.messages.units.seconds: simply the word "seconds" in your language.
  • options.messages.units.minutes: simply the word "minutes" in your language.
  • options.messages.units.hours: simply the word "hours" in your language.
  • options.messages.units.days: simply the word "days" in your language.

Note: units should be in the plural.

For example :

client.giveawaysManager.start(message.channel, {
    time: ms(args[0]),
    prize: args.slice(2).join(' '),
    winnerCount: parseInt(args[1]),
    messages: {
        giveaway: '@everyone\n\nπŸŽ‰πŸŽ‰ **GIVEAWAY** πŸŽ‰πŸŽ‰',
        giveawayEnded: '@everyone\n\nπŸŽ‰πŸŽ‰ **GIVEAWAY ENDED** πŸŽ‰πŸŽ‰',
        timeRemaining: 'Time remaining: **{duration}**!',
        inviteToParticipate: 'React with πŸŽ‰ to participate!',
        winMessage: 'Congratulations, {winners}! You won **{prize}**!\n{messageURL}',
        embedFooter: 'Giveaways',
        noWinner: 'Giveaway cancelled, no valid participations.',
        hostedBy: 'Hosted by: {user}',
        winners: 'winner(s)',
        endedAt: 'Ended at',
        units: {
            seconds: 'seconds',
            minutes: 'minutes',
            hours: 'hours',
            days: 'days',
            pluralS: false // Not needed, because units end with a S so it will automatically removed if the unit value is lower than 2
        }
    }
});

And for the reroll() function:

client.giveawaysManager
    .reroll(messageID, {
        messages: {
            congrat: 'πŸŽ‰ New winner(s) : {winners}! Congratulations!\n{messageURL}',
            error: 'No valid participations, no winners can be chosen!'
        }
    })
    .catch((err) => {
        message.channel.send('No giveaway found for ' + messageID + ', please check and try again');
    });

options.messages.congrat: the congratulatory message.
options.messages.error: the error message if there is no valid participations.

Custom database

You can use your custom database to save giveaways, instead of the json files (the "database" by default for discord-giveaways). For this, you will need to extend the GiveawaysManager class, and replace some methods with your custom ones. There are 4 methods you will need to replace:

  • getAllGiveaways: this method returns an array of stored giveaways.
  • saveGiveaway: this method stores a new giveaway in the database.
  • editGiveaway: this method edits a giveaway already stored in the database.
  • deleteGiveaway: this method deletes a giveaway from the database (permanently).

All the methods should be asynchronous to return a promise.

Here is an example, using quick.db, a Sqlite database. The comments in the code below are very important to understand how it works!

Other examples:

const Discord = require('discord.js'),
    client = new Discord.Client(),
    settings = {
        prefix: 'g!',
        token: 'Your Discord Token'
    };

// Load quick.db - it's an example of custom database, you can use MySQL, PostgreSQL, etc...
const db = require('quick.db');
if (!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 the giveaway stored in the database.
    async getAllGiveaways() {
        // Get all the giveaway in the database
        return db.get('giveaways');
    }

    // This function is called when a giveaway needs to be saved in the database (when a giveaway is created or when a giveaway is edited).
    async saveGiveaway(messageID, giveawayData) {
        // Add the new one
        db.push('giveaways', giveawayData);
        // Don't forget to return something!
        return true;
    }

    async editGiveaway(messageID, giveawayData) {
        // Gets all the current giveaways
        const giveaways = db.get('giveaways');
        // Remove the old giveaway from the current giveaways ID
        const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageID !== messageID);
        // Push the new giveaway to 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) {
        // Remove the giveaway from the array
        const newGiveawaysArray = db.get('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, {
    storage: false, // Important - use false instead of a storage path
    updateCountdownEvery: 10000,
    default: {
        botsCanWin: false,
        exemptPermissions: ['MANAGE_MESSAGES', 'ADMINISTRATOR'],
        embedColor: '#FF0000',
        reaction: 'πŸŽ‰'
    }
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;

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

client.login(settings.token);

Support shards

To make discord-giveaways working with shards, you will need to extend the GiveawaysManager class and to update the refreshStorage() method. This method should call the getAllGiveaways() method for every shard, so all the GiveawaysManager synchronize their cache with the updated database.

const Discord = require('discord.js'),
    client = new Discord.Client(),
    settings = {
        prefix: 'g!',
        token: 'Your Discord Token'
    };

// Extends the GiveawaysManager class and update the refreshStorage method
const { GiveawaysManager } = require('discord-giveaways');
const GiveawayManagerWithShardSupport = class extends GiveawaysManager {
    // Refresh storage method is called when the database is updated on one of the shards
    async refreshStorage() {
        // This should make all shard refreshing their cache with the updated database
        return client.shard.broadcastEval(() => this.giveawaysManager.getAllGiveaways());
    }
};

// Create a new instance of your new class
const manager = new GiveawayManagerWithShardSupport(client, {
    storage: './storage.json',
    updateCountdownEvery: 10000,
    default: {
        botsCanWin: false,
        exemptPermissions: ['MANAGE_MESSAGES', 'ADMINISTRATOR'],
        embedColor: '#FF0000',
        reaction: 'πŸŽ‰'
    }
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;

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

client.login(settings.token);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].