diff --git a/config.json b/config.json index 4a94a45..8bd9698 100644 --- a/config.json +++ b/config.json @@ -6,5 +6,7 @@ "PROD_SOCKET_URL": "wss://play.tinagtg.com", "endpoints": { "register": "/parse/users" - } + }, + "discord_token": "", + "discord_channel": "504041066559963137" } \ No newline at end of file diff --git a/package.json b/package.json index 5e43bae..36e69c8 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "babel-preset-env": "^1.7.0", "babel-preset-es2016": "^6.24.1", "babel-preset-stage-2": "^6.24.1", - "discord.js": "^11.3.2", + "discord.js": "^11.4.2", "faker": "^4.1.0", "fs": "0.0.1-security", "request": "^2.87.0", diff --git a/quiz.js b/quiz.js index 84df549..f5255de 100644 --- a/quiz.js +++ b/quiz.js @@ -1,7 +1,9 @@ import ws from 'ws'; import fs from 'fs'; +import Discord from 'discord.js'; const config = JSON.parse(fs.readFileSync('config.json')); +const client = new Discord.Client(); class Player { constructor({ objectId, username }) { @@ -69,7 +71,6 @@ class Player { // maybe a bit hackish? but works for every example I see so far and cleaner. const msgCode = parseInt(message); const data = parseMessage(); - switch (msgCode) { case 0: const parsedJSON = JSON.parse(message.slice(1)); @@ -87,6 +88,28 @@ class Player { console.error(`Player ${this.username} failed to connect!`); } break; + case 431: + const game = data[1]; + let showQuestions = false; + + if (game && game.questions) { + if (this.game && this.game.questions) { + if (game.questions.length !== this.game.questions.length) { + showQuestions = true; + } + } else { + showQuestions = true; + } + } + + this.game = game; + + if (this.isMaster && showQuestions) { + console.log('Received the game\'s questions:'); + this.showQuestions(); + } + + break; case 42: const type = data[0]; switch (type) { @@ -112,7 +135,6 @@ class Player { showQuestions = true; } } - this.game = game; if (this.isMaster && showQuestions) { console.log('Received the game\'s questions:'); @@ -127,22 +149,39 @@ class Player { } } - showQuestions() { - const { questions } = this.game; + showQuestions() { + const { questions } = this.game; try { questions.forEach((question, index) => { + const embed = new Discord.RichEmbed(); console.log(`\nQuestion ${index + 1} (${question.type})`); switch (question.type.toLowerCase()) { case 'image': console.log(`${question.imageSrc}\n`); + embed.setTitle(`${index + 1}: ${question.imageSrc}`); break; case 'text': + embed.setTitle(`${index + 1}: ${question.text}`); console.log(`${question.text}\n`); break; } + embed.setDescription(`WRONG: ${question.answerWrongText}\nRIGHT: ${question.answerRightText}`); question.answers.forEach((answer, index) => { + embed.addField(index + 1, answer.text, false); console.log(`${index + 1}) ${answer.text}`); }); + const channel = client.channels.get(config.discord_channel); + channel.send( { embed: embed } ).then((message)=>{ + message.react('504045972217659432').then((m)=>{ + message.react('504045973241331712').then((m)=>{ + message.react('504045972045955073').then((m)=>{ + message.react('504045971878051851').then((m)=>{ + message.react('504045972419117057'); + }); + }); + }); + }); + }); }); } catch (error) { console.error('Question format was invalid. Here is a dump of what we got:'); @@ -173,5 +212,15 @@ class Game { } } -const game = new Game(); -game.run(); +client.on('ready', () => { + // This event will run if the bot starts, and logs in, successfully. + console.log(`Discord module started with ${client.users.size} users.`); + client.user.setActivity('Samantha Bee'); + +}); +client.login(config.discord_token).then(()=>{ + const game = new Game(); + game.run(); +}); + +