From a6193465add37d4055cdaf5de7e846aa4b5beb37 Mon Sep 17 00:00:00 2001 From: vilP1L Date: Thu, 16 May 2019 22:12:40 -0400 Subject: [PATCH] [Trivia] Add multiple channel support --- config.json | 6 +++--- src/modules/hq-discord.js | 45 +++++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/config.json b/config.json index e8041f1..85c74cb 100644 --- a/config.json +++ b/config.json @@ -6,9 +6,9 @@ "defaultTokenSet": "bots.json", "admins": ["471344177045569546", "448294491032518666"], "channels": { - "hq-trivia": "568926468290248734", - "hq-words": "568926468290248734", - "confetti": "568927056172285981" + "hq-trivia": ["568926468290248734", "578754960897081344"], + "hq-words": ["568926468290248734"], + "confetti": ["568927056172285981"] }, "logChannel": "572870712428199951" }, diff --git a/src/modules/hq-discord.js b/src/modules/hq-discord.js index dd9124a..e684a5c 100644 --- a/src/modules/hq-discord.js +++ b/src/modules/hq-discord.js @@ -10,7 +10,7 @@ export default async (client, prize) => { const tokens = getTokens(discord.defaultTokenSet); // eslint-disable-next-line max-len const master = new Client(tokens[0], 0); - const channel = client.channels.get(discord.channels['hq-trivia']); + const channels = discord.channels['hq-trivia'].map(c => client.channels.get(c)); await master.login(); master.testWs = client.testWs; master.logging = false; @@ -20,7 +20,7 @@ export default async (client, prize) => { .setTitle('Game Live!') .setDescription(`HQ Trivia is live for **$${(prize || 5000).toLocaleString('en')}!**`) .setColor('#373C98'); - channel.send(embed); + channels.forEach(c => c.send(embed)); let correctCount = 0; let questionCount; let questionNumber; @@ -49,18 +49,20 @@ export default async (client, prize) => { const weight = client.weights.hq[i]; embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' }); }); - const m = await channel.send(embed); - const i = setInterval(() => { - const embed = new MessageEmbed() - .setTitle(`Question ${json.questionNumber}/${json.questionCount}:`) - .setDescription(`**${json.question}**`); - json.answers.forEach((a, i) => { - const weight = client.weights.hq[i]; - embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' }); - }); - m.edit(embed); - }, 2000); - setTimeout(() => clearInterval(i), 10000); + channels.forEach(async (c) => { + const m = await c.send(embed); + const i = setInterval(() => { + const embed = new MessageEmbed() + .setTitle(`Question ${json.questionNumber}/${json.questionCount}:`) + .setDescription(`**${json.question}**`); + json.answers.forEach((a, i) => { + const weight = client.weights.hq[i]; + embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' }); + }); + m.edit(embed); + }, 2000); + setTimeout(() => clearInterval(i), 12000); + }); } if (json.type === 'questionSummary') { const bestAnswer = client.weights.hq.indexOf(Math.max(...client.weights.hq)); @@ -73,12 +75,13 @@ export default async (client, prize) => { value: '[]()', }); }); - const m = await channel.send(embed); const correctIndex = json.answerCounts.map(a => a.correct).indexOf(true); - if (bestAnswer === correctIndex) { - await m.react('👍'); - correctCount += 1; - } else await m.react('👎'); + channels.forEach(async (c) => { + const m = await c.send(embed); + if (bestAnswer === correctIndex) await m.react('👍'); + else await m.react('👎'); + }); + if (correctIndex === bestAnswer) correctCount += 1; winners = json.advancingPlayersCount; winnings = `$${((prize || 5000) / winners).toFixed(2)}`; const correctUsers = client.userAnswers.hq.filter(u => u.answer === (correctIndex + 1)); @@ -95,9 +98,9 @@ export default async (client, prize) => { } }); master.ws.on('close', async () => { - await channel.send('**GAME OVER!**'); + channels.forEach(c => c.send('**GAME OVER!**')); const image = genResults('HQ Trivia', winners, winnings, `${correctCount}/${questionCount}`); const attachment = new MessageAttachment(image); - channel.send(attachment); + channels.forEach(c => c.send(attachment)); }); };