-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoderation.py
71 lines (61 loc) · 2.35 KB
/
moderation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from constants import (
SERVER_TO_MODERATION_CHANNEL,
MODERATION_VALUES_FOR_BLOCKED,
MODERATION_VALUES_FOR_FLAGGED,
)
from openai import OpenAI
client = OpenAI()
from typing import Optional, Tuple
import discord
from utils import logger
def moderate_message(
message: str, user: str
) -> Tuple[str, str]: # [flagged_str, blocked_str]
moderation_response = client.moderations.create(input=message, model="text-moderation-latest")
category_scores = moderation_response.results[0].category_scores or {}
blocked_str = ""
flagged_str = ""
for category, score in category_scores.items():
if score > MODERATION_VALUES_FOR_BLOCKED.get(category, 1.0):
blocked_str += f"({category}: {score})"
logger.info(f"blocked {user} {category} {score}")
break
if score > MODERATION_VALUES_FOR_FLAGGED.get(category, 1.0):
flagged_str += f"({category}: {score})"
logger.info(f"flagged {user} {category} {score}")
return (flagged_str, blocked_str)
async def fetch_moderation_channel(
guild: Optional[discord.Guild],
) -> Optional[discord.abc.GuildChannel]:
if not guild or not guild.id:
return None
moderation_channel = SERVER_TO_MODERATION_CHANNEL.get(guild.id, None)
if moderation_channel:
channel = await guild.fetch_channel(moderation_channel)
return channel
return None
async def send_moderation_flagged_message(
guild: Optional[discord.Guild],
user: str,
flagged_str: Optional[str],
message: Optional[str],
url: Optional[str],
):
if guild and flagged_str and len(flagged_str) > 0:
moderation_channel = await fetch_moderation_channel(guild=guild)
if moderation_channel:
message = message[:100] if message else None
await moderation_channel.send(
f"⚠️ {user} - {flagged_str} - {message} - {url}"
)
async def send_moderation_blocked_message(
guild: Optional[discord.Guild],
user: str,
blocked_str: Optional[str],
message: Optional[str],
):
if guild and blocked_str and len(blocked_str) > 0:
moderation_channel = await fetch_moderation_channel(guild=guild)
if moderation_channel:
message = message[:500] if message else None
await moderation_channel.send(f"❌ {user} - {blocked_str} - {message}")