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

    Interface BaseAdapter

    Base interface for giveaway storage adapters.

    This interface defines the contract that all storage adapters must implement to provide persistent storage for giveaway data. Adapters can use any storage backend (JSON files, databases, cloud storage, etc.) as long as they implement these methods.

    The interface supports full CRUD operations (Create, Read, Update, Delete) for giveaway data, allowing the GiveawayManager to work with different storage solutions without code changes.

    // Custom Redis adapter implementation
    class RedisAdapter implements BaseAdapter {
    private redis: Redis;

    constructor(redisClient: Redis) {
    this.redis = redisClient;
    }

    async save(data: GiveawayData): Promise<void> {
    await this.redis.set(`giveaway:${data.giveawayId}`, JSON.stringify(data));
    }

    async get(id: string): Promise<GiveawayData | null> {
    const data = await this.redis.get(`giveaway:${id}`);
    return data ? JSON.parse(data) : null;
    }

    // ... implement other methods
    }
    interface BaseAdapter {
        save(data: GiveawayData): Promise<void>;
        get(id: string): Promise<null | GiveawayData>;
        delete(id: string): Promise<void>;
        getAll(): Promise<GiveawayData[]>;
        edit(id: string, data: GiveawayData): Promise<void>;
    }

    Implemented by

    Index

    Methods

    • Saves or updates giveaway data in storage.

      If a giveaway with the same ID already exists, it should be updated. If it doesn't exist, a new record should be created.

      Parameters

      Returns Promise<void>

      A Promise that resolves when the data has been saved

      await adapter.save({
      giveawayId: 'abc123',
      messageId: '987654321',
      channelId: '123456789',
      prize: 'Discord Nitro',
      winnerCount: 1,
      endAt: Date.now() + 86400000,
      ended: false
      });
    • Retrieves a specific giveaway by its ID.

      Parameters

      • id: string

        The unique giveaway ID to retrieve

      Returns Promise<null | GiveawayData>

      A Promise that resolves to the giveaway data or null if not found

      const giveaway = await adapter.get('abc123');
      if (giveaway) {
      console.log(`Found giveaway: ${giveaway.prize}`);
      } else {
      console.log('Giveaway not found');
      }
    • Deletes a giveaway from storage.

      Parameters

      • id: string

        The unique giveaway ID to delete

      Returns Promise<void>

      A Promise that resolves when the giveaway has been deleted

      await adapter.delete('abc123');
      console.log('Giveaway deleted successfully');
    • Retrieves all giveaways from storage.

      This method is used during manager initialization to restore timeouts for active giveaways after a bot restart.

      Returns Promise<GiveawayData[]>

      A Promise that resolves to an array of all giveaway data

      const allGiveaways = await adapter.getAll();
      const activeGiveaways = allGiveaways.filter(g => !g.ended);
      console.log(`Found ${activeGiveaways.length} active giveaways`);
    • Updates an existing giveaway with new data.

      This method is used when editing giveaway details such as prize, winner count, or requirements.

      Parameters

      • id: string

        The unique giveaway ID to update

      • data: GiveawayData

        The new giveaway data to save

      Returns Promise<void>

      A Promise that resolves when the update is complete

      await adapter.edit('abc123', {
      ...existingGiveaway,
      prize: 'Updated Prize Name',
      winnerCount: 3
      });