-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
224 lines (183 loc) · 7.99 KB
/
message.py
File metadata and controls
224 lines (183 loc) · 7.99 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from discord import Message
import json
import re
def json_from(message: Message):
attachments = []
embeds = []
mentions = []
role_mentions = []
channel_mentions = []
for attachment in message.attachments:
attachment_payload = []
attachment_payload["id"] = attachment.id
attachment_payload["filename"] = attachment.filename
attachment_payload["content_type"] = attachment.content_type
attachment_payload["size"] = attachment.size
attachment_payload["url"] = attachment.url
attachment_payload["proxy_url"] = attachment.proxy_url
attachment_payload["height"] = attachment.height
attachment_payload["width"] = attachment.width
attachment_payload["description"] = attachment.description
attachments.append(attachment_payload)
for embed in message.embeds:
if embed:
embed_payload = {}
embed_payload["url"] = embed.url
embed_payload["description"] = embed.description
embed_payload["title"] = embed.title
embed_payload["author"] = embed.author.name
embed_payload["image_url"] = embed.image.url
embed_payload["thumbnail_url"] = embed.thumbnail.url
embed_payload["footer_text"] = embed.footer.text
embed_payload["footer_icon_url"] = embed.footer.icon_url
embed_payload["video_url"] = embed.video.url
embeds.append(embed_payload)
for mention in message.mentions:
mention_payload = {}
mention_payload["id"] = mention.id
mention_payload["name"] = mention.name
mention_payload["discriminator"] = mention.discriminator
mention_payload["display_name"] = mention.display_name
mention_payload["global_name"] = mention.global_name
mention_payload["bot"] = mention.bot
mention_payload["mention"] = mention.mention
mentions.append(mention_payload)
for mention in message.role_mentions:
mention_payload = {}
mention_payload["id"] = mention.id
mention_payload["name"] = mention.name
mention_payload["mention"] = mention.mention
mention_payload["position"] = mention.position
mention_payload["mentionable"] = mention.mentionable
mention_payload["hoist"] = mention.hoist
mention_payload["managed"] = mention.managed
role_mentions.append(mention_payload)
for mention in message.channel_mentions:
mention_payload = {}
mention_payload["id"] = mention.id
mention_payload["name"] = mention.name
mention_payload["mention"] = mention.mention
mention_payload["created_at"] = str(mention.created_at)
mention_payload["guild"] = mention.guild.id
mention_payload["category"] = mention.category.id
channel_mentions.append(mention_payload)
payload = {}
payload["content"] = message.content
payload["author"] = message.author.name
payload["channel_id"] = message.channel.id
payload["guild_id"] = message.guild.id
payload["id"] = message.id
payload["type"] = message.type.name
payload["attachments"] = attachments
payload["embeds"] = embeds
payload["mentions"] = mentions
payload["role_mentions"] = role_mentions
payload["channel_mentions"] = channel_mentions
return json.dumps(payload)
def title_from(data: dict) -> str:
post_title = "Untitled Post"
embeds = data.get("embeds", [])
content = data.get("content", "")
# --- Twitch Stream Detected ---
if embeds and "twitch.tv" in (embeds[0].get("url") or ""):
# Extract user from 'author' field if possible
embed_author = embeds[0].get("author", "")
user = embed_author.replace(" is live on Twitch", "").strip() if "is live on Twitch" in embed_author else embed_author
# Fallback to parsing from content if needed
if not user and "**" in content:
user = content.split("**")[1]
if user:
post_title = f"{user} is live!"
# --- YouTube Video Detected ---
elif embeds and "youtu" in (embeds[0].get("url") or ""):
embed_author = embeds[0].get("author", "")
if "published a new video" in embed_author:
user = embed_author.split(" published a new video")[0].strip()
post_title = f"{user} uploaded a video!"
# --- Fallback for plain content detection ---
elif "just posted a new video" in content:
# Example: "**ThatOneGuyJames** just posted a new video!"
user = content.split("**")[1] if "**" in content else "Unknown"
post_title = f"{user} uploaded a video!"
return post_title
async def description_from(data: dict, message: Message) -> str:
embeds = data.get("embeds", [])
if embeds and embeds[0].get("title"):
return embeds[0]["title"]
content = data.get("content", message.content or "")
# Sanitize @everyone and @here
content = re.sub(r"@everyone", "everyone", content)
content = re.sub(r"@here", "here", content)
# Replace channel mentions
def replace_channel(match):
channel_id = int(match.group(1))
channel = message.guild.get_channel(channel_id)
return f"#{channel.name}" if channel else "#channel"
content = re.sub(r"<#(\d+)>", replace_channel, content)
# Replace role mentions
def replace_role(match):
role_id = int(match.group(1))
role = message.guild.get_role(role_id)
return f"@{role.name}" if role else "@role"
content = re.sub(r"<@&(\d+)>", replace_role, content)
# Replace user mentions
async def replace_user(match):
user_id = int(match.group(1))
member = message.guild.get_member(user_id) or await message.guild.fetch_member(user_id)
return f"@{member.display_name}" if member else "@user"
# Process <@123> and <@!123> mentions one by one
user_mention_pattern = r"<@!?(\d+)>"
matches = list(re.finditer(user_mention_pattern, content))
for match in reversed(matches):
replacement = await replace_user(match)
start, end = match.span()
content = content[:start] + replacement + content[end:]
return content.strip()
def url_from(data: dict) -> str:
url_pattern = re.compile(r"https?://[^\s)>\]]+")
embeds = data.get("embeds", [])
content = data.get("content", "")
url = ""
# Check embeds first
if embeds and embeds[0].get("url"):
url = embeds[0]["url"]
# If no URL from embeds, search content
if not url:
match = url_pattern.search(content)
if match:
url = match.group(0)
return url
def thumbnail_url_from(data: dict) -> str:
embeds = data.get("embeds", [])
content = data.get("content", "")
img_url = ""
if embeds:
embed = embeds[0]
url = embed.get("url", "") or ""
# Step 1: image_url
if embed.get("image_url"):
img_url = embed["image_url"]
# Step 2: thumbnail_url
elif embed.get("thumbnail_url"):
img_url = embed["thumbnail_url"]
# Step 3: Construct YouTube thumbnail from URL
elif embed.get("url") and "youtu" in embed["url"]:
match = youtube_pattern.search(embed["url"])
if match:
video_id = match.group(1)
img_url = f"https://i.ytimg.com/vi/{video_id}/maxresdefault.jpg"
# Step 4: Construct Twitch thumbnail from URL
elif "twitch.tv" in url:
# Extract username from URL
match = re.search(r"twitch\.tv/([\w\d_]+)", url)
if match:
username = match.group(1).lower()
img_url = f"https://static-cdn.jtvnw.net/previews-ttv/live_user_{username}-880x496.jpg"
return img_url
async def handle(message: Message):
data = json.loads(json_from(message=message))
title = title_from(data=data)
description = await description_from(data=data, message=message)
url = url_from(data=data)
thumbnail = thumbnail_url_from(data=data)
return title, description, url, thumbnail