Androz2091 / discord-giveaways

🎉 Complete framework to facilitate the creation of giveaways using discord.js
https://discord-giveaways.js.org
MIT License
335 stars 127 forks source link

Typescript: How do I use custom database? (mongoose) #502

Closed blacbrue closed 1 year ago

blacbrue commented 1 year ago

Here's my code:

import { GiveawaysManager } from 'discord-giveaways';
import Giveaways from "@hydra/db/Giveaways"

export class giveawayManager extends GiveawaysManager {
    public override async getAllGiveaways() {
        return await Giveaways.model.find().lean().exec()
    }

    public override async saveGiveaway(_messageId: string, giveawayData: any) {
        await Giveaways.model.create(giveawayData)
        return true;
    }

    public override async editGiveaway(messageId: string, giveawayData: any) {
        await Giveaways.model.updateOne({ messageId }, giveawayData).exec()
        return true;
    }

    public override async deleteGiveaway(messageId: string) {
        await Giveaways.model.deleteOne({ messageId }).exec()
        return true;
    }
}
Giveaways Model/Schema code ```ts import { Schema, model, SchemaTypes } from "mongoose"; import { ModelWithCache } from "cache-mongodb"; const GiveawaySchema = new Schema( { messageId: String, channelId: String, guildId: String, startAt: Number, endAt: Number, ended: Boolean, winnerCount: Number, prize: String, messages: { giveaway: String, giveawayEnded: String, title: String, inviteToParticipate: String, drawing: String, dropMessage: String, winMessage: SchemaTypes.Mixed, embedFooter: SchemaTypes.Mixed, noWinner: String, winners: String, endedAt: String, hostedBy: String }, thumbnail: String, image: String, hostedBy: String, winnerIds: { type: [String], default: undefined }, reaction: SchemaTypes.Mixed, botsCanWin: Boolean, embedColor: SchemaTypes.Mixed, embedColorEnd: SchemaTypes.Mixed, exemptPermissions: { type: [], default: undefined }, exemptMembers: String, bonusEntries: String, extraData: SchemaTypes.Mixed, lastChance: { enabled: Boolean, content: String, threshold: Number, embedColor: SchemaTypes.Mixed }, pauseOptions: { isPaused: Boolean, content: String, unPauseAfter: Number, embedColor: SchemaTypes.Mixed, durationAfterPause: Number, infiniteDurationText: String }, isDrop: Boolean, allowedMentions: { parse: { type: [String], default: undefined }, users: { type: [String], default: undefined }, roles: { type: [String], default: undefined } } }, { id: false } ) const giveawayModel = model("giveaway", GiveawaySchema) export default new ModelWithCache(giveawayModel) ```

It's giving me an error. It says that: Property 'getAllGiveaways' in type 'giveawayManager' is not assignable to the same property in base type 'GiveawaysManager<any>'.

When I run the bot this I get this error:

D:\dirname\node_modules\discord-giveaways\src\Manager.js:685
        rawGiveaways.forEach((giveaway) => this.giveaways.push(new Giveaway(this, giveaway)));
                     ^
TypeError: Cannot read properties of undefined (reading 'forEach')
    at giveawayManager._init (D:\dirname\node_modules\discord-giveaways\src\Manager.js:685:22)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

How do I solve this error? Any help would be appreciated. Thanks!