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

this.client.on is not a function #264

Closed fedesugo closed 3 years ago

fedesugo commented 3 years ago

Module is not working, tells me this.client.on is not a function.

Nico105 commented 3 years ago

ahm where do you do this.client.on?

fedesugo commented 3 years ago

its on node_modules manager.js line 464

Nico105 commented 3 years ago

https://github.com/Androz2091/discord-giveaways/issues/190 If you are using a extended client, take a look at that issue

fedesugo commented 3 years ago

so what i need to take out?

fedesugo commented 3 years ago
const config_gw = require('../src/Resources/Config/bot');
bot.config_gw = config;

const { GiveawaysManager } = require("discord-giveaways");
const db = require("quick.db");
const client = require('../src/Resources/Config/bot');
if (!db.get("giveaways")) db.set("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 db.get("giveaways");
    }

    // This function is called when a giveaway needs to be saved in the database (when a giveaway is created or when a giveaway is edited).
    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 = 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 = db.get("giveaways").filter((giveaway) => giveaway.messageID !== 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,
    updateCountdownEvery: 5000,
    default: {
        botsCanWin: false,
        exemptPermissions: [],
        embedColor: "#FF0000",
        reaction: config_gw.reaction
    }
});
client.giveawaysManager = manager;
// We now have a client.giveawaysManager property to manage our giveaways!

client.giveawaysManager.on("giveawayReactionAdded", (giveaway, member, reaction) => {

    try {

        if (member.user.bot) return;

        const Discord = require("discord.js")
        const db = require('quick.db')

        let language = db.fetch(`lang_${member.guild.id}`)

        if (language === null) language = config_gw.basiclang

        const lang = require(`../src/lang/${language}.js`)

        let logs = db.fetch(`logs_${member.guild.id}`)

        if (logs === null) return;

        const salon = member.guild.channels.cache.get(logs);

        const Embed = new Discord.MessageEmbed()
        .setAuthor(lang.logs.raddtitle)
        .setDescription(lang.logs.raddmsg1 + "** **" + "`" + member.user.tag + "`" + "** **" + lang.logs.raddmsg2 + "** **" + "`" + giveaway.messageID + "`" + "** **" + config.reaction)
        .setFooter(config_gw.embeds.footers)
        .setColor(config_gw.events.addcolor)
        .setTimestamp()

        salon.send(Embed)

    } catch (e) {
        return;
    }
});

client.giveawaysManager.on("giveawayReactionRemoved", (giveaway, member, reaction) => {

    try {

        if (member.user.bot) return;

        const Discord = require("discord.js")

        const db = require('quick.db')

        let language = db.fetch(`lang_${member.guild.id}`)

        if (language === null) language = config_gw.basiclang

        const lang = require(`../src/lang/${language}`)

        let logs = db.fetch(`logs_${member.guild.id}`)

        if (logs === null) return;

        const salon = member.guild.channels.cache.get(logs);

        const Embed = new Discord.MessageEmbed()
            .setAuthor(lang.logs.rremtitle)
            .setDescription(lang.logs.rremmsg1 + "** **" + "`" + member.user.tag + "`" + "** **" + lang.logs.rremmsg2 + "** **" + "`" + giveaway.messageID + "`" + "** **" + config.reaction)
            .setFooter(config_gw.embeds.footers)
            .setColor(config_gw.events.remcolor)
            .setTimestamp()

        salon.send(Embed)

    } catch (e) {
        return;
    }
});
Nico105 commented 3 years ago

the problem is the giveaway manager needs the client that got created with new someClass(), you do that somewhere right? so you can either put the manager creation in the file where you do new Client()

const { client } = require('../src/Resources/Config/bot');
const client = new Client();
const GiveawayManagerWithOwnDatabase = require('SomeFileWhereyouPutTheExtendedClass')
const manager = new GiveawayManagerWithOwnDatabase(client, {
    storage: false,
    updateCountdownEvery: 5000,
    default: {
        botsCanWin: false,
        exemptPermissions: [],
        embedColor: "#FF0000",
        reaction: config_gw.reaction
    }
});
client.giveawaysManager = manager;
require('fileWithGIveawayEvents')(client)

or you add the manager in your extended client = https://github.com/Androz2091/discord-giveaways/issues/190#issuecomment-764833315

Dragonizedpizza commented 3 years ago

but... the real question is, where is this.client.on?

Nico105 commented 3 years ago

but... the real question is, where is this.client.on?

it's for the raw discord event in order to to emit giveaway events and that does not work because the giveaway manager did not reveive a initialized client = const client = new Class() the only other way as mentioned if he defined the manager in his extended client and does const manager = new GiveawayManagerWithOwnDatabase(this, {

fedesugo commented 3 years ago

now the file with giveaway events is Config/Bot?

Dragonizedpizza commented 3 years ago

?

Nico105 commented 3 years ago

now the file with giveaway events is Config/Bot?

moment, do you have a extended discord client = custom or do you use the default one?

fedesugo commented 3 years ago

i hace the disable mentions one

Nico105 commented 3 years ago

not exactly what i asked but nvm

remove const client = require('../src/Resources/Config/bot'); put require('fileWithTheCodeYouSentAbove')(client) into the file where you do client.login(token) put module.exports = client => {} around the whole https://github.com/Androz2091/discord-giveaways/issues/264#issuecomment-823502916 code

fedesugo commented 3 years ago

i haved like this

fedesugo commented 3 years ago

https://sourceb.in/g0zi493Btj

Nico105 commented 3 years ago

Ahm okay if you want everything in your main file... now just add

const manager = new GiveawayManagerWithOwnDatabase(bot, {
    storage: false,
    updateCountdownEvery: 5000,
    default: {
        botsCanWin: false,
        exemptPermissions: [],
        embedColor: "#FF0000",
        reaction: config_gw.reaction
    }
});
client.giveawaysManager = manager;
fedesugo commented 3 years ago

ready

fedesugo commented 3 years ago

now?

fedesugo commented 3 years ago

WOW!!! THANKS IT WORKS