73 lines
2 KiB
JavaScript
Executable file
73 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(this.urbanCommand.bind(this))
|
|
)
|
|
)
|
|
|
|
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.prefix
|
|
const texts = []
|
|
let string = ''
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
let c = text[i]
|
|
|
|
if (c === '[') {
|
|
if (string) texts.push(string)
|
|
string = ''
|
|
let subword = ''
|
|
i++
|
|
for (; i < text.length; i++) {
|
|
c = text[i]
|
|
if (c === ']') {
|
|
if (subword) texts.push({ text: subword, underlined: true, clickEvent: { action: 'suggest_command', value: prefix + 'urban ' + subword } })
|
|
subword = ''
|
|
break
|
|
}
|
|
else subword += c
|
|
}
|
|
if (subword) texts.push({ text: subword, underlined: true, clickEvent: { action: 'suggest_command', value: prefix + 'urban ' + subword } })
|
|
continue
|
|
}
|
|
|
|
string += c
|
|
}
|
|
|
|
if (string) texts.push(string)
|
|
return texts
|
|
}
|
|
}
|