Google, Toxicity (Perspective)

This commit is contained in:
Daniel Odendahl Jr
2018-02-21 22:16:19 +00:00
parent c9d7d89f13
commit 33d1c2d0a2
4 changed files with 89 additions and 2 deletions
+41
View File
@@ -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!`);
}
}
};
+46
View File
@@ -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)}`);
}
}
};
+1 -1
View File
@@ -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.',
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "65.3.3",
"version": "65.4.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {