[Trivia] Add multiple channel support

This commit is contained in:
vilP1L
2019-05-16 22:12:40 -04:00
parent d3541f4bd9
commit a6193465ad
2 changed files with 27 additions and 24 deletions

View File

@@ -6,9 +6,9 @@
"defaultTokenSet": "bots.json", "defaultTokenSet": "bots.json",
"admins": ["471344177045569546", "448294491032518666"], "admins": ["471344177045569546", "448294491032518666"],
"channels": { "channels": {
"hq-trivia": "568926468290248734", "hq-trivia": ["568926468290248734", "578754960897081344"],
"hq-words": "568926468290248734", "hq-words": ["568926468290248734"],
"confetti": "568927056172285981" "confetti": ["568927056172285981"]
}, },
"logChannel": "572870712428199951" "logChannel": "572870712428199951"
}, },

View File

@@ -10,7 +10,7 @@ export default async (client, prize) => {
const tokens = getTokens(discord.defaultTokenSet); const tokens = getTokens(discord.defaultTokenSet);
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const master = new Client(tokens[0], 0); 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(); await master.login();
master.testWs = client.testWs; master.testWs = client.testWs;
master.logging = false; master.logging = false;
@@ -20,7 +20,7 @@ export default async (client, prize) => {
.setTitle('Game Live!') .setTitle('Game Live!')
.setDescription(`HQ Trivia is live for **$${(prize || 5000).toLocaleString('en')}!**`) .setDescription(`HQ Trivia is live for **$${(prize || 5000).toLocaleString('en')}!**`)
.setColor('#373C98'); .setColor('#373C98');
channel.send(embed); channels.forEach(c => c.send(embed));
let correctCount = 0; let correctCount = 0;
let questionCount; let questionCount;
let questionNumber; let questionNumber;
@@ -49,18 +49,20 @@ export default async (client, prize) => {
const weight = client.weights.hq[i]; const weight = client.weights.hq[i];
embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' }); embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' });
}); });
const m = await channel.send(embed); channels.forEach(async (c) => {
const i = setInterval(() => { const m = await c.send(embed);
const embed = new MessageEmbed() const i = setInterval(() => {
.setTitle(`Question ${json.questionNumber}/${json.questionCount}:`) const embed = new MessageEmbed()
.setDescription(`**${json.question}**`); .setTitle(`Question ${json.questionNumber}/${json.questionCount}:`)
json.answers.forEach((a, i) => { .setDescription(`**${json.question}**`);
const weight = client.weights.hq[i]; json.answers.forEach((a, i) => {
embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' }); const weight = client.weights.hq[i];
}); embed.fields.push({ name: `${weight.toFixed(2)} | **${a.text}**`, value: '[]()' });
m.edit(embed); });
}, 2000); m.edit(embed);
setTimeout(() => clearInterval(i), 10000); }, 2000);
setTimeout(() => clearInterval(i), 12000);
});
} }
if (json.type === 'questionSummary') { if (json.type === 'questionSummary') {
const bestAnswer = client.weights.hq.indexOf(Math.max(...client.weights.hq)); const bestAnswer = client.weights.hq.indexOf(Math.max(...client.weights.hq));
@@ -73,12 +75,13 @@ export default async (client, prize) => {
value: '[]()', value: '[]()',
}); });
}); });
const m = await channel.send(embed);
const correctIndex = json.answerCounts.map(a => a.correct).indexOf(true); const correctIndex = json.answerCounts.map(a => a.correct).indexOf(true);
if (bestAnswer === correctIndex) { channels.forEach(async (c) => {
await m.react('👍'); const m = await c.send(embed);
correctCount += 1; if (bestAnswer === correctIndex) await m.react('👍');
} else await m.react('👎'); else await m.react('👎');
});
if (correctIndex === bestAnswer) correctCount += 1;
winners = json.advancingPlayersCount; winners = json.advancingPlayersCount;
winnings = `$${((prize || 5000) / winners).toFixed(2)}`; winnings = `$${((prize || 5000) / winners).toFixed(2)}`;
const correctUsers = client.userAnswers.hq.filter(u => u.answer === (correctIndex + 1)); 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 () => { 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 image = genResults('HQ Trivia', winners, winnings, `${correctCount}/${questionCount}`);
const attachment = new MessageAttachment(image); const attachment = new MessageAttachment(image);
channel.send(attachment); channels.forEach(c => c.send(attachment));
}); });
}; };