Add support for interactions in moderation
This commit is contained in:
parent
f6ac5281e7
commit
28dce0b29f
2 changed files with 36 additions and 12 deletions
|
@ -1,31 +1,39 @@
|
||||||
import { Collection, Message } from "discord.js";
|
import { Collection, InteractionResponse, Message } from "discord.js";
|
||||||
import { openai } from "./index";
|
import { openai } from "./index";
|
||||||
import { formatMessage } from "./toOpenAIMessages";
|
import { formatRequestOrResponse } from "./toOpenAIMessages";
|
||||||
|
|
||||||
export default class Moderation {
|
export default class Moderation {
|
||||||
/** Represents cache of messages that have been checked aganist OpenAI moderation API. */
|
/** Represents cache of messages that have been checked aganist OpenAI moderation API. */
|
||||||
private static cache = new Collection<string, boolean>();
|
private static cache = new Collection<string, boolean>();
|
||||||
|
|
||||||
public static async checkMessage(message: Message): Promise<boolean> {
|
public static async checkMessage(message: Message | InteractionResponse): Promise<boolean> {
|
||||||
if (this.cache.has(message.id)) {
|
if (this.cache.has(message.id)) {
|
||||||
return this.cache.get(message.id) as boolean;
|
return this.cache.get(message.id) as boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (message instanceof Message) {
|
||||||
const warningReaction = message.reactions.resolve("⚠");
|
const warningReaction = message.reactions.resolve("⚠");
|
||||||
// if bot reacted to that message, we already know that it returned true for moderation API
|
// if bot reacted to that message, we already know that it returned true for moderation API
|
||||||
if (warningReaction && warningReaction.me) {
|
if (warningReaction && warningReaction.me) {
|
||||||
this.cache.set(message.id, true);
|
this.cache.set(message.id, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const answer = await openai.createModeration({
|
const answer = await openai.createModeration({
|
||||||
input: formatMessage(message),
|
input: await formatRequestOrResponse(message),
|
||||||
});
|
});
|
||||||
|
|
||||||
const flagged = answer.data.results[0].flagged;
|
const flagged = answer.data.results[0].flagged;
|
||||||
this.cache.set(message.id, flagged);
|
this.cache.set(message.id, flagged);
|
||||||
if (flagged) message.react("⚠");
|
if (flagged) if (message instanceof Message) {
|
||||||
|
message.react("⚠");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const channelMessage = await message.fetch();
|
||||||
|
channelMessage.react("⚠");
|
||||||
|
}
|
||||||
|
|
||||||
return flagged;
|
return flagged;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,25 @@
|
||||||
import { ChatCompletionRequestMessage as OpenAIMessage } from "openai";
|
import { ChatCompletionRequestMessage as OpenAIMessage } from "openai";
|
||||||
import { Collection, Message as DiscordMessage } from "discord.js";
|
import { Collection, Message as DiscordMessage, InteractionResponse } from "discord.js";
|
||||||
import FoldToAscii from "fold-to-ascii";
|
import FoldToAscii from "fold-to-ascii";
|
||||||
|
|
||||||
import config from "./config";
|
import config from "./config";
|
||||||
import countTokens from "./tokenCounter";
|
import countTokens from "./tokenCounter";
|
||||||
|
import { request } from "./execution";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* formats the request to use as a message contend in OpenAI api
|
||||||
|
* @param request the request to format
|
||||||
|
* @returns the formatted request
|
||||||
|
*/
|
||||||
|
export async function formatRequestOrResponse(request: request | InteractionResponse): Promise<string> {
|
||||||
|
if (request instanceof DiscordMessage) {
|
||||||
|
return formatMessage(request);
|
||||||
|
}
|
||||||
|
if (request instanceof InteractionResponse) {
|
||||||
|
return formatMessage(await request.fetch());
|
||||||
|
}
|
||||||
|
return formatMessage(await request.fetchReply());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the message to use as a message content in OpenAI api
|
* Formats the message to use as a message content in OpenAI api
|
||||||
|
|
Loading…
Add table
Reference in a new issue