-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 2.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const telegraf = require("telegraf")
const axios = require("axios")
const { config } = require("dotenv")
const Markup = require("telegraf").Markup
const fs = require("fs")
config({
path: ".env"
})
const bot = new telegraf(process.env.TOKEN)
const categories = JSON.parse(fs.readFileSync("./categories.json"))
bot.command("new", async ctx => {
let question = (await axios.get("https://opentdb.com/api.php?amount=1&encode=url3986")).data.results[0]
let answers = [question.correct_answer].concat(question.incorrect_answers)
let correct_answer = answers[0]
answers = shuffle(answers)
ctx.replyWithQuiz(`Category: ${decodeURIComponent(question.category)}\n\n${decodeURIComponent(question.question)}`, answers.map(element => decodeURIComponent(element)), {correct_option_id: answers.indexOf(correct_answer)})
})
bot.command(Object.keys(categories), async ctx => {
let question = (await axios.get(`https://opentdb.com/api.php?amount=1&category=${categories[ctx.message.text.split(" ")[0].slice(1)][0]}&encode=url3986`)).data.results[0]
let answers = [question.correct_answer].concat(question.incorrect_answers)
let correct_answer = answers[0]
answers = shuffle(answers)
ctx.replyWithQuiz(`Category: ${decodeURIComponent(question.category)}\n\n${decodeURIComponent(question.question)}`, answers.map(element => decodeURIComponent(element)), {correct_option_id: answers.indexOf(correct_answer)})
})
bot.command("help", ctx => {
let msg = [["help", "List Of Categories"], ["new", "Random Category\n"]]
for (let cat in categories) {
msg.push([cat, categories[cat][1]])
}
ctx.reply(" - COMMANDS - \n" + msg.map(item => `/${item[0]} - ${item[1]}`).join("\n"))
})
bot.action(/answer\-.+/, async ctx => {
let index = (ctx.callbackQuery.data.split("-")[1] == 1) ? true : false
let correct_answer = ctx.callbackQuery.data.split("-")[2]
ctx.editMessageText(`${(index) ? "Your answer was correct!" : "Your answer was incorrect!"}\ncorrect answer: ${decodeURIComponent(correct_answer)}`)
})
bot.launch()
function shuffle (array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}