From 2b899daf1659f99994c0d0cd6e46cb0f15da6c4a Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Sat, 4 Feb 2023 00:41:41 +0100 Subject: [PATCH] perspective command v0 --- commands/message/mod/perspective.js | 105 ++++++++++++++++++++++++++++ labscore/api/endpoints.js | 1 + labscore/api/index.js | 6 ++ labscore/utils/statics.js | 5 ++ 4 files changed, 117 insertions(+) create mode 100644 commands/message/mod/perspective.js diff --git a/commands/message/mod/perspective.js b/commands/message/mod/perspective.js new file mode 100644 index 0000000..876c585 --- /dev/null +++ b/commands/message/mod/perspective.js @@ -0,0 +1,105 @@ +const { Constants } = require("detritus-client"); +const { perspective } = require("../../../labscore/api"); +const { format } = require("../../../labscore/utils/ansi"); +const { createEmbed } = require("../../../labscore/utils/embed"); +const Permissions = Constants.Permissions; + +const { icon, pill, codeblock, iconPill, highlight } = require("../../../labscore/utils/markdown"); +const { editOrReply } = require("../../../labscore/utils/message"); +const { STATICS } = require("../../../labscore/utils/statics"); +const { getMember } = require("../../../labscore/utils/users"); + + +function getPerspectiveColor(score){ + if(score >= 0.8) return "r" + if(score >= 0.3) return "y" + return "g" +} + +function formatPerspectiveScores(data){ + let entries = []; + let srt = []; + let len = Math.max(...(Object.keys(data.scores).map(el => el.length))) + 3; + + for(const scr of Object.keys(data.scores)){ + let score = data.scores[scr]; + perc = `${score.toString().substr(2,2)}.${score.toString().substr(3,1)}` + if(perc.startsWith('0')) perc = ` ${perc.substr(1, perc.length)}` + srt.push(`${data.scores[scr]}|${format(perc + '%', getPerspectiveColor(score))} ${scr.substr(0,1).toUpperCase()}${scr.substr(1,scr.length).toLowerCase().replace(/_/g, ' ')}`) + } + for(const i of srt.sort().reverse()) entries.push(i.split('|')[1]) + return entries +} + +module.exports = { + label: "input", + name: "perspective", + metadata: { + // description: `Uses PerspectiveAPI to grade the toxicity of a prompt or user.`, + // description_short: 'Toxicity scores for prompts or users.', + description: `Uses Perspective to judge the toxicity of a prompt.`, + description_short: `Toxicity scores for prompts.`, + examples: ['perspective I hate otters.'], + category: 'mod', + // usage: 'perspective OR perspective ' + usage: 'perspective ' + }, + ratelimit: { + type: 'guild', + limit: 1, + duration: 5000 + }, + permissionsClient: [Permissions.MANAGE_MESSAGES], + run: async (context, args) => { + await context.triggerTyping(); + + try{ + // The per-user scanning is coming soon ;) + + // let u = await getMember(context, args.input) + // if(!u){ // Assume its a prompt + let perspectiveApi = await perspective(context, [ args.input ]) + + return await editOrReply(context, { embeds: [createEmbed("default", context, { + description: `${iconPill("rules", "Scores")} ${codeblock("ansi", formatPerspectiveScores(perspectiveApi.response.body))}`, + footer: { + iconUrl: STATICS.perspectiveapi, + text: `Perspective • ${context.application.name}` + } + })] }) + // } + + // let response = await editOrReply(context, { embeds: [createEmbed("loading", context, `Collecting messages...`)] }) + + // const messages = await context.message.channel.fetchMessages({limit: args.amount}); + // + // await response.edit({ embeds: [createEmbed("loading", context, `Analyzing messages...`)] }) + // + // let messageContent = []; + + // for(const m of messages){ + // // User limit + // if(m[1].author.id == u.id && m[1].content.length >= 1) messageContent.push(m[1].content) + // } + + // console.log(messageContent) + + // if(messageContent.length == 0){ + // return await response.edit({ embeds: [createEmbed("warning", context, `No content found that could be analyzed.`)] }) + // } + + // let perspectiveApi = await perspective(context, messageContent) + + // return await editOrReply(context, { embeds: [createEmbed("default", context, { + // description: `Analyzed ${iconPill("analytics", `${messageContent.length} Messages`)} by <@${u.id}>.\n\n${formatPerspectiveScores(perspectiveApi.response.body).join('\n')}`, + // footer: { + // iconUrl: STATICS.perspectiveapi, + // text: `Perspective • ${context.application.name}` + // } + // })] }) + }catch(e){ + await editOrReply(context, { embeds: [createEmbed("error", context, `Something went wrong.`)] }) + console.log(e) + } + } +}; \ No newline at end of file diff --git a/labscore/api/endpoints.js b/labscore/api/endpoints.js index d25fe1a..49d3b1f 100644 --- a/labscore/api/endpoints.js +++ b/labscore/api/endpoints.js @@ -50,6 +50,7 @@ const Api = Object.freeze({ UTILS_EMOJIPEDIA: '/utils/emojipedia', UTILS_INFERKIT: '/utils/inferkit', + UTILS_PERSPECTIVE: '/utils/perspective', UTILS_SCREENSHOT: '/utils/screenshot', }) diff --git a/labscore/api/index.js b/labscore/api/index.js index 33e7cd9..f698e6b 100644 --- a/labscore/api/index.js +++ b/labscore/api/index.js @@ -287,6 +287,12 @@ module.exports.inferkit = async function(context, input){ }) } +module.exports.perspective = async function(context, content = []){ + return await request(Api.UTILS_PERSPECTIVE, "GET", {}, { + input: content.join('\n\n') + }) +} + module.exports.screenshot = async function(context, url, nsfw){ return await request(Api.UTILS_SCREENSHOT, "GET", {}, { url: url, diff --git a/labscore/utils/statics.js b/labscore/utils/statics.js index 76431e5..ad3d443 100644 --- a/labscore/utils/statics.js +++ b/labscore/utils/statics.js @@ -32,6 +32,10 @@ const Statics = Object.freeze({ file: "brands/openweathermap.png", revision: 0 }, + perspectiveapi: { + file: "brands/perspectiveapi.png", + revision: 0 + }, photofunia: { file: "brands/photofunia.png", revision: 1 @@ -97,6 +101,7 @@ module.exports.STATICS = Object.freeze({ inferkit: staticAsset(Statics.brands.inferkit), makesweet: staticAsset(Statics.brands.makesweet), openweathermap: staticAsset(Statics.brands.openweathermap), + perspectiveapi: staticAsset(Statics.brands.perspectiveapi), photofunia: staticAsset(Statics.brands.photofunia), quora: staticAsset(Statics.brands.quora), reddit: staticAsset(Statics.brands.reddit),