forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.py
65 lines (59 loc) · 2.43 KB
/
discord.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
from uuid import UUID
import requests
from loguru import logger
from oasst_backend.celery_worker import app as celery_app
from oasst_backend.config import settings
ROOT_ENDPOINT = "https://discord.com/api/v10"
@celery_app.task(name="send_new_report_message")
def send_new_report_message(message_details: dict, label_text: str, user_id: UUID):
"""
Send a message to the Discord channel when a new message is flagged.
Note: this is a Celery task.
Args:
message_details (dict): some of the attributes of a Message instance that we will use to compose the discord
message.
label_text (str): the label text
user_id (UUID): the user ID
"""
if settings.DISCORD_API_KEY is None or settings.DISCORD_CHANNEL_ID is None:
return
try:
logger.debug("Sending flagged message to Discord")
label_text = label_text[:4096] # 4096 is the max length of discord embed description
message_content_embed = {
"title": "Message content",
"description": message_details["message_text"],
"color": 0x3498DB, # Blue
"footer": {
"text": (
f"Role: {message_details['role']}\t "
f"Lang: {message_details['lang']}\t "
f"👍{message_details['thumbs_up']} "
f"👎{message_details['thumbs_down']} "
f"🚩{message_details['red_flag']}"
)
},
}
label_text_embed = {
"title": "Report content",
"description": f"{label_text}",
"color": 0xE74C3C, # Red
"author": {
"name": f"User ID: {user_id}",
"url": f"https://open-assistant.io/admin/manage_user/{user_id}",
},
}
res = requests.post(
f"{ROOT_ENDPOINT}/channels/{settings.DISCORD_CHANNEL_ID}/messages",
headers={
"user-agent": "DiscordBot (https://open-assistant.io, 1)",
"authorization": f"Bot {settings.DISCORD_API_KEY}",
},
json={
"content": f"New flagged message https://open-assistant.io/admin/messages/{message_details['message_id']}",
"embeds": [message_content_embed, label_text_embed],
},
)
res.raise_for_status()
except Exception as e:
logger.exception(f"Failed to send flagged message. error: {e}")