Better Giveaways - v1.0.0-beta.2
    Preparing search index...

    Class GiveawayManager

    GiveawayManager represents a comprehensive manager for creating and managing Discord giveaways.

    import { Client } from 'discord.js';
    import { GiveawayManager, JSONAdapter } from 'better-giveaways';

    const client = new Client({ intents: ['Guilds', 'GuildMessages', 'GuildMessageReactions'] });
    const adapter = new JSONAdapter('./giveaways.json');

    const giveawayManager = new GiveawayManager(client, adapter, {
    reaction: '🎉',
    botsCanWin: false,
    language: 'en'
    });
    Index

    Constructors

    Properties

    Methods

    Constructors

    • Creates a new GiveawayManager instance.

      Parameters

      • client: Client

        The Discord.js client instance that will be used for bot operations

      • adapter: BaseAdapter

        The storage adapter for persisting giveaway data (JSONAdapter, SequelizeAdapter, etc.)

      • options: GiveawayManagerOptions

        Configuration options for the giveaway manager

        Configuration options for the GiveawayManager instance.

        These options control the global behavior of the giveaway manager, including reaction emoji, bot participation, and localization.

        • reaction: string

          The emoji reaction that users will use to enter giveaways

        • botsCanWin: boolean

          Whether bot accounts are allowed to participate and win giveaways

        • Optionallanguage?: "en" | "cs"

          Language for giveaway messages and UI text

      Returns GiveawayManager

      const giveawayManager = new GiveawayManager(client, adapter, {
      reaction: '🎉',
      botsCanWin: false,
      language: 'en'
      });

    Properties

    Event emitter for giveaway-related events.

    This property provides access to all giveaway events that you can listen to for custom handling.

    Available events:

    • giveawayStarted: Emitted when a new giveaway is started
    • giveawayEnded: Emitted when a giveaway naturally ends
    • giveawayRerolled: Emitted when a giveaway is rerolled
    • giveawayEdited: Emitted when a giveaway is edited
    • reactionAdded: Emitted when someone reacts to a giveaway
    • requirementsFailed: Emitted when a user fails entry requirements
    • requirementsPassed: Emitted when a user passes entry requirements
    // Listen for giveaway events
    giveawayManager.events.on('giveawayStarted', (giveaway) => {
    console.log(`Giveaway started: ${giveaway.prize}`);
    });

    giveawayManager.events.on('giveawayEnded', (giveaway, winners) => {
    console.log(`Giveaway ended! Winners: ${winners.join(', ')}`);
    });

    giveawayManager.events.on('requirementsFailed', (giveaway, user, reason) => {
    console.log(`${user.username} failed requirements: ${reason}`);
    });

    Methods

    • Starts a new giveaway with the specified options.

      This method creates a new giveaway, sends an embed message to the specified channel, adds the reaction emoji, sets up automatic ending, and begins collecting reactions.

      Parameters

      • options: GiveawayOptions

        The configuration for the giveaway

        Configuration options for creating a new giveaway.

        This interface defines all the parameters needed to start a giveaway, including duration, prize details, and entry requirements.

        • channelId: string

          Discord channel ID where the giveaway will be posted

        • prize: string

          Description of the prize being offered

        • winnerCount: number

          Number of winners to select when the giveaway ends

        • duration: number

          Duration of the giveaway in milliseconds

        • Optionalrequirements?: GiveawayRequirements

          Optional entry requirements that participants must meet

      Returns Promise<GiveawayData>

      A Promise that resolves to the created GiveawayData object

      When the channel is not found or is not text-based

      const giveaway = await giveawayManager.start({
      channelId: '123456789',
      prize: 'Discord Nitro',
      winnerCount: 2,
      duration: 24 * 60 * 60 * 1000, // 24 hours
      requirements: {
      requiredRoles: ['987654321'],
      accountAgeMin: Date.now() - (7 * 24 * 60 * 60 * 1000) // 7 days old
      }
      });
    • Internal

      Ends a giveaway and selects winners.

      This method retrieves all reactions from the giveaway message, filters out bots (if configured), selects random winners, updates the embed to show the results, and emits appropriate events.

      Parameters

      • giveawayId: string

        The unique identifier of the giveaway to end

      • rerolled: boolean

        Whether this is a reroll operation (affects which event is emitted)

      Returns Promise<void>

      A Promise that resolves when the giveaway has been ended

      // End a giveaway normally
      await giveawayManager.end('abc123def');

      // The method is also called automatically when the giveaway duration expires

      This method is called automatically when giveaways expire, but can also be called manually

    • Edits an existing giveaway's details.

      This method allows you to modify the prize, winner count, and requirements of an active giveaway. The giveaway message embed will be updated to reflect the changes, and the updated data will be saved to storage.

      Parameters

      • giveawayId: string

        The unique identifier of the giveaway to edit

      • options: GiveawayOptions

        The new giveaway options (prize, winnerCount, requirements)

        Configuration options for creating a new giveaway.

        This interface defines all the parameters needed to start a giveaway, including duration, prize details, and entry requirements.

        • channelId: string

          Discord channel ID where the giveaway will be posted

        • prize: string

          Description of the prize being offered

        • winnerCount: number

          Number of winners to select when the giveaway ends

        • duration: number

          Duration of the giveaway in milliseconds

        • Optionalrequirements?: GiveawayRequirements

          Optional entry requirements that participants must meet

      Returns Promise<void>

      A Promise that resolves when the giveaway has been updated

      When the giveaway is not found or has already ended

      // Edit a giveaway to change the prize and add requirements
      await giveawayManager.edit('abc123def', {
      channelId: '123456789', // Must match original
      prize: 'Discord Nitro + $50 Gift Card', // Updated prize
      winnerCount: 3, // Increased winner count
      duration: 0, // Not used in editing
      requirements: {
      requiredRoles: ['987654321', '876543210'] // Added requirements
      }
      });
    • Internal

      Restores timeouts and reaction collectors for all active giveaways.

      This method is essential for maintaining giveaway functionality after bot restarts. It retrieves all active giveaways from storage and re-establishes their timeouts and reaction collectors. This method is automatically called during construction.

      Returns Promise<void>

      A Promise that resolves when all timeouts have been restored

      // Usually called automatically, but can be called manually if needed
      await giveawayManager.restoreTimeouts();

      This method is called automatically during manager initialization

    • Rerolls the winners of an active giveaway.

      This method allows you to select new winners for a giveaway without ending it permanently. It performs the same winner selection process as the end method but emits a 'giveawayRerolled' event instead of 'giveawayEnded'.

      Parameters

      • giveawayId: string

        The unique identifier of the giveaway to reroll

      Returns Promise<void>

      A Promise that resolves when the reroll has been completed

      // Reroll winners for a giveaway
      await giveawayManager.reroll('abc123def');

      // Listen for the reroll event
      giveawayManager.events.on('giveawayRerolled', (giveaway, newWinners) => {
      console.log(`New winners selected: ${newWinners.join(', ')}`);
      });