mirror of
https://github.com/jcreek/MupBot.git
synced 2026-07-12 18:33:44 +00:00
Merge pull request #1 from OllyNicholass/master
Add urban dictionary command, add arguments to the help command, and tweak the helper methods
This commit is contained in:
+75
-4
@@ -1,4 +1,5 @@
|
||||
const { sendEmbedMessage } = require('./helpers.js');
|
||||
const { generateEmbedMessage, trim } = require('./helpers.js');
|
||||
const axios = require('axios');
|
||||
|
||||
module.exports = {
|
||||
matchCommand: function (Discord, config, logger, message, command, args) {
|
||||
@@ -6,6 +7,9 @@ module.exports = {
|
||||
case 'help':
|
||||
commandHelp(Discord, config, logger, message, command, args);
|
||||
break;
|
||||
case 'ud':
|
||||
commandUrbanDictionary(Discord, config, logger, message, command, args);
|
||||
break;
|
||||
default:
|
||||
message.channel.send(`That command is not one I know yet - but you could add it! To find out more visit ${config.github_url}`);
|
||||
}
|
||||
@@ -13,10 +17,77 @@ module.exports = {
|
||||
};
|
||||
|
||||
function commandHelp (Discord, config, logger, message, command, args) {
|
||||
const msg = `
|
||||
const helpMessage = `
|
||||
The commands available to you are:
|
||||
- ${config.prefix}commandgoeshere - Do something
|
||||
\`${config.prefix}ud <query>\` - Search Urban Dictionary
|
||||
\`${config.prefix}help <command>\` - For help with a specific command add the command as an argument
|
||||
`;
|
||||
|
||||
message.channel.send(msg);
|
||||
// Specify specific command help
|
||||
if (args.length > 0) {
|
||||
switch (args[0]) {
|
||||
case 'ud':
|
||||
message.channel.send(`Usage: \`${config.prefix}ud <query>\` *Example: ${config.prefix}ud yeet*`);
|
||||
break;
|
||||
default:
|
||||
message.channel.send(`That command is not one I know yet - but you could add it! To find out more visit ${config.github_url}`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
message.channel.send(helpMessage);
|
||||
}
|
||||
}
|
||||
|
||||
function commandUrbanDictionary(Discord, config, logger, message, command, args) {
|
||||
if (!args.length) {
|
||||
//If the command is used incorrectly without arguments
|
||||
return message.channel.send(`You didn't provide any arguments, ${message.author}!\nCorrect Usage: \`${config.prefix}ud <query>\``);
|
||||
}
|
||||
else if (args[0] != "") {
|
||||
const searchQuery = args[0];
|
||||
|
||||
// Send a GET request to urban dictionary including the search query
|
||||
axios({
|
||||
method: 'get',
|
||||
url: 'http://api.urbandictionary.com/v0/define',
|
||||
headers: {"Content-Type": "application/json"},
|
||||
params: {"term": searchQuery}
|
||||
})
|
||||
.then(function(response) {
|
||||
const answers = response.data.list;
|
||||
if (answers.length > 0) {
|
||||
try {
|
||||
// Set up rich embed for the command to return the API response, add the first responses URL with an example of the usage and rating
|
||||
const embed = generateEmbedMessage(Discord, logger, message, answers[0].word, trim(Discord, logger, answers[0].definition, 1024));
|
||||
embed.setURL(answers[0].permalink)
|
||||
.addFields(
|
||||
{ name: 'Example', value: trim(Discord, logger, answers[0].example, 1024) },
|
||||
{ name: 'Rating', value: `:thumbsup: ${answers[0].thumbs_up} :thumbsdown: ${answers[0].thumbs_down}` }
|
||||
);
|
||||
|
||||
if (answers.length > 1) {
|
||||
// If more than 1 definition is found, add additional fields to show the definitions of up to another 3 results
|
||||
embed.addField('Other Definitions', 'Below are some that didn\t quite cut the mustard.')
|
||||
|
||||
for (let i = 1; i < answers.length && i < 4; i++) {
|
||||
const ele = answers[i];
|
||||
embed.addField(`${i}. `, `[${ele.word}](${ele.permalink})\r\n${trim(Discord, logger, ele.definition, 1024)}`)
|
||||
}
|
||||
}
|
||||
message.channel.send(embed);
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Failed to send embed message', error);
|
||||
message.channel.send('There was a problem, sorry!');
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If the API returns no results for the specified query
|
||||
message.channel.send(`There are no results for *${args[0]}*`)
|
||||
}
|
||||
})
|
||||
.catch(function (error) {
|
||||
logger.error('Failed to make GET request to Urban Dictionary API', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+30
-16
@@ -1,19 +1,33 @@
|
||||
module.exports = {
|
||||
sendEmbedMessage: function (Discord, logger, message, title, description, colour = '#F4B400') {
|
||||
// For documentation see https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/first-bot/using-embeds-in-messages.md
|
||||
try {
|
||||
const embed = new Discord.MessageEmbed()
|
||||
.setTitle(title)
|
||||
.setColor(colour)
|
||||
.setDescription(description)
|
||||
.setFooter('This is a community-made open source bot - if it goes down, fix it')
|
||||
.setThumbnail('https://cdn.discordapp.com/icons/333376110329069578/b5cf0e90b74a3e1b828c15a262b7a5c8.png?size=128')
|
||||
.setTimestamp();
|
||||
|
||||
message.channel.send(embed);
|
||||
} catch (error) {
|
||||
logger.error('Failed to send embed message', error);
|
||||
message.channel.send('There was a problem, sorry!');
|
||||
// Return a stylised embed for consistancy - this allows you to add additional properties like fields
|
||||
generateEmbedMessage : function (Discord, logger, message, title, description, colour = '#F4B400') {
|
||||
// For documentation see https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/first-bot/using-embeds-in-messages.md
|
||||
try {
|
||||
const embed = new Discord.MessageEmbed()
|
||||
.setTitle(title)
|
||||
.setColor(colour)
|
||||
.setDescription(description)
|
||||
.setFooter('This is a community-made open source bot - if it goes down, fix it')
|
||||
.setThumbnail('https://cdn.discordapp.com/icons/333376110329069578/b5cf0e90b74a3e1b828c15a262b7a5c8.png?size=128')
|
||||
.setTimestamp();
|
||||
return embed;
|
||||
} catch (error) {
|
||||
logger.error('Failed to send embed message', error);
|
||||
message.channel.send('There was a problem, sorry!');
|
||||
}
|
||||
},
|
||||
// Trim a string to a specific character length
|
||||
trim: function (Discord, logger, string, max) {
|
||||
try {
|
||||
if (string.length > max) {
|
||||
return `${string.slice(0, max - 3)}...`;
|
||||
}
|
||||
},
|
||||
else {
|
||||
return string;
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to trim selection', error);
|
||||
message.channel.send('There was a problem, sorry!');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Generated
+13
@@ -70,6 +70,14 @@
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
||||
},
|
||||
"axios": {
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
|
||||
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"chalk": {
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
|
||||
@@ -213,6 +221,11 @@
|
||||
"resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz",
|
||||
"integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw=="
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.13.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
|
||||
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
|
||||
},
|
||||
"has-ansi": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"author": "jcreek",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"discord.js": "12.4.1",
|
||||
"winston": "3.3.3",
|
||||
"winston-elasticsearch": "^0.7.12"
|
||||
|
||||
Reference in New Issue
Block a user