forked from BenC14/Dumbledore
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YOUNG EUN
committed
Nov 6, 2017
1 parent
5fb36ef
commit 4691bb9
Showing
40 changed files
with
1,120 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BOT_API_KEY= | ||
BOT_NAME= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,8 +60,8 @@ typings/ | |
.env | ||
|
||
.idea | ||
pm2.json | ||
nodemon.json | ||
.DS_Store | ||
pm2.json | ||
data/dumbledore.db | ||
package-lock.json | ||
bundle.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,26 @@ | ||
const getPointsFromDatabase = require('../helper/getPointsFromDatabase'); | ||
const botCommand = require('../helper/botCommand'); | ||
const Parse = require('parse/node'); | ||
const { DB } = require('../word'); | ||
|
||
function awardPoints(originalMessage) { | ||
let points = originalMessage.text.split(' ')[0].replace(/[^\d.]/g, ''); | ||
if (points > 100) { points = 100; } | ||
|
||
if (originalMessage.text.toLowerCase().indexOf('gryffindor') > -1) { | ||
this.db.run('UPDATE houses SET points = points + ? WHERE house = "gryffindor"', points); | ||
this.db.run('UPDATE students SET points_given = (points_given + ?) WHERE user_id = ?', points, originalMessage.user); | ||
getPointsFromDatabase.call(this, originalMessage, 'gryffindor', botCommand.awardPointsCallback); | ||
} else if (originalMessage.text.toLowerCase().indexOf('hufflepuff') > -1) { | ||
this.db.run('UPDATE houses SET points = points + ? WHERE house = "hufflepuff"', points); | ||
this.db.run('UPDATE students SET points_given = (points_given + ?) WHERE user_id = ?', points, originalMessage.user); | ||
getPointsFromDatabase.call(this, originalMessage, 'hufflepuff', botCommand.awardPointsCallback); | ||
} else if (originalMessage.text.toLowerCase().indexOf('ravenclaw') > -1) { | ||
this.db.run('UPDATE houses SET points = points + ? WHERE house = "ravenclaw"', points); | ||
this.db.run('UPDATE students SET points_given = (points_given + ?) WHERE user_id = ?', points, originalMessage.user); | ||
getPointsFromDatabase.call(this, originalMessage, 'ravenclaw', botCommand.awardPointsCallback); | ||
} else if (originalMessage.text.toLowerCase().indexOf('slytherin') > -1) { | ||
this.db.run('UPDATE houses SET points = points + ? WHERE house = "slytherin"', points); | ||
this.db.run('UPDATE students SET points_given = (points_given + ?) WHERE user_id = ?', points, originalMessage.user); | ||
getPointsFromDatabase.call(this, originalMessage, 'slytherin', botCommand.awardPointsCallback); | ||
} else if (originalMessage.text.toLowerCase().indexOf('@') > -1) { | ||
const student = originalMessage.text.substring(originalMessage.text.indexOf('@') + 1).split('>')[0]; | ||
|
||
this.db.get('SELECT user_id, username, house FROM students WHERE user_id = ?', student, (err, record) => { | ||
if (err) { | ||
return console.error('DATABASE ERROR:', err); | ||
} | ||
if (record !== undefined) { | ||
const message = originalMessage; | ||
message.text = points + ' ' + record.house; | ||
this.awardPoints(message); | ||
|
||
this.db.run('UPDATE students SET points_earned = (points_earned + ?) WHERE user_id = ?', points, student); | ||
function awardPoints(botId, userId, point) { | ||
if (typeof botId === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return; | ||
const Student = new Parse.Object(DB.STUDENT.CALL); | ||
const query = new Parse.Query(Student); | ||
query.equalTo(DB.STUDENT.BOT_ID, botId).equalTo(DB.STUDENT.USER_ID, userId).find({ | ||
success(results) { | ||
if (results.length === 0) { | ||
Student.save({ | ||
[DB.STUDENT.BOT_ID]: botId, | ||
[DB.STUDENT.USER_ID]: userId, | ||
[DB.STUDENT.POINT]: point | ||
}); | ||
} | ||
}); | ||
} | ||
results[0].increment(DB.STUDENT.POINT, point); | ||
results[0].save(); | ||
}, | ||
error(error) { | ||
console.log(error.message); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = awardPoints; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,26 @@ | ||
const getPointsFromDatabase = require('../helper/getPointsFromDatabase'); | ||
const botCommand = require('../helper/botCommand'); | ||
const Parse = require('parse/node'); | ||
const { DB } = require('../word'); | ||
|
||
function deductPoints(originalMessage) { | ||
let points = originalMessage.text.split(' ')[0].replace(/[^\d.]/g, ''); | ||
function deductPoints(botId, userId, point) { | ||
if (typeof botId === 'undefined' || typeof userId === 'undefined' || typeof point === 'undefined') return; | ||
const Student = new Parse.Object(DB.STUDENT.CALL); | ||
const query = new Parse.Query(Student); | ||
query.equalTo(DB.STUDENT.BOT_ID, botId).equalTo(DB.STUDENT.USER_ID, userId).find({ | ||
success(results) { | ||
if (results.length === 0) { | ||
Student.set(DB.STUDENT.BOT_ID, botId); | ||
Student.set(DB.STUDENT.USER_ID, userId); | ||
Student.set(DB.STUDENT.POINT, -point); | ||
|
||
if (points > 100) { points = 100; } | ||
if (originalMessage.text.toLowerCase().indexOf('gryffindor') > -1) { | ||
this.db.run('UPDATE houses SET points = MAX(0, points - ?) WHERE house = "gryffindor"', points); | ||
this.db.run('UPDATE students SET points_taken = (points_taken + ?) WHERE user_id = ?', points, originalMessage.user); | ||
getPointsFromDatabase.call(this, originalMessage, 'gryffindor', botCommand.deductPointsCallback); | ||
} else if (originalMessage.text.toLowerCase().indexOf('hufflepuff') > -1) { | ||
this.db.run('UPDATE houses SET points = MAX(0, points - ?) WHERE house = "hufflepuff"', points); | ||
getPointsFromDatabase.call(this, originalMessage, 'hufflepuff', botCommand.deductPointsCallback); | ||
this.db.run('UPDATE students SET points_taken = (points_taken + ?) WHERE user_id = ?', points, originalMessage.user); | ||
} else if (originalMessage.text.toLowerCase().indexOf('ravenclaw') > -1) { | ||
this.db.run('UPDATE houses SET points = MAX(0, points - ?) WHERE house = "ravenclaw"', points); | ||
getPointsFromDatabase.call(this, originalMessage, 'ravenclaw', botCommand.deductPointsCallback); | ||
this.db.run('UPDATE students SET points_taken = (points_taken + ?) WHERE user_id = ?', points, originalMessage.user); | ||
} else if (originalMessage.text.toLowerCase().indexOf('slytherin') > -1) { | ||
this.db.run('UPDATE houses SET points = MAX(0, points - ?) WHERE house = "slytherin"', points); | ||
getPointsFromDatabase.call(this, originalMessage, 'slytherin', botCommand.deductPointsCallback); | ||
this.db.run('UPDATE students SET points_taken = (points_taken + ?) WHERE user_id = ?', points, originalMessage.user); | ||
} else if (originalMessage.text.toLowerCase().indexOf('@') > -1) { | ||
const student = originalMessage.text.substring(originalMessage.text.indexOf('@') + 1).split('>')[0]; | ||
|
||
this.db.get('SELECT user_id, username, house FROM students WHERE user_id = ?', student, (err, record) => { | ||
if (err) { | ||
return console.error('DATABASE ERROR:', err); | ||
} | ||
|
||
if (record !== undefined) { | ||
const message = originalMessage; | ||
message.text = points + ' ' + record.house; | ||
this.deductPoints(message); | ||
Student.save(); | ||
} | ||
}); | ||
} | ||
results[0].increment(DB.STUDENT.POINT, -point); | ||
results[0].save(); | ||
}, | ||
error(error) { | ||
console.log(error.message); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = deductPoints; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.