chipmunkbot3/commands/test.js
Chipmunk 6332d9baa0 Parse other vanilla formats
comes with an experimental mediawiki scraper
2024-08-04 16:50:34 -04:00

42 lines
1.4 KiB
JavaScript

const { literal, argument, string, greedyString, SimpleCommandExceptionType } = require('brigadier-commands')
const TextMessage = require('../util/command/text_message')
const { parseDocument, DomUtils } = require('htmlparser2')
const WPTEXTBOX1_PARSE_ERROR = new SimpleCommandExceptionType(new TextMessage('Unable to parse edit page (wpTextbox1 is missing!)'))
module.exports = {
register (dispatcher) {
const node = dispatcher.register(
literal('test')
.then(
argument('url', string())
.then(
argument('title', greedyString())
.executes(c => this.testCommand(c))
)
)
)
node.description = 'Tests something'
node.permissionLevel = 0
},
async testCommand (context) {
const source = context.source
const bot = source.bot
let baseUrl = context.getArgument('url')
const title = context.getArgument('title')
if (!baseUrl.includes('://')) baseUrl = 'http://' + baseUrl
if (baseUrl[baseUrl.length - 1] !== '/') baseUrl += '/'
const response = await fetch(`${baseUrl}index.php?title=${encodeURIComponent(title)}&action=edit`)
const html = await response.text()
const document = parseDocument(html)
const wpTextbox1 = DomUtils.getElementById('wpTextbox1', document.children)
if (!wpTextbox1) throw WPTEXTBOX1_PARSE_ERROR.create()
const text = DomUtils.getText(wpTextbox1)
source.sendFeedback(text, false)
}
}