This commit is contained in:
Dragon Fire
2024-03-21 17:00:55 -04:00
parent 6aabebc15f
commit 0a99c865ef
+18 -9
View File
@@ -1,6 +1,7 @@
const Command = require('../../framework/Command'); const Command = require('../../framework/Command');
const request = require('node-superfetch'); const request = require('node-superfetch');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const UserAgent = require('user-agents');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
const { list } = require('../../util/Util'); const { list } = require('../../util/Util');
const games = require('../../assets/json/20-questions'); const games = require('../../assets/json/20-questions');
@@ -36,8 +37,9 @@ module.exports = class TwentyQuestionsCommand extends Command {
const current = this.client.games.get(msg.channel.id); const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
try { try {
const startURL = await this.initialize(game); const agent = new UserAgent().toString();
let question = await this.startGame(game, startURL); const startURL = await this.initialize(game, agent);
let question = await this.startGame(game, startURL, agent);
let win = null; let win = null;
this.client.games.set(msg.channel.id, { name: this.name }); this.client.games.set(msg.channel.id, { name: this.name });
while (win === null) { while (win === null) {
@@ -64,7 +66,7 @@ module.exports = class TwentyQuestionsCommand extends Command {
break; break;
} }
const answer = question.answers[answers.indexOf(choice)]; const answer = question.answers[answers.indexOf(choice)];
question = await this.nextQuestion(game, answer.href, question.url); question = await this.nextQuestion(game, answer.href, question.url, agent);
if (typeof question.win !== 'undefined') { if (typeof question.win !== 'undefined') {
win = question.win; win = question.win;
break; break;
@@ -87,20 +89,24 @@ module.exports = class TwentyQuestionsCommand extends Command {
return `http://${game}.20q.net`; return `http://${game}.20q.net`;
} }
async initialize(game) { async initialize(game, agent) {
const { text } = await request const { text } = await request
.get(`${this.makeBaseURI(game)}/gs${game ? 'e' : 'q-en'}`) .get(`${this.makeBaseURI(game)}/gs${game ? 'e' : 'q-en'}`)
.set({ Referer: 'http://www.20q.net/' }); .set({
Referer: 'http://www.20q.net/',
'User-Agent': agent
});
const $ = cheerio.load(text); const $ = cheerio.load(text);
return $('form').first().attr('action'); return $('form').first().attr('action');
} }
async startGame(game, startURL) { async startGame(game, startURL, agent) {
const { text } = await request const { text } = await request
.post(`${this.makeBaseURI(game)}${startURL}`) .post(`${this.makeBaseURI(game)}${startURL}`)
.set({ .set({
Origin: `${this.makeBaseURI(game)}/`, Origin: `${this.makeBaseURI(game)}/`,
Referer: `${this.makeBaseURI(game)}/gs${game ? 'e' : 'q-en'}` Referer: `${this.makeBaseURI(game)}/gs${game ? 'e' : 'q-en'}`,
'User-Agent': agent
}) })
.attach({ .attach({
submit: ' Play ', submit: ' Play ',
@@ -122,10 +128,13 @@ module.exports = class TwentyQuestionsCommand extends Command {
}; };
} }
async nextQuestion(game, url, referer) { async nextQuestion(game, url, referer, agent) {
const { text } = await request const { text } = await request
.get(url) .get(url)
.set({ Referer: referer }); .set({
Referer: referer,
'User-Agent': agent
});
const $ = cheerio.load(text); const $ = cheerio.load(text);
const win = $('h2').first().text(); const win = $('h2').first().text();
if (win === '20Q won!') { if (win === '20Q won!') {