/[sudobot]/branches/4.x/src/commands/moderation/AIModStatsCommand.ts
ViewVC logotype

Annotation of /branches/4.x/src/commands/moderation/AIModStatsCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months, 1 week ago) by rakinar2
File MIME type: application/typescript
File size: 5268 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { Message, Permissions, Util } from "discord.js";
21     import DiscordClient from "../../client/Client";
22     import CommandOptions from "../../types/CommandOptions";
23     import BaseCommand from "../../utils/structures/BaseCommand";
24     import { google } from "googleapis";
25     import MessageEmbed from "../../client/MessageEmbed";
26     import { emoji } from "../../utils/Emoji";
27    
28     export default class AIModStats extends BaseCommand {
29     permissions = [Permissions.FLAGS.MANAGE_MESSAGES];
30     name = "aimodstats";
31     category = "moderation";
32     aliases = ['aimodtest'];
33    
34     async run(discordClient: DiscordClient, message: Message, options: CommandOptions) {
35     if (options.args[0] === undefined) {
36     await message.reply(":x: Please specify a text to test.");
37     return;
38     }
39    
40     if (!process.env.PERSPECTIVE_API_TOKEN) {
41     return;
42     }
43    
44     const DISCOVERY_URL = "https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1";
45     const content = message.content.slice(discordClient.config.props[message.guildId!].prefix.length).trim().slice(options.cmdName.length).trim();
46     const time = Date.now();
47    
48     google
49     .discoverAPI(DISCOVERY_URL)
50     .then((client) => {
51     const analyzeRequest = {
52     comment: {
53     text: content,
54     },
55     requestedAttributes: {
56     TOXICITY: {},
57     THREAT: {},
58     SEVERE_TOXICITY: {},
59     },
60     };
61    
62     (client.comments as any).analyze(
63     {
64     key: process.env.PERSPECTIVE_API_TOKEN,
65     resource: analyzeRequest,
66     },
67     (err: any, response: any) => {
68     if (err) {
69     console.log(err);
70     return;
71     }
72    
73     console.log(JSON.stringify(response.data.attributeScores, null, 4));
74    
75     const config = discordClient.config.props[message.guildId!].ai_mod;
76     const threat = response.data.attributeScores.THREAT.summaryScore.value >= (config?.threat ?? 0.8);
77     const toxic = response.data.attributeScores.TOXICITY.summaryScore.value >= (config?.toxicity ?? 0.8);
78     const severeToxic = response.data.attributeScores.SEVERE_TOXICITY.summaryScore.value >= (config?.severe_toxicity ?? 0.8);
79    
80     const embed = new MessageEmbed({
81     title: "AI Moderator Message Analysis",
82     description: `**Input Message:**\n\`\`\`\n${Util.escapeCodeBlock(content)}\`\`\``,
83     fields: [
84     {
85     name: 'Analysis Results',
86     value: `Toxicity: ${response.data.attributeScores.TOXICITY.summaryScore.value * 100}% (${(config?.toxicity ?? 0.8) * 100}% maximum)\nSevere Toxicity: ${response.data.attributeScores.SEVERE_TOXICITY.summaryScore.value * 100}% (${(config?.severe_toxicity ?? 0.8) * 100}% maximum)\nThreat: ${response.data.attributeScores.THREAT.summaryScore.value * 100}% (${(config?.threat ?? 0.8) * 100}% maximum)`
87     },
88     ],
89     footer: {
90     text: `Took ${((Date.now() - time) / 1000).toFixed(1)} seconds to analyze`
91     }
92     })
93     .setTimestamp();
94    
95     if (toxic || threat || severeToxic) {
96     embed.addFields({
97     name: 'Summary',
98     value: `:x: This message is ${threat ? 'too threatful' : (toxic ? 'too toxic' : (severeToxic ? 'severely toxic' : ''))}`
99     });
100     }
101     else {
102     embed.addFields({
103     name: 'Summary',
104     value: `${emoji('check')} This message looks good.`
105     });
106     }
107    
108     message.reply({ embeds: [embed] }).catch(console.log);
109     }
110     )
111     })
112     .catch((err) => {
113     console.log(err);
114     });
115     }
116     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26