78 lines
2 KiB
JavaScript
Executable file
78 lines
2 KiB
JavaScript
Executable file
const { literal, argument, greedyString } = require('brigadier-commands')
|
|
const ud = require('urban-dictionary')
|
|
|
|
module.exports = {
|
|
register (dispatcher) {
|
|
const node = dispatcher.register(
|
|
literal('urban')
|
|
.then(
|
|
argument('word', greedyString())
|
|
.executes(c => this.urbanCommand(c))
|
|
)
|
|
)
|
|
|
|
node.description = 'Shows word definitions from the Urban Dictionary'
|
|
node.permissionLevel = 0
|
|
},
|
|
|
|
async urbanCommand (context) {
|
|
const source = context.source
|
|
|
|
const definitions = await ud.define(context.getArgument('word'))
|
|
.catch(error => source.sendError(error.toString()))
|
|
if (!definitions) return
|
|
|
|
const msg = [{ text: '', color: 'gray' }]
|
|
for (const definition of definitions) {
|
|
msg.push(
|
|
{ text: '[', color: 'dark_gray' },
|
|
{ text: 'Urban', color: 'red' },
|
|
{ text: '] ', color: 'dark_gray' },
|
|
{ text: `${definition.word} `, bold: true },
|
|
...this.parseDefinitionText(definition.definition, source),
|
|
'\n'
|
|
)
|
|
}
|
|
|
|
msg.pop()
|
|
source.sendFeedback(msg, false)
|
|
},
|
|
|
|
parseDefinitionText (text, source) {
|
|
const prefix = source.bot.commands.prefixes[0]
|
|
const texts = []
|
|
let string = ''
|
|
const state = { i: 0 }
|
|
|
|
while (state.i < text.length) {
|
|
texts.push(this.readUntil(text, '[', state))
|
|
state.i++
|
|
|
|
if (state.i >= text.length) break // we are already done reading
|
|
|
|
const subword = this.readUntil(text, ']', state)
|
|
texts.push({ text: subword, underlined: true, clickEvent: { action: 'suggest_command', value: prefix + 'urban ' + subword } })
|
|
state.i++
|
|
}
|
|
|
|
return texts
|
|
},
|
|
|
|
readUntil (string, terminator, state) {
|
|
let substr = ''
|
|
for (; state.i < string.length && string[state.i] !== terminator; state.i++) {
|
|
const i = state.i
|
|
const c = string[i]
|
|
|
|
if (c === '\r') {
|
|
substr += '\n'
|
|
state.i += string[i + 1] === '\n' ? 2 : 1
|
|
continue
|
|
}
|
|
|
|
substr += c
|
|
}
|
|
|
|
return substr
|
|
}
|
|
}
|