From 33d1c2d0a2795ee8301142eed253b67565c349a2 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 21 Feb 2018 22:16:19 +0000 Subject: [PATCH] Google, Toxicity (Perspective) --- commands/other/toxicity.js | 41 ++++++++++++++++++++++++++++++++ commands/search/google.js | 46 ++++++++++++++++++++++++++++++++++++ commands/text-edit/lmgtfy.js | 2 +- package.json | 2 +- 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 commands/other/toxicity.js create mode 100644 commands/search/google.js diff --git a/commands/other/toxicity.js b/commands/other/toxicity.js new file mode 100644 index 00000000..3b23ee2e --- /dev/null +++ b/commands/other/toxicity.js @@ -0,0 +1,41 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { GOOGLE_KEY } = process.env; + +module.exports = class ToxicityCommand extends Command { + constructor(client) { + super(client, { + name: 'toxicity', + aliases: ['perspective', 'comment-toxicity'], + group: 'other', + memberName: 'toxicity', + description: 'Determines the toxicity of some text.', + args: [ + { + key: 'text', + prompt: 'What text do you want to test the toxicity of?', + type: 'string' + } + ] + }); + } + + async run(msg, { text }) { + try { + const { body } = await snekfetch + .post('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze') + .query({ key: GOOGLE_KEY }) + .send({ + comment: { text }, + langauges: ['en'], + requestedAttributes: { TOXICITY: {} } + }); + const toxicity = Math.round(body.attributeScores.TOXICITY.summaryScore.value * 100); + if (toxicity < 70) return msg.say(`Likely to be perceived as toxic. (${toxicity}%)`); + if (toxicity < 40) return msg.say(`Unsure if this will be perceived as toxic. (${toxicity}%)`); + return msg.say(`Unlikely to be perceived as toxic. (${toxicity}%)`); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/google.js b/commands/search/google.js new file mode 100644 index 00000000..9f937944 --- /dev/null +++ b/commands/search/google.js @@ -0,0 +1,46 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { stripIndents } = require('common-tags'); +const { GOOGLE_KEY, CUSTOM_SEARCH_ID } = process.env; + +module.exports = class GoogleCommand extends Command { + constructor(client) { + super(client, { + name: 'google', + aliases: ['google-search', 'search-google'], + group: 'search', + memberName: 'google', + description: 'Searches Google for your query.', + args: [ + { + key: 'query', + prompt: 'What would you like to search for?', + type: 'string', + max: 1000 + } + ] + }); + } + + async run(msg, { query }) { + try { + const { body } = await snekfetch + .get('https://www.googleapis.com/customsearch/v1') + .query({ + key: GOOGLE_KEY, + cx: CUSTOM_SEARCH_ID, + q: query + }); + if (!body.items) return msg.say('Could not find any results.'); + const data = body.items[0]; + return msg.say(stripIndents` + **${data.title}** + ${data.snippet} + + <${data.formattedUrl}> + `); + } catch (err) { + return msg.say(`http://lmgtfy.com/?iie=1&q=${encodeURIComponent(query)}`); + } + } +}; diff --git a/commands/text-edit/lmgtfy.js b/commands/text-edit/lmgtfy.js index 6fadf01e..58e22b39 100644 --- a/commands/text-edit/lmgtfy.js +++ b/commands/text-edit/lmgtfy.js @@ -4,7 +4,7 @@ module.exports = class LMGTFYCommand extends Command { constructor(client) { super(client, { name: 'lmgtfy', - aliases: ['let-me-google-that-for-you', 'google'], + aliases: ['let-me-google-that-for-you'], group: 'text-edit', memberName: 'lmgtfy', description: 'Creates a LMGTFY link with the query you provide.', diff --git a/package.json b/package.json index a6d8a89c..c2f0e3e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "65.3.3", + "version": "65.4.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {