Parse other vanilla formats

comes with an experimental mediawiki scraper
This commit is contained in:
Chipmunk 2024-08-04 16:50:34 -04:00
parent f8712bd513
commit 6332d9baa0
2 changed files with 43 additions and 1 deletions

42
commands/test.js Normal file
View file

@ -0,0 +1,42 @@
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)
}
}

View file

@ -4,7 +4,7 @@ const json5 = require('json5')
function parseMessage (message, data) {
if (message === null || typeof message !== 'object') return
if (message.translate !== 'chat.type.text' || !Array.isArray(message.with) || message.with.length !== 2) return
if (!message.translate?.startsWith?.('chat.type.') || !Array.isArray(message.with) || message.with.length !== 2) return
const [displayName, contents] = message.with