Click here to Skip to main content
16,004,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Usage : .gift username #
Script should give specified username X amount of points. It is working if user has all lowercase letters in the name, if there are any uppercase letters it does not recognise the user and spits out following message :
Unhandled rejection TypeError: Cannot read property 'id' of undefined
    at /home/ubuntu/workspace/context.js:186:132
    at null.<anonymous> (/home/ubuntu/workspace/context.js:141:13)
    at tryCatcher (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/util.js:11:23)
    at Promise.module.exports.Promise._settlePromiseFromHandler (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:489:31)
    at Promise.module.exports.Promise._settlePromise (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:546:18)
    at Promise.module.exports.Promise._settlePromise0 (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:591:10)
    at Promise.module.exports.Promise._settlePromises (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:674:18)
    at Async._drainQueue (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/async.js:125:16)
    at Async._drainQueues (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/async.js:135:10)
    at Immediate.__dirname.drainQueues [as _onImmediate] (/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/async.js:16:14)
    at processImmediate [as _immediateCallback] (timers.js:374:17)


I made lines where problem is BOLD.

gift.js
JavaScript
    exports.names = ['gift'];
    exports.hidden = false;
    exports.enabled = true;
    exports.matchStart = true;
    exports.handler = function (data) {

    var input = data.message.split(' ');
    var username = input[1];
    var usernameFormatted = S(username).chompLeft('@').s;
    var amount = parseInt(input[2]);
    var user = bot.getUserByName(usernameFormatted.toLowerCase());

    if (!amount || isNaN(amount) || amount < 1) {
        amount = 1;
    }

    if (!username) {
        bot.sendChat('/me usage: .gift @username 50 (gifts 50 points)');
        return;
    }
    else if (!user) {
        bot.sendChat('/me user ' + username + ' was not found.');
    }

   

     getDbUserFromSiteUser(data.user, function (row) {
        if (!row || row.custom_points == 0) {
            bot.sendChat('You do not have any ' + config.customPointName + ' to give,                      @' + data.user.username + '.')
            return;
        } else if (row.custom_points < amount) {
            bot.sendChat('You only have ' + row.custom_points + ' ' +  config.customPointName + ' to give, @' +
                data.user.username + '.');
            return;
        }

        transferCustomPoints(data.user, user, amount);

    });
};


Context.js

JavaScript
    getDbUserFromSiteUser = function (siteUser, callback) {
        models.User.find({
            where: {site_id: siteUser.id}
        }).then(function (row) {
            callback(row); 
        });

    transferCustomPoints = function (fromUser, toUser, points) {

        // Create them out of thin air!
        if (fromUser === null) {
            fromUser = bot.getSelf();

            models.User.update({custom_points: Sequelize.literal('(custom_points + ' + points + ')')}, {where: {site_id: toUser.id}});
            console.log('[GIFT] ' + fromUser.username + ' awarded ' + points + ' points to ' + toUser.username);
            bot.sendChat(':gift: ' + fromUser.username + ' awarded ' + points + ' ' + config.customPointName + ' to @' +
                toUser.username + ' :gift:');

            return;
        }
        else {
            getDbUserFromSiteUser(fromUser, function (row) {
                if (!row || row.custom_points < points) {
                    console.log('Gift failed');
                    return false;
                }

                // Deduct the points from the sender's balance and add to the recipient
                models.User.update({custom_points: Sequelize.literal('(custom_points - ' + points + ')')}, {where: {site_id: fromUser.id}});
                models.User.update({custom_points: Sequelize.literal('(custom_points + ' + points + ')')}, {where: {site_id: toUser.id}});


                console.log('[GIFT] ' + fromUser.username + ' gave ' + points + ' points to ' + toUser.username);
                bot.sendChat(':gift: @' + fromUser.username + ' gave ' + points + ' ' + config.customPointName + ' to @' +
                    toUser.username + ' :gift:');

            });
        }
    }

  };
};
Posted
Updated 20-Jan-16 5:38am
v7
Comments
Sergey Alexandrovich Kryukov 17-Jan-16 20:18pm    
In what line? Yes, I can see your exception stack, but it's not helpful. In what line of your code?
—SA
Member 12269577 18-Jan-16 6:35am    
I updated the question, does that help at all?
Richard MacCutchan 18-Jan-16 6:56am    
You still have not indicated the line of code where the error occurs.
Member 12269577 20-Jan-16 11:35am    
Done, do you need me to do anything else, also sorry for being slow
Sergey Alexandrovich Kryukov 20-Jan-16 11:59am    
Look at those
/home/ubuntu/workspace/context.js:141:13
/home/ubuntu/workspace/node_modules/sequelize/node_modules/bluebird/js/release/util.js:11:23
and so on...

You have all those files during execution, and we don't have them.

You have to track the execution through all these points and see what goes on. It would be the best to do it under the debugger. Ultimately, you will see the point with
someVariable.id, where someVariable equals to the predefined object "undefined". You need to investigate how your execution comes to it.

Some of the point in your exception stack lie in your code. You should better comment all those lines with relevant information and refer to these comments in the text of your question.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900