2.0.2 • Published 3 months ago

@falloutstudios/djs-giveaways v2.0.2

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
3 months ago

Djs Giveaways

npm bundle size (scoped) GitHub npm (scoped)

A giveaway library for discord.js

Installation

npm i @falloutstudios/djs-giveaways discord.js

Available Database Adapter

Usage

// @ts-check
import { GiveawayManager, MongodbDatabaseAdapter } from '@falloutstudios/djs-giveaways';
import { Client, SlashCommandBuilder, userMention } from 'discord.js';
import ms from 'ms';

// The discord bot client
const client = new Client({
    intents: ['Guilds', 'GuildMessages']
});

// The giveaway manager
const giveaways = new GiveawayManager({
    database: new MongodbDatabaseAdapter({
        mongooseConnection: `mongodb://username:password@host:port/database`
    }),
    client
});

client.on('ready', async () => {
    // Slash command
    const command = new SlashCommandBuilder()
        .setName('giveaway')
        .setDescription('Manage giveaways')
        .addSubcommand(start => start
            .setName('start')
            .setDescription('Start a new giveaway')
            .addStringOption(name => name
                .setName('name')
                .setDescription('The giveaway name (Giveaway prize)')
                .setRequired(true)
            )
            .addStringOption(duration => duration
                .setName('duration')
                .setDescription('Giveaway duration')
                .setRequired(true)
            )
            .addNumberOption(winners => winners
                .setName('winners')
                .setDescription('Number of winners')
                .setRequired(true)
            )
        )
        .addSubcommand(end => end
            .setName('end')
            .setDescription('Ends a giveaway')
            .addStringOption(giveaway => giveaway
                .setName('giveaway')
                .setDescription('The giveaway you want to end')
                .setRequired(true)
            )
            .addBooleanOption(cancel => cancel
                .setName('cancel')
                .setDescription('End giveaway without choosing winners')
            )
        )
        .addSubcommand(reroll => reroll
            .setName('reroll')
            .setDescription('Rerolls giveaway winners')
            .addStringOption(giveaway => giveaway
                .setName('giveaway')
                .setDescription('The giveaway you want to end')
                .setRequired(true)
            )
        );

    // Register command globally
    await client.application?.commands.set([command]);
    // Start giveaway listeners
    await giveaways.start();
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isChatInputCommand() || interaction.commandName !== 'giveaway' || !interaction.inCachedGuild() || !interaction.channel) return;

    const subcommand = interaction.options.getSubcommand(true);

    if (subcommand === 'start') {
        const name = interaction.options.getString('name', true);
        const duration = ms(interaction.options.getString('duration', true));
        const winners = interaction.options.getNumber('winners', true);

        await interaction.deferReply({ ephemeral: true });

        const giveaway = await giveaways.createGiveaway({
            channel: interaction.channel,
            endsAt: duration,
            name,
            winnerCount: winners
        });

        const message = await giveaways.fetchGiveawayMessage(giveaway);
        await interaction.editReply(message.url);
    } else if (subcommand === 'end') {
        const giveawayId = interaction.options.getString('giveaway', true);
        const cancel = interaction.options.getBoolean('cancel') || false;

        await interaction.deferReply({ ephemeral: true });

        const giveaway = (await giveaways.database.fetchGiveaways({ filter: { messageId: giveawayId } }))[0];
        if (!giveaway) {
            await interaction.editReply(`Giveaway not found`);
            return;
        }

        await giveaways.endGiveaway(giveaway.id, cancel);
        await interaction.editReply(`Ended giveaway`);
    } else if (subcommand === 'reroll') {
        const giveawayId = interaction.options.getString('giveaway', true);

        await interaction.deferReply({ ephemeral: true });

        const giveaway = (await giveaways.database.fetchGiveaways({ filter: { messageId: giveawayId } }))[0];
        if (!giveaway) {
            await interaction.editReply(`Giveaway not found`);
            return;
        }

        const winners = await giveaways.selectGiveawayEntries(giveaway.id, { winnerCount: giveaway.winnerCount, ignoredUsersId: giveaway.winnersEntryId });
        const message = await giveaways.fetchGiveawayMessage(giveaway);

        if (!winners.selectedEntries.length) {
            await interaction.editReply(`No winners selected from reroll`);
            return;
        }

        await message?.reply(`${winners.selectedEntries.map(e => userMention(e.userId)).join('')} won the reroll!`);
        await interaction.editReply(`Reroll successfull`);
    }
});

client.login(`TOKEN`);
2.0.2

3 months ago

2.0.1

3 months ago

2.0.0

4 months ago

1.2.0

9 months ago

1.3.2

7 months ago

1.3.1

8 months ago

1.3.0

9 months ago

1.2.1

9 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago