Skip to content

Instantly share code, notes, and snippets.

@theabbie
Last active October 23, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theabbie/c4eee712e685f5679a3daebb4f609439 to your computer and use it in GitHub Desktop.
Save theabbie/c4eee712e685f5679a3daebb4f609439 to your computer and use it in GitHub Desktop.
DevRant Trivia Bot
var app = require('express')();
var Bot = require('devrant-bot');
var axios = require("axios");
var encryptor = require('simple-encryptor')(process.env.quizkey);
app.get("/*", async function (req, res) {
try {
var bot = new Bot();
await bot.login("trivia", process.env.devpass);
var mentions = await bot.get();
for (msg of mentions) {
try {
if (msg.text.trim().length == 0) {
var quiz = (await axios("https://quizapi.io/api/v1/questions?apiKey=" + process.env.quiz + "&limit=1")).data;
var answers = Object.values(quiz[0].answers).filter(a => a != null);
var answer = msg.rto.length + "" + Object.values(quiz[0]["correct_answers"]).indexOf("true");
var reply = quiz[0].question + "\n\n" + answers.map((a, i) => msg.rto.length + "" + i + ". " + a).join("\n") + "\n\n" + encryptor.encrypt(answer);
await bot.reply(msg.user, msg.rid, reply);
}
else {
var guess = +msg.text.trim();
var correct = +encryptor.decrypt(msg.rto[Math.floor(guess / 10)].text.split("\n").reverse()[0]);
if (guess == correct) await bot.reply(msg.user, msg.rid, "🏆");
else await bot.reply(msg.user, msg.rid, "Sorry, correct answer is option " + correct);
}
}
catch (e) {
continue;
}
}
res.end();
}
catch (e) {
res.end(e.message);
}
})
app.listen(process.env.PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment