diff --git a/bot.js b/bot.js index a302100..c8818da 100755 --- a/bot.js +++ b/bot.js @@ -54,7 +54,7 @@ function validateDm(msg) { // Compliment request('https://complimentr.com/api', {json: true}, function (error, response, body) { let text; - if (!error && response.statusCode == 200) { + if (!error && response.statusCode === 200) { text = body.compliment; } else { text = "I'm not in the mood to compliment you."; @@ -70,10 +70,31 @@ function validateDm(msg) { }); bot.on('/register', (msg) => { - if (!validateDm(msg)) { - return; - } - return db.selectTeamDialog(bot, msg, "register", "To which Team do you pledge your allegiance?"); + + const fs = require('fs'); + fs.readFile('./teamlist.json', 'utf8', (error, jsonString) => { + if (error) { + console.log("Error reading JSON file:", error); + return; + } + try { + const teamlist = JSON.parse(jsonString); + const username = msg.from.username; + const team = teamlist[username.toLowerCase()]; + console.log(msg.from.id); + + if (!validateDm(msg)) { + return; + } + + return team + ? db.processRegistration(msg, team, (message) => { + return bot.sendMessage(msg.from.id, message); + }) + : bot.sendMessage(msg.chat.id, "You don't seem to have signed up for Ashanshins! Please contact the Ashanshins game masters for assistance."); + + } finally {} + }); }); // bot.on(/^\/register (.+)$/, (msg, props) => { // const text = props.match[1].toLowerCase(); @@ -241,8 +262,11 @@ function validateDm(msg) { if (!validateAdmin(msg.from.id)) { return; } - const userName = props.match[1] - // console.log("Attempting to unregister " + userName); + const command = props.match[1]; + const userName = command.substring(12); + console.log("Processing unregister" + userName); + console.log("Regex capture: " + command); + console.log("userName: " + userName); return db.processUnregistration(msg, userName, (message) => { return bot.sendMessage(msg.chat.id, message); }); @@ -358,11 +382,11 @@ bot.on('callbackQuery', msg => { case "random": return db.rollTeam(bot, msg, data.t); break; - case "register": - return db.processRegistration(msg, data.t, (message) => { - return bot.sendMessage(msg.from.id, message); - }); - break; + // case "register": + // return db.processRegistration(msg, data.t, (message) => { + // return bot.sendMessage(msg.from.id, message); + // }); + // break; case 'cancel': cancelCallback(msg); break; @@ -390,21 +414,21 @@ function extractFirst(input) { return [inputArray[0], remainder]; } -function extractLast(input) { - var inputArray = input.split(" "); - var remaining = inputArray[0]; - - for (var i = 1; i < inputArray.length - 1; i++) { - remaining += " " + inputArray[i]; - } - return [remaining, inputArray[inputArray.length - 1]]; -} - -function pingAdmins(bot, message) { - for (var i in adminIDs) { - bot.sendMessage(adminIDs[i], message); - } -} +// function extractLast(input) { +// var inputArray = input.split(" "); +// var remaining = inputArray[0]; +// +// for (var i = 1; i < inputArray.length - 1; i++) { +// remaining += " " + inputArray[i]; +// } +// return [remaining, inputArray[inputArray.length - 1]]; +// } +// +// function pingAdmins(bot, message) { +// for (var i in adminIDs) { +// bot.sendMessage(adminIDs[i], message); +// } +// } // bot.on('/generate_equations', (msg) => { //nani? // var operands = [" + ", " - ", " * ", " / "]; diff --git a/config.js b/config.js index f9fc75f..f1b1f5a 100644 --- a/config.js +++ b/config.js @@ -10,7 +10,9 @@ module.exports.VALIDATE_DIRECT_MESSAGE = "Walao, don't spam people lah. Message // TODO: move to .env module.exports.TEAMS = ["Scone Scoopers", "Shake Shack", "Yoda Soda", "Pizza the Hutt"]; //phase 1 +module.exports.TEAMLINKS = ["https://t.me/joinchat/znnCwpeRgLdjYmVl", "https://t.me/joinchat/p20ldVveuP4xOTVl", "https://t.me/joinchat/qEx3E8K0zhZjZDll", "https://t.me/joinchat/kLDG3q3q6mkzYWVl"]; //phase 1 telegram group links // module.exports.TEAMS = ["Rebels", "Nestle"]; //phase 2 +// module.exports.TEAMLINKS = ["", ""]; //phase 2 telegram group links module.exports.GROUP_CHATS = [ // -429367857, // dev records 1543255785 // -1507493514, // announcements diff --git a/db.js b/db.js index ac94c59..fd54953 100755 --- a/db.js +++ b/db.js @@ -28,6 +28,7 @@ const { DbUriString, SUCCESSFUL_DEATH_MESSAGE, TEAMS, + TEAMLINKS, GROUP_CHATS } = require("./config") @@ -472,6 +473,15 @@ function isValidTeam(team) { return false; } +function getTeamLink(team) { + for (let i = 0; i < TEAMS.length; i++) { + if (team === TEAMS[i]) { + return TEAMLINKS[i]; + } + } + return ""; +} + async function isRegistered(userId) { const matches = await Player.find({"user.id": userId}).exec(); return matches.length > 0; @@ -479,15 +489,22 @@ async function isRegistered(userId) { async function processRegistration(msg, team, callback) { if (await isRegistered(msg.from.id)) { - callback("Either you've already registered, or we have encountered an error."); - return; - } - if (!isValidTeam(team)) { - console.log("Error in processing registration, invalid team"); - const bad = "An unknown error in team selection has occurred, please contact the bot developer" - callback(bad); + callback("You are already registered as part of Team " + + team + + "!\n\nMain Ashanshins Channel:\nhttps://t.me/joinchat/KQbQCtr_GJo0ODk1\n\nMain Ashanshins Group:\nhttps://t.me/joinchat/6_HD4CX2E21lY2U1\n\nTeam " + + team + + " Group:\n" + + getTeamLink(team) + + "\n\nMay the force be with you..." + ); return; } + // if (!isValidTeam(team)) { + // console.log("Error in processing registration, invalid team"); + // console.log(msg.from.username); + // callback("An unknown error in team selection has occurred! Please contact the bot developers."); + // return; + // } try { addPlayer({ name: msg.from.first_name, @@ -509,7 +526,16 @@ async function processRegistration(msg, team, callback) { callback("An error occured in registration!"); return; } - callback("Successful registration!"); + callback("Welcome to Ashanshins 8, " + + msg.from.first_name + + ".\n\nThe Sorting Hat says... " + + team + + "!\n\nMain Ashanshins Channel:\nhttps://t.me/joinchat/KQbQCtr_GJo0ODk1\n\nMain Ashanshins Group:\nhttps://t.me/joinchat/6_HD4CX2E21lY2U1\n\nTeam " + + team + + " Group:\n" + + getTeamLink(team) + + "\n\nMay the force be with you..." + ); } // function processUnregistration(msg, user, callback) { @@ -520,6 +546,7 @@ async function processRegistration(msg, team, callback) { // } async function processUnregistration(msg, userName, callback) { + // console.log("executing processUnregistration, userName: " + userName); const doc = await Player.findOneAndDelete({"user.username": userName}) .exec(); if (doc) { @@ -553,7 +580,7 @@ function selectTeamDialog(bot, msg, purpose, text) { buttons ); - if (purpose == "random" || purpose == "register") { + if (purpose === "random" || purpose === "register") { bot.sendMessage(msg.chat.id, text, {replyMarkup}); } else { bot.sendMessage(msg.from.id, text, {replyMarkup}); diff --git a/teamlist.json b/teamlist.json new file mode 100644 index 0000000..f1443df --- /dev/null +++ b/teamlist.json @@ -0,0 +1,103 @@ +{ + "nicktohzyu": "Pizza the Hutt", + "leehws": "Shake Shack", + "itsraininggg": "Yoda Soda", + "awoops": "Scone Scoopers", + "nandi_del_fuego": "Scone Scoopers", + "vigonometry": "Yoda Soda", + "buyaofan": "Yoda Soda", + "hkhkoh": "Yoda Soda", + "jusaphilosopher": "Yoda Soda", + "casperplz": "Yoda Soda", + "iantensoku": "Yoda Soda", + "chugcup": "Yoda Soda", + "gottagofas": "Yoda Soda", + "omyhangu": "Yoda Soda", + "bonypony": "Yoda Soda", + "zevesley": "Yoda Soda", + "gayofree": "Yoda Soda", + "nareus11": "Yoda Soda", + "bridsss": "Yoda Soda", + "iztanpy": "Yoda Soda", + "mimae508": "Yoda Soda", + "uriibabaa": "Yoda Soda", + "shreeshreeshreeee": "Yoda Soda", + "ju_can": "Yoda Soda", + "xinderella": "Yoda Soda", + "ruoyannniee": "Yoda Soda", + "steffint": "Yoda Soda", + "aemeera": "Yoda Soda", + "ja95sos": "Yoda Soda", + "jonaudie": "Pizza the Hutt", + "dominickohhh": "Pizza the Hutt", + "bigtreenterprises": "Pizza the Hutt", + "kashyfisalleh": "Pizza the Hutt", + "froschbravo": "Pizza the Hutt", + "beeyyekem": "Pizza the Hutt", + "keane_ong": "Pizza the Hutt", + "joanthechin": "Pizza the Hutt", + "eetenggggg": "Pizza the Hutt", + "edologgerbird": "Pizza the Hutt", + "timkmz": "Pizza the Hutt", + "shireenlee": "Pizza the Hutt", + "dhiiiii": "Pizza the Hutt", + "ongsy": "Pizza the Hutt", + "sid_premnath": "Pizza the Hutt", + "jj_ayj": "Pizza the Hutt", + "hyunmin_cho": "Pizza the Hutt", + "aa_lxyxh": "Pizza the Hutt", + "abiramen": "Pizza the Hutt", + "natashalyx": "Pizza the Hutt", + "christalofthelow": "Pizza the Hutt", + "bloopyaldrey": "Pizza the Hutt", + "seavhouy": "Pizza the Hutt", + "fourbraincells": "Pizza the Hutt", + "benteoo": "Scone Scoopers", + "tunnytunesss": "Scone Scoopers", + "praveeeenk": "Scone Scoopers", + "teamlemon": "Scone Scoopers", + "sai_thejaa": "Scone Scoopers", + "bertybertquek": "Scone Scoopers", + "barniee": "Scone Scoopers", + "rendewong": "Scone Scoopers", + "ychuan18": "Scone Scoopers", + "yitching": "Scone Scoopers", + "liang_en": "Scone Scoopers", + "leelehan": "Scone Scoopers", + "leonardodavinsri": "Scone Scoopers", + "meownir": "Scone Scoopers", + "jxycel": "Scone Scoopers", + "liankit": "Scone Scoopers", + "douglaschoi": "Scone Scoopers", + "kelsykoh": "Scone Scoopers", + "nicchew": "Scone Scoopers", + "liuxinyuu": "Scone Scoopers", + "keziakhoo": "Scone Scoopers", + "itschlowee": "Scone Scoopers", + "fayolatan": "Scone Scoopers", + "jialeaves": "Scone Scoopers", + "seadragon2000": "Shake Shack", + "kelsomebody": "Shake Shack", + "haowen181": "Shake Shack", + "ddannyiel": "Shake Shack", + "mtdx99": "Shake Shack", + "nuynixf": "Shake Shack", + "tmxfean": "Shake Shack", + "erichasaboringlife": "Shake Shack", + "meiyeee": "Shake Shack", + "tsingzx": "Shake Shack", + "mmeganloww": "Shake Shack", + "dev_rh": "Shake Shack", + "kaenaaa": "Shake Shack", + "doctoorhouse": "Shake Shack", + "htetdbear": "Shake Shack", + "poofingfudges": "Shake Shack", + "lordnishant": "Shake Shack", + "jobeet": "Shake Shack", + "lotassssss": "Shake Shack", + "itscharlottelah": "Shake Shack", + "jodytng": "Shake Shack", + "timtamzy": "Shake Shack", + "tingwanlin": "Shake Shack", + "bettersafethansushi": "Shake Shack" +} \ No newline at end of file