在过去的几周中,我一直在为自己的机器人编写测验命令。它工作正常,但我对此有疑问。
我在这里使用的Quiz命令意味着一旦有人运行了该命令。运行命令的用户只能回答问题。我想要其他人可以回答的地方,他们会得到正确回答的金钱/积分。
代码
const { get } = require('node-superfetch')
const { RichEmbed } = require('discord.js');
const db = require('quick.db');
const choices = ['1', '2', '3', '4'];
const util = require('../../utilquiz.js')
exports.run = async (client, message, args) => {
let TotalQuiz;
let quizNew = await db.fetch(`quizNew_${message.author.id}`)
if (quizNew === null) db.set(`quizNew_${message.author.id}`, 0);
else TotalQuiz = quizNew;
if (TotalQuiz === undefined) TotalQuiz = 1;
try {
const { body } = await get('https://opentdb.com/api.php?amount=4')
.query({
amount: 1,
encode: 'url3986'
});
let difficult = body.results[0].difficulty;
let category = body.results[0].category;
let question = body.results[0].question
let answer = body.results[0].incorrect_answers;
answer.push(body.results[0].correct_answer);
answer = util.shuffle(answer);
const embed = new RichEmbed()
.setAuthor('Nate Bot Quiz ⁉️', 'https://i.imgur.com/EVTiOBW.png')
.setColor('76d6ff')
.setDescription(`**${decodeURIComponent(body.results[0].question)}**\n\n` + answer.map((x, i) => `**${choices[i]} »** \`${decodeURIComponent(x)}\``).join('\n'))
.addField('**Difficulty**', `\`${decodeURIComponent(difficult)}\``, true)
.addField('**Quiz Genre**', `\`${decodeURIComponent(category)}\``, true)
.setFooter('You have 30 seconds to answer this! | $40 Reward if Answered Correctly')
message.channel.send(embed);
const filter = res => choices.includes(res.content) && res.author.id === message.author.id
const reply = await message.channel.awaitMessages(filter, {
max: 1,
time: 30000
});
if (!reply.size) return message.channel.send(`**Quiz ❓|** **Time is up Buddy! ?**\n\n**Question »** \`${decodeURIComponent(body.results[0].question)}\`\n**Answer(s) »** \`${decodeURIComponent(body.results[0].correct_answer)}\``);
db.add(`quizNew_${message.author.id}`, 1)
db.add(`coins_${message.author.id}`, 40)
if (answer[choices.indexOf(reply.first().content.toUpperCase())] === body.results[0].correct_answer) return message.channel.send(`**Quiz ❓|** **Well done ${message.author.username}!** You answered it correctly ??\n\n**Question »** \`${decodeURIComponent(body.results[0].question)}\`\n**Answer(s) »** \`${decodeURIComponent(body.results[0].correct_answer)}\`\n\n*You gained $40 for answering this Correct!*`);
db.subtract(`quizNew_${message.author.id}`, 1);
db.subtract(`coins_${message.author.id}`, 40);
return message.channel.send(`**Quiz ❓|** Incorrect Answer! ?\n\n**Question »** \`${decodeURIComponent(body.results[0].question)}\`\n**Correct Answer »** \`${decodeURIComponent(body.results[0].correct_answer)}\``);
} catch (e) {
return message.channel.send(`**Quiz ❓|** Error occured! please try again later :(\n${e}`);
}
}
exports.conf = {
aliases: ['qna'],
cooldown: "5"
}
exports.help = {
name: "trivia",
description: "play trivia game with randomly quiz",
usage: "trivia"
}