Files
xiao/commands/search/urban-dictionary.js
T
Daniel Odendahl Jr 0ebd66a3c9 Database
2018-02-17 14:05:01 +00:00

59 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { shorten, list } = require('../../util/Util');
const types = ['random', 'top'];
module.exports = class UrbanDictionaryCommand extends Command {
constructor(client) {
super(client, {
name: 'urban-dictionary',
aliases: ['urban', 'define-urban'],
group: 'search',
memberName: 'urban-dictionary',
description: 'Defines a word, but with Urban Dictionary.',
details: `**Types**: ${types.join(', ')}`,
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'word',
prompt: 'What word would you like to look up?',
type: 'string'
},
{
key: 'type',
prompt: 'Do you want to get the top answer or a random one?',
type: 'string',
default: 'top',
validate: type => {
if (types.includes(type.toLowerCase())) return true;
return `Invalid type, please enter either ${list(types, 'or')}.`;
},
parse: type => type.toLowerCase()
}
]
});
}
async run(msg, { word, type }) {
try {
const { body } = await snekfetch
.get('http://api.urbandictionary.com/v0/define')
.query({ term: word });
if (!body.list.length) return msg.say('Could not find any results.');
const data = body.list[type === 'top' ? 0 : Math.floor(Math.random() * body.list.length)];
const embed = new MessageEmbed()
.setColor(0x32A8F0)
.setAuthor('Urban Dictionary', 'https://i.imgur.com/Fo0nRTe.png')
.setURL(data.permalink)
.setTitle(data.word)
.setDescription(shorten(data.definition))
.addField(' Example',
data.example ? shorten(data.example, 1000) : 'None');
return msg.embed(embed);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};