-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
70 lines (57 loc) · 2.05 KB
/
utils.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
from constants import (
ALLOWED_SERVER_IDS,
)
import logging
logger = logging.getLogger(__name__)
from base import Message
from discord import Message as DiscordMessage
from typing import Optional, List
import discord
from constants import MAX_CHARS_PER_REPLY_MSG, INACTIVATE_THREAD_PREFIX
def discord_message_to_message(message: DiscordMessage) -> Optional[Message]:
if (
message.type == discord.MessageType.thread_starter_message
and message.reference.cached_message
and len(message.reference.cached_message.embeds) > 0
and len(message.reference.cached_message.embeds[0].fields) > 0
):
field = message.reference.cached_message.embeds[0].fields[0]
if field.value:
return Message(user=field.name, text=field.value)
else:
if message.content:
return Message(user=message.author.name, text=message.content)
return None
def split_into_shorter_messages(message: str) -> List[str]:
return [
message[i : i + MAX_CHARS_PER_REPLY_MSG]
for i in range(0, len(message), MAX_CHARS_PER_REPLY_MSG)
]
def is_last_message_stale(
interaction_message: DiscordMessage, last_message: DiscordMessage, bot_id: str
) -> bool:
return (
last_message
and last_message.id != interaction_message.id
and last_message.author
and last_message.author.id != bot_id
)
async def close_thread(thread: discord.Thread):
await thread.edit(name=INACTIVATE_THREAD_PREFIX)
await thread.send(
embed=discord.Embed(
description="**Thread closed** - Context limit reached, closing...",
color=discord.Color.blue(),
)
)
await thread.edit(archived=True, locked=True)
def should_block(guild: Optional[discord.Guild]) -> bool:
if guild is None:
# dm's not supported
logger.info(f"DM not supported")
return True
if guild.id and guild.id not in ALLOWED_SERVER_IDS:
# not allowed in this server
logger.info(f"Guild {guild} not allowed")
return True
return False