Androz2091 / discord-giveaways

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

Mongoose - The schema don't get deleted on db #466

Closed TheDogHusky closed 2 years ago

TheDogHusky commented 2 years ago
No issue like this one created **Describe the bug** A clear and concise description of what the bug is. I used the example for mongodb, just edited it for my actual structure (file of model: ./models/giveaway.js (and it works), file of manager: ./utils/giveawaymanager.js (it still work on exports and all)). When I start a giveaway, it creates a document for the giveaway, everything's fine. Then, when it ends, or I end it manually, the document don't get deleted. (btw when i delete it manually i get the error that it can't find a giveaway with this message id after ending it manually and when it tries to end it automatically.) **Error** A screenshot or the text block of the error if there is one. No error for the document deletion, but the error for the auto end it "Ending aborted because giveaway with message Id 984443503364935756could not get edited. Try later!" even if a giveaway exists on db and with the message id. **To Reproduce** Steps to reproduce the behavior: 1 - Make a file named giveawaymanager.js where you put the example code 2 - Make a file named giveaway.js for the model, enter the model and export it (module.exports = mongoose.model("Giveaways", giveawayschema);) 3 - In the giveawaymanager.js file put this line: `const giveawayModel = require('./giveaway');` 4 - In index.js, make a client, a giveaway manager and a mongoose db connection working. 5 - Start a giveaway, end it manually and the document don't get deleted + it gets error after trying to delete it a the end time. **Expected behavior** A clear and concise description of what you expected to happen. It's expected to delete the document on mongodb database + not supposed to have error if i ended the giveaway manually before. **Screenshots** If applicable, add screenshots to help explain your problem. https://imgur.com/FImHlDo https://imgur.com/f7xYkNH https://imgur.com/QSaa68x https://imgur.com/32TfLEW **Additional context** * `discord-giveaways` version: 5.2.1 * `discord.js` version: 13.8.0 * Custom Database (if you use one): MongoDB (Mongoose 6.3.5) Add any other context about the problem here.
Nico105 commented 2 years ago

giveaway data does not get deleted by default. you have to use the endedGiveawaysLifetime manager option.

for the "coult not get edited" error tho, I will have to investigate more

Nico105 commented 2 years ago

" but the error for the auto end it" "it gets error after trying to delete it a the end time." => Ending aborted because giveaway with

could you describe the things you did to get a this a bit more (precisely)?

TheDogHusky commented 2 years ago

So, i try to end the giveaway with the command, first, it's supposed to reply after and i just get an interaction failed then the giveaways stops. After, the package tries to delete it manually but it gets this error. Code of the end.js command:

module.exports = {
    name: "end",
    description: '🎉 Mettre fin à un giveaway déjà lancé',
    options: [
        {
            name: 'giveaway',
            description: 'Mettre fin au giveaway (message ID ou prix du giveaway)',
            type: 3,
            required: true
        }
    ],
    run: async (client, interaction) => {

        // If the member doesn't have enough permissions
        if (!interaction.member.permissions.has('MANAGE_MESSAGES') && !interaction.member.roles.cache.some((r) => r.name === "Giveaways")) {
            return interaction.reply({
                content: '<:Non_:844228495542452296> **Vous devez disposer des autorisations de gestion des messages pour terminer des giveaways.**',
                ephemeral: true
            });
        }

        const query = interaction.options.getString('giveaway');

        // fetching the giveaway with message Id or prize
        const giveaway =
            // Search with giveaway prize
            client.giveawaysManager.giveaways.find((g) => g.prize === query && g.guildId === interaction.guild.id) ||
            // Search with giveaway Id
            client.giveawaysManager.giveaways.find((g) => g.messageId === query && g.guildId === interaction.guild.id);

        // If no giveaway was found with the corresponding input
        if (!giveaway) {
            return interaction.reply({
                content: 'Impossible de trouver un giveaway pour `' + query + '`.',
                ephemeral: true
            });
        }

        if (giveaway.ended) {
            return interaction.reply({
                content: 'Ce giveaway est déjà terminé !',
                ephemeral: true
            });
        }

        // Edit the giveaway
        client.giveawaysManager.end(giveaway.messageId)
            // Success message
            .then(() => {
                // Success message
                interaction.reply(`**[Ce giveaway](https://discord.com/channels/${giveaway.guildId}/${giveaway.channelId}/${giveaway.messageId}) à été terminé !**`);
            })
            .catch((e) => {
                interaction.reply({
                    content: e,
                    ephemeral: true
                });
            });

    }
};
TheDogHusky commented 2 years ago

and for the ednedGiveawayLifetime it's ms passed or something else?

Nico105 commented 2 years ago

and for the ednedGiveawayLifetime it's ms passed or something else?

ms yeah

TheDogHusky commented 2 years ago

huh.. I passed 10000 as parameter (10s) and after 10 seconds the document doesn't get deleted

TheDogHusky commented 2 years ago

Any ideas?

TheDogHusky commented 2 years ago

I found the issue

The giveaway isn't marked as ended and doesn't get deleted. I found it while debugging and logging every giveaway on the _checkGiveaways() function. The ended property doesn't get updated. Still ended: false.

TheDogHusky commented 2 years ago

@Nico105 need to patch that

Nico105 commented 2 years ago

if it would be that easy could you provide your discord.js client options?

TheDogHusky commented 2 years ago

Here you are:

const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MEMBERS, Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
TheDogHusky commented 2 years ago

@Nico105 ?

TheDogHusky commented 2 years ago

@Nico105, so what’s the problem? I need to fix it fast so I can publish a mongo version of a certified bot, it’s really important.

confusion0 commented 2 years ago

So, i try to end the giveaway with the command, first, it's supposed to reply after and i just get an interaction failed then the giveaways stops. After, the package tries to delete it manually but it gets this error. Code of the end.js command:

module.exports = {
    name: "end",
    description: '🎉 Mettre fin à un giveaway déjà lancé',
    options: [
        {
            name: 'giveaway',
            description: 'Mettre fin au giveaway (message ID ou prix du giveaway)',
            type: 3,
            required: true
        }
    ],
    run: async (client, interaction) => {

        // If the member doesn't have enough permissions
        if (!interaction.member.permissions.has('MANAGE_MESSAGES') && !interaction.member.roles.cache.some((r) => r.name === "Giveaways")) {
            return interaction.reply({
                content: '<:Non_:844228495542452296> **Vous devez disposer des autorisations de gestion des messages pour terminer des giveaways.**',
                ephemeral: true
            });
        }

        const query = interaction.options.getString('giveaway');

        // fetching the giveaway with message Id or prize
        const giveaway =
            // Search with giveaway prize
            client.giveawaysManager.giveaways.find((g) => g.prize === query && g.guildId === interaction.guild.id) ||
            // Search with giveaway Id
            client.giveawaysManager.giveaways.find((g) => g.messageId === query && g.guildId === interaction.guild.id);

        // If no giveaway was found with the corresponding input
        if (!giveaway) {
            return interaction.reply({
                content: 'Impossible de trouver un giveaway pour `' + query + '`.',
                ephemeral: true
            });
        }

        if (giveaway.ended) {
            return interaction.reply({
                content: 'Ce giveaway est déjà terminé !',
                ephemeral: true
            });
        }

        // Edit the giveaway
        client.giveawaysManager.end(giveaway.messageId)
            // Success message
            .then(() => {
                // Success message
                interaction.reply(`**[Ce giveaway](https://discord.com/channels/${giveaway.guildId}/${giveaway.channelId}/${giveaway.messageId}) à été terminé !**`);
            })
            .catch((e) => {
                interaction.reply({
                    content: e,
                    ephemeral: true
                });
            });

    }
};

Instead of using the .end() function, try using the .edit() function and set the end timestamp parameter to the current date. Example:

client.giveawaysManager.edit(giveaway.messageId, {
            setEndTimestamp: Date.now()
})
Nico105 commented 2 years ago

@Nico105 ?

try updating to the latest version

confusion0 commented 2 years ago

you guys should add a feature that after a set amount of time, ended giveaways will get permanently deleted from the db

Nico105 commented 2 years ago

you guys should add a feature that after a set amount of time, ended giveaways will get permanently deleted from the db

that feature exist: endedGiveawaysLifeTime

confusion0 commented 2 years ago

ah, i didnt notice lol

TheDogHusky commented 2 years ago

Thank you very much, it's finally solved with the new Update.