diff --git a/commands/test.js b/commands/test.js new file mode 100644 index 0000000..5ecd127 --- /dev/null +++ b/commands/test.js @@ -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) + } +} diff --git a/util/chat/message_parser/vanilla.js b/util/chat/message_parser/vanilla.js index 8d1992c..09a6073 100644 --- a/util/chat/message_parser/vanilla.js +++ b/util/chat/message_parser/vanilla.js @@ -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