From 513d0aebd8e2582fe2cf96e484eaee1830fb2bd8 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 09:51:57 +0000 Subject: [PATCH 01/10] feat(*): Add Axios package to talk with APIs --- package-lock.json | 13 +++++++++++++ package.json | 1 + 2 files changed, 14 insertions(+) diff --git a/package-lock.json b/package-lock.json index 71c0989..966aaa0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 6c6142e..96b91f9 100644 --- a/package.json +++ b/package.json @@ -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" From 853b934607249ead1887e94a34eba79d037fc7be Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 11:18:49 +0000 Subject: [PATCH 02/10] feat(*): Add trim helper function --- helpers.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/helpers.js b/helpers.js index 19acdcf..b432cdb 100644 --- a/helpers.js +++ b/helpers.js @@ -16,4 +16,18 @@ module.exports = { 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!'); + } + } }; From 245903b6207be1357b2b6600ec3633580ec10323 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 11:41:35 +0000 Subject: [PATCH 03/10] feat(*): Change embed helper to return for more flexibilty --- helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helpers.js b/helpers.js index b432cdb..82a6586 100644 --- a/helpers.js +++ b/helpers.js @@ -1,4 +1,5 @@ module.exports = { + // Return a stylised embed for consistancy - this allows you to add additional properties like fields 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 { @@ -9,8 +10,7 @@ module.exports = { .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); + return embed; } catch (error) { logger.error('Failed to send embed message', error); message.channel.send('There was a problem, sorry!'); From 2e700311838f0791bd7fc4dd9d1246a8b70222ed Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 11:42:02 +0000 Subject: [PATCH 04/10] feat(*): Add urban dictionary command --- commands.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/commands.js b/commands.js index 26ecc4f..e07a906 100644 --- a/commands.js +++ b/commands.js @@ -1,4 +1,5 @@ -const { sendEmbedMessage } = require('./helpers.js'); +const { sendEmbedMessage, 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}`); } @@ -15,8 +19,62 @@ module.exports = { function commandHelp (Discord, config, logger, message, command, args) { const msg = ` The commands available to you are: -- ${config.prefix}commandgoeshere - Do something +- \`${config.prefix}ud \` - Search Urban Dictionary `; message.channel.send(msg); } + +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}!`); + } + else if (args[0] != "") { + var searchQuery = args[0]; + + // Send a POST 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 = sendEmbedMessage(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('Something went wrong', error); + }); + } +} From 28b54e10fa9ce282e97c461ea3b65dc9326c2a44 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 11:55:47 +0000 Subject: [PATCH 05/10] feat(*): Add arguments to help command --- commands.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/commands.js b/commands.js index e07a906..f2216cd 100644 --- a/commands.js +++ b/commands.js @@ -17,18 +17,33 @@ module.exports = { }; function commandHelp (Discord, config, logger, message, command, args) { - const msg = ` + const helpMessage = ` The commands available to you are: -- \`${config.prefix}ud \` - Search Urban Dictionary +\`${config.prefix}ud \` - Search Urban Dictionary +\`${config.prefix}help \` - 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 \` *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}!`); + return message.channel.send(`You didn't provide any arguments, ${message.author}!\nCorrect Usage: \`${config.prefix}ud \``); } else if (args[0] != "") { var searchQuery = args[0]; From 8fbef4dd8416e1414b42e5f371e88e920a1673d8 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 12:20:41 +0000 Subject: [PATCH 06/10] fix(*): Correct indentation in helper.js --- helpers.js | 56 +++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/helpers.js b/helpers.js index 82a6586..9bc108e 100644 --- a/helpers.js +++ b/helpers.js @@ -1,33 +1,33 @@ module.exports = { // Return a stylised embed for consistancy - this allows you to add additional properties like fields 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(); - 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!'); - } + // 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!'); + } + } }; From 39c34fbce18f5dfec6e387d5c66d5834ed7a37e1 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 12:21:55 +0000 Subject: [PATCH 07/10] fix(*): Rename sendEmbedMessage to generateEmbedMessage --- commands.js | 6 ++---- helpers.js | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/commands.js b/commands.js index f2216cd..94c1072 100644 --- a/commands.js +++ b/commands.js @@ -1,4 +1,4 @@ -const { sendEmbedMessage, trim } = require('./helpers.js'); +const { generateEmbedMessage, trim } = require('./helpers.js'); const axios = require('axios'); module.exports = { @@ -36,8 +36,6 @@ The commands available to you are: else { message.channel.send(helpMessage); } - - } function commandUrbanDictionary(Discord, config, logger, message, command, args) { @@ -60,7 +58,7 @@ function commandUrbanDictionary(Discord, config, logger, message, command, args) 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 = sendEmbedMessage(Discord, logger, message, answers[0].word, trim(Discord, logger, answers[0].definition, 1024)); + 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) }, diff --git a/helpers.js b/helpers.js index 9bc108e..beafeb3 100644 --- a/helpers.js +++ b/helpers.js @@ -1,6 +1,6 @@ module.exports = { // Return a stylised embed for consistancy - this allows you to add additional properties like fields - sendEmbedMessage: function (Discord, logger, message, title, description, colour = '#F4B400') { + 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() From 8a68c6c8d518dd853d9b4c7a9c71657c78bf3e80 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 12:23:26 +0000 Subject: [PATCH 08/10] fix(*): Correct comment for ud command get request --- commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.js b/commands.js index 94c1072..bce54bd 100644 --- a/commands.js +++ b/commands.js @@ -46,7 +46,7 @@ function commandUrbanDictionary(Discord, config, logger, message, command, args) else if (args[0] != "") { var searchQuery = args[0]; - // Send a POST request to urban dictionary including the search query + // Send a GET request to urban dictionary including the search query axios({ method: 'get', url: 'http://api.urbandictionary.com/v0/define', From 0cfaf2c240d9271f14b17382abeb81ada35972aa Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 12:24:51 +0000 Subject: [PATCH 09/10] fix(*): Update syntax of ud command to use const --- commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.js b/commands.js index bce54bd..7427e7a 100644 --- a/commands.js +++ b/commands.js @@ -44,7 +44,7 @@ function commandUrbanDictionary(Discord, config, logger, message, command, args) return message.channel.send(`You didn't provide any arguments, ${message.author}!\nCorrect Usage: \`${config.prefix}ud \``); } else if (args[0] != "") { - var searchQuery = args[0]; + const searchQuery = args[0]; // Send a GET request to urban dictionary including the search query axios({ From 40553fd0afa7f619b5be42cc8c3c218169f15dd5 Mon Sep 17 00:00:00 2001 From: Olly Nicholass Date: Fri, 15 Jan 2021 12:25:43 +0000 Subject: [PATCH 10/10] fix(*): Update ud command error message when making GET request --- commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.js b/commands.js index 7427e7a..f1319f2 100644 --- a/commands.js +++ b/commands.js @@ -87,7 +87,7 @@ function commandUrbanDictionary(Discord, config, logger, message, command, args) } }) .catch(function (error) { - logger.error('Something went wrong', error); + logger.error('Failed to make GET request to Urban Dictionary API', error); }); } }