Repository navigation
Expand file tree
/
Copy pathmodlog.py
More file actions
150 lines (116 loc) · 5.76 KB
/
Copy pathmodlog.py
File metadata and controls
150 lines (116 loc) · 5.76 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
"""Builds the posts PauseBot makes in the moderators' log channel (MESSAGE_LOG_CHANNEL_ID) when a
message is edited or deleted, from what the message log in Postgres holds."""
import datetime
import discord
# Discord's limits: an embed's description holds 4096 characters, one embed 6000 in all, and one
# message up to 10 embeds that together hold at most 6000. Discord's client counts in UTF-16 code
# units (an emoji can be two), so everything here does too, to be safe either way.
DESCRIPTION_LIMIT = 4096
MESSAGE_EMBED_CHARS = 6000
MESSAGE_EMBEDS = 10
# Room left for the header lines (author, channel, link) in an edit's description
EDIT_TEXT_LIMIT = 1800
DELETE_TEXT_LIMIT = 2800
ATTACHMENTS_LIMIT = 1000
EDIT_COLOUR = discord.Colour.orange()
DELETE_COLOUR = discord.Colour.red()
FOOTER_PREFIX = "Message ID "
# Log posts never ping, whatever the text they quote
NO_MENTIONS = discord.AllowedMentions.none()
def utf16_len(text):
return len(text.encode("utf-16-le")) // 2
def cut_utf16(text, units):
"""The longest start of text that fits in `units` UTF-16 code units, never splitting a character."""
n = 0
for i, ch in enumerate(text):
n += 2 if ord(ch) > 0xFFFF else 1
if n > units:
return text[:i]
return text
def truncation_marker(text):
return f"\n… (truncated, {len(text)} characters in all)"
def truncate(text, limit):
"""text cut to at most `limit` UTF-16 units, saying how much was left out."""
if utf16_len(text) <= limit:
return text
marker = truncation_marker(text)
return cut_utf16(text, limit - utf16_len(marker)) + marker
FENCE = "```"
def quoted(text, limit):
"""Message text in a code block, so its own markdown, mentions and line breaks can't pass for
the post's own lines; or a note that it was empty (e.g. attachments only). Backticks get a
zero-width space after them so the text can't close the block. Within `limit` UTF-16 units."""
if not text:
return "*(no text)*"
body = text.replace("`", "`\u200b")
room = limit - utf16_len(FENCE + "\n" + "\n" + FENCE)
if utf16_len(body) <= room:
return f"{FENCE}\n{body}\n{FENCE}"
marker = truncation_marker(text)
body = cut_utf16(body, room - utf16_len(marker))
# Never end the block mid-escape: a lone backtick before the fence would join it
if body.endswith("`"):
body = body[:-1]
return f"{FENCE}\n{body}\n{FENCE}{marker}"
def jump_url(guild_id, channel_id, message_id):
return f"https://discord.com/channels/{guild_id}/{channel_id}/{message_id}"
def where(channel_id, parent_id=None):
"""The channel, or the thread and its parent channel."""
return f"<#{channel_id}> in <#{parent_id}>" if parent_id else f"<#{channel_id}>"
def timestamp(when):
"""Discord renders this in each viewer's own time zone."""
return f"<t:{int(when.timestamp())}:f>"
def header(author_id, channel_id, parent_id):
return f"**Author:** <@{author_id}> (`{author_id}`)\n**Channel:** {where(channel_id, parent_id)}"
def edit_embed(*, guild_id, channel_id, message_id, author_id, before, after, edited_at, parent_id=None):
description = "\n".join([
header(author_id, channel_id, parent_id),
f"**Message:** {jump_url(guild_id, channel_id, message_id)}",
"", "**Before**", quoted(before, EDIT_TEXT_LIMIT),
"", "**After**", quoted(after, EDIT_TEXT_LIMIT),
])
return discord.Embed(
title="Message edited", description=description, colour=EDIT_COLOUR,
timestamp=edited_at or datetime.datetime.now(datetime.UTC),
).set_footer(text=f"{FOOTER_PREFIX}{message_id}")
def delete_embed(row, deleted_at, parent_id=None):
"""One deleted message, from the message_log row log_delete returned."""
lines = [
header(row["author_id"], row["channel_id"], parent_id),
f"**Sent:** {timestamp(row['created_at'])}",
]
names = [a.get("filename") or "(unnamed)" for a in row["attachments"] or []]
if names:
names = discord.utils.escape_markdown(", ".join(names))
lines.append(f"**Attachments:** {truncate(names, ATTACHMENTS_LIMIT)}")
lines += ["", quoted(row["content"], DELETE_TEXT_LIMIT)]
return discord.Embed(
title="Message deleted", description="\n".join(lines), colour=DELETE_COLOUR, timestamp=deleted_at,
).set_footer(text=f"{FOOTER_PREFIX}{row['message_id']}")
def unlogged_embed(count, channel_id, deleted_at, parent_id=None):
"""Bulk-deleted messages neither the log nor discord.py's cache has."""
return discord.Embed(
title=f"{count} more message{'s' if count != 1 else ''} deleted",
description=f"**Channel:** {where(channel_id, parent_id)}\nTheir text isn't known: they're not in the "
"message log (sent over 30 days ago, while PauseBot was down, or by PauseBot itself).",
colour=DELETE_COLOUR, timestamp=deleted_at,
)
def embed_size(embed):
"""An embed's length as Discord counts it toward the 6000 limit, in UTF-16 units."""
return sum(utf16_len(t or "") for t in (embed.title, embed.description, embed.footer.text))
def chunk(items):
"""Groups (embed, sent_at) items into what one message can carry each. A message only holds
items whose quoted messages were sent on the same UTC day, as the daily prune deletes a post
when its oldest quoted message passes retention: that way no post goes more than a day early."""
groups, current, size = [], [], 0
for item in items:
n = embed_size(item[0])
if current and (len(current) == MESSAGE_EMBEDS or size + n > MESSAGE_EMBED_CHARS
or item[1].date() != current[0][1].date()):
groups.append(current)
current, size = [], 0
current.append(item)
size += n
if current:
groups.append(current)
return groups