Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
getting members data from model
  • Loading branch information
drubiodev committed Mar 9, 2018
commit 1731fc11339697d5d07010758127fa3bd74d3cab
30 changes: 26 additions & 4 deletions bot/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Discord = require('discord.js');
const util = require('apex-util');
const { isAdmin } = require('./botUtils.js');
const models = require('../db/models');

// If production server, set default debug mode to production setting
if (process.env.NODE_ENV === 'production' && !process.env.DEBUG_MODE) process.env.DEBUG_MODE = 0;
Expand All @@ -10,11 +11,32 @@ const client = new Discord.Client();
// Pre-load controllers
const controllers = require('./controllers')();

// const numberOfMessagesForBonus = 1000;
const awardBonusPoints = (user) => {

const awardBonusPoints = async (user) => {
const numberOfMessagesForBonus = 1000;
// Get User Message Count
const memberData = await models.Member.findAll(
{
attributes: ['messagesCount', 'points'],
where: { discordUser: user },
},
);
let { messagesCount } = memberData[0].dataValues;
let { points } = memberData[0].dataValues;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! If you wanted to you could condense these two lines into one. When deconstructing a variable, you can add multiple items... For example here you could say let { messagesCount, points } = memberData[0].dataValues; and it would say the same thing with one less line of code. They could also be made a const too since they don't change.

util.log('Results from database call', memberData[0].dataValues, 4);
// Check if its greater or equal to numberOfMessagesForBonus
util.log('User: ', user, 0);
if (messagesCount >= numberOfMessagesForBonus) {
points += 5;
messagesCount = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick input, I noticed this is going to affect the points that users gain from regularly chatting. That feature directly uses the messagesCount to determine how many points the users have earned through chatting. If there would be a way to implement this without resetting the message count to zero each time that would be great.

}
// Update member information
// await models.Member.update(
// { messagesCount: 3424242 },
// { where: { discordUser: user } },
// ).then((updatedRows) => {
// util.log('Updated result', updatedRows, 4);
// });
// util.log('User: ', user, 0);
};

// Alert when ready
Expand All @@ -24,7 +46,7 @@ client.on('ready', () => {

// Listen for messages
client.on('message', (message) => {
awardBonusPoints(message.author.username);
awardBonusPoints(message.author.id);
// Check for ! prefix on message to ensure it is a command
if (message.content.charAt(0) === '!') {
util.log('Command message received', message.content, 0);
Expand Down