0

So I've been working on adding a points system into my Discord bot and I've sat here for like 3 hours trying different things and I think it's something obvious and yet I'm clearly missing it completely. I send an embed via DM to them with the information regarding the addition or removal of their points. I am storing it in the database in a table called "users" with a primary key of "user" which is their Discord ID and "sulfur" which is the name of the points.

exports.sendInfo = function(to, type, amount, by)
{   
    const infoEmbed = new discord.RichEmbed();

    if (type == "give")
    {
        infoEmbed.setThumbnail(bot.user.displayAvatarURL);
        infoEmbed.setColor("#32CD32");
        infoEmbed.setDescription("You have been given sulfur!");
        infoEmbed.addField("Given By", by);
        infoEmbed.addField("Amount Given", amount);
        infoEmbed.addField("Current Sulfur", utilities.getPoints(to.id));
    }
    if (type == "take")
    {
        infoEmbed.setThumbnail(bot.user.displayAvatarURL);
        infoEmbed.setColor("#ff0000");
        infoEmbed.setDescription("You have lost sulfur!");
        infoEmbed.addField("Taken By", by);
        infoEmbed.addField("Amount Taken", amount);
        infoEmbed.addField("Current Sulfur", utilities.getPoints(to.id));
    }

    to.send({embed: infoEmbed});
}

exports.getPoints = function(id)
{
    var points;
    utilities.getPointsQuery(id, function(err, result)
    {
        if (err)
            logger.error(err);
        else
            points = result;
    });

    return points;
}

exports.getPointsQuery = function(id, callback)
{
    db.query("SELECT * FROM `users` WHERE `user` = '" + id + "'", function(err, results)
    {
        if (err)
            callback(err, null);
        else
            callback(null, results[0].sulfur);
    });
}

So I figured "getPoints" would be easier than hard coding the callback multiple times in different places. So, what should happen is I call getPoints(user id here) and it should make a call with the ID and the callback function to the getPointsQuery and then it should callback to the getPoints function and set the value it callsback as points and then the function returns the points so I can share that with the user. I've tried console logging the results[0].sulfur and it is indeed the correct value and yet it returns undefined in Discord.

Picture of what is shown to the user. No matter what it keeps saying undefined..

Jake Adams
  • 25
  • 7

0 Answers0