Hey there! I’m trying to make a Kahoot bot and suddenly I’m getting this error:
request error: Error: socket hang up
at createHangUpError (_http_client.js:335:17)
at TLSSocket.socketOnEnd (_http_client.js:436:23)
at TLSSocket.emit (events.js:201:15)
at endReadableNT (_stream_readable.js:1130:12)
at processTicksAndRejections (internal/process/task_queues.js:83:17) {
code: 'ECONNRESET'
}
This is my code (Some of it is from the example from Glitch):
const path = require("path");
// Require the fastify framework and instantiate it
const fastify = require("fastify")({
// Set this to true for detailed logging:
logger: false
});
// ADD FAVORITES ARRAY VARIABLE FROM README HERE
// Setup our static files
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "public"),
prefix: "/" // optional: default '/'
});
// fastify-formbody lets us parse incoming forms
fastify.register(require("fastify-formbody"));
// point-of-view is a templating manager for fastify
fastify.register(require("point-of-view"), {
engine: {
handlebars: require("handlebars")
}
});
// Load and parse SEO data
const seo = require("./src/seo.json");
if (seo.url === "glitch-default") {
seo.url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`;
}
// Our home page route, this returns src/pages/index.hbs with data built into it
fastify.get("/", function(request, reply) {
// params is an object we'll pass to our handlebars template
let params = { seo: seo };
// The Handlebars code will be able to access the parameter values and build them into the page
reply.view("/src/pages/index.hbs", params);
});
// Run the server and report out to the logs
fastify.listen(process.env.PORT, function(err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Your app is listening on ${address}`);
fastify.log.info(`server listening on ${address}`);
});
// Pressed the button
fastify.post("/", function(request, reply) {
const Kahoot = require("kahoot.js");
const client = new Kahoot;
console.log("Logging in...")
client.join(request.body.pin, "Bot");
client.on("joined", () => {
console.log("I joined the Kahoot!");
});
client.on("quizStart", () => {
console.log("The quiz has started!");
});
client.on("quizEnd", () => {
console.log("The quiz has ended.");
});
client.on("questionStart", question => {
console.log(question)
const choices = client.quiz.questions[question.questionIndex].choices;
for (let i in question.choices) {
if(question.choices[i].correct) {
var answer = i;
break;
}
}
question.answer(answer);
});
});
Also, the Kahoot bot doesn’t join the game for some reason. I think it’s because of this error. Sorry if I’m making a stupid mistake I just don’t really know these kinds of stuff.