npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thenorthsolution/djs-giveaways

v1.0.0

Published

Discord.js v14 Giveaways

Downloads

7

Readme

@thenorthsolution/djs-giveaways

npm bundle size (scoped) GitHub npm (scoped)

A giveaway library for discord.js

Installation

npm i @thenorthsolution/djs-giveaways discord.js

Available Database Adapter

Usage

import { GiveawayManager, MongodbDatabaseAdapter } from '@thenorthsolution/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`);