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)
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); }); };
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:'); }); } } }; };
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)