Androz2091 / discord-giveaways

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

error quickmongo #90

Closed Akurejs closed 4 years ago

Akurejs commented 4 years ago

Missing the example of the bot with mongodb, I'm trying to use quickmongo to use the mongodb database but I'm finding a problem with the package

in the index I add this in place of the quick.db application

const { Database } = require("quickmongo");
const db = new Database("mongodb+srv://giveaway:pass@cluster0.phlie.gcp.mongodb.net/giveaway?retryWrites=true&w=majority");

and I get this error at the terminal

(node:2412) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of null
    at GiveawayManagerWithOwnDatabase._init (C:\Users\Italo\Desktop\GiveBotV12\node_modules\discord-giveaways\src\Manager.js:392:22)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:2412) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
misa-coder commented 4 years ago

@Akurejs bruh you just showed ur pass and I'd edit your message.

misa-coder commented 4 years ago

Also error is not originated from here I tried quickmongo and it works fine with me

Akurejs commented 4 years ago

rest assured that access to the database does not exist, it is just an example.

For me it is giving this error when the bot turns on, see a screenshot of my code index

https://i.imgur.com/i179Zna.png

twlite commented 4 years ago

@Akurejs hey, try adding await before every QuickMongo methods. QuickMongo is asynchronous.

Also, db.get("giveaways") is currently null. Set it before calling GiveawaysManager.

Like,

if (await db.get("giveaways") === null) await db.set("giveaways", [])

It is also mentioned in quick.db example. image

Akurejs commented 4 years ago

passed this error now, arrows point to db

if (await db.get("giveaways") === null) await db.set("giveaways", []) ^^ SyntaxError: Unexpected identifier at wrapSafe (internal/modules/cjs/loader.js:1070:16) at Module._compile (internal/modules/cjs/loader.js:1120:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) at Module.load (internal/modules/cjs/loader.js:1000:32) at Function.Module._load (internal/modules/cjs/loader.js:899:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47```

misa-coder commented 4 years ago

Remove that and use

db.get('giveaways').then(data => {
   if(!data) db.set('giveaways', [])
})
Xynnix commented 4 years ago

Hi! Not entirely sure if I fixed the issue or just got something to work??? It doesnt show logs that anything failed when starting since beforehand I had that same issue. I'll embed an image showing what I added to the module Its self. it should help because it works for me And makes sense only because I believe the existing was looking for ongoing giveaways. I could send more screenshots of it working if needed but also note I went and did the same thing stated to replace the quick.db things with quickmongo in index.

Edit: The message doesnt send twice, I just had been mistaken or had two running instances.

edit

Akurejs commented 4 years ago

sorry, but all the methods you told me didn’t work, they all give the same problem

passed this error now, arrows point to db

if (await db.get ("giveaways") === null) await db.set ("giveaways", []) ^^ SyntaxError: Unexpected identifier at wrapSafe (internal / modules / cjs / loader.js: 1070: 16) at Module._compile (internal / modules / cjs / loader.js: 1120: 27) at Object.Module._extensions..js (internal / modules / cjs / loader.js: 1176: 10) at Module.load (internal / modules / cjs / loader.js: 1000: 32) at Function.Module._load (internal / modules / cjs / loader.js: 899: 14) at Function.executeUserEntryPoint [as runMain] (internal / modules / run_main.js: 74: 12) at internal / main / run_main_module.js: 18: 47```

Xynnix commented 4 years ago

Ok, heres a method maybe... I have a test version here of this repo with the changes I've made to manager.js. Maybe try using it and follow something close to this in index.js (or main file unless you shard it) If this doesnt work then apologies.

const { Database } = require("quickmongo");
const db = new Database(process.env.MongoDB)
//replace process.env.MongoDB with your variable?
const { GiveawaysManager } = require("discord-giveaways");
const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {

    // This function is called when the manager needs to get all the giveaway stored in the database.
    async getAllGiveaways(){
        // Get all the giveaway in the database
        return await db.get("giveaways");
    }

    // This function is called when a giveaway needs to be saved in the database (when a giveaway is$
    async saveGiveaway(messageID, giveawayData){
        // Add the new one
        db.push("giveaways", giveawayData);
        // Don't forget to return something!
        return true;
    }

    async editGiveaway(messageID, giveawayData){
        // Gets all the current giveaways
        const giveaways = await db.get("giveaways");
        // Remove the old giveaway from the current giveaways ID
const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageID !== messageID);
        // Push the new giveaway to the array
        newGiveawaysArray.push(giveawayData);
        // Save the updated array
        db.set("giveaways", newGiveawaysArray);
        // Don't forget to return something!
        return true;
    }

    // This function is called when a giveaway needs to be deleted from the database.
    async deleteGiveaway(messageID){
        // Remove the giveaway from the array
        const newGiveawaysArray = await db.get("giveaways").filter((giveaway) => giveaway.messageID $
        // Save the updated array
        db.set("giveaways", newGiveawaysArray);
        // Don't forget to return something!
return true;
    }

};

// Create a new instance of your new class
const manager = new GiveawayManagerWithOwnDatabase(client, {
    storage: false, // Important - use false instead of a storage path
    updateCountdownEvery: 10000,
    default: {
        everyoneMention: false,
        botsCanWin: false,
        exemptPermissions: [],
        embedColor: "#FF0000",
        reaction: "🎉"
    }
});
// We now have a giveawaysManager property to access the manager everywhere!
client.giveawaysManager = manager;
twlite commented 4 years ago

@Akurejs

if (await db.get ("giveaways") === null) await db.set ("giveaways", [])
^^
SyntaxError: Unexpected identifier

ah, here's a simple trick. QuickMongo has events and ready is one of them. Put that code inside quickmongo#ready event:

db.on("ready", async () => {
    if (await db.get("giveaways") === null) await db.set("giveaways", []);
});
twlite commented 4 years ago

I've tested the code just now on my bot and it worked perfectly. Here is the extended GiveawaysManager class:

const { GiveawaysManager } = require("discord-giveaways");
const { Database } = require("quickmongo");
const db = new Database("mongodb://localhost/giveaways");

db.once("ready", async () => {
    if (await db.get("giveaways") === null) await db.set("giveaways", []);
});

class GiveawayManager extends GiveawaysManager {

    async getAllGiveaways() {
        return await db.get("giveaways");
    }

    async saveGiveaway(messageID, giveawayData) {
        await db.push("giveaways", giveawayData);
        return true;
    }

    async editGiveaway(messageID, giveawayData) {
        const giveaways = await db.get("giveaways");
        const newGiveawaysArray = giveaways.filter((giveaway) => giveaway.messageID !== messageID);
        newGiveawaysArray.push(giveawayData);
        await db.set("giveaways", newGiveawaysArray);
        return true;
    }

    async deleteGiveaway(messageID) {
        const data = await db.get("giveaways");
        const newGiveawaysArray = data.filter((giveaway) => giveaway.messageID !== messageID);
        await db.set("giveaways", newGiveawaysArray);
        return true;
    }

}

module.exports = GiveawayManager;
misa-coder commented 4 years ago

Thanks @Snowflake107