small improvments

This commit is contained in:
Klemek
2021-05-18 16:54:18 +02:00
parent 38056f430f
commit a01414dce7
3 changed files with 29 additions and 25 deletions
+5 -12
View File
@@ -3,12 +3,11 @@ import discord
from datetime import datetime
from . import MessageLog
from utils import serialize, FakeMessage
CHUNK_SIZE = 2000
FORMAT = 3
NOT_SERIALIZED = ["channel", "guild", "start_date"]
class ChannelLogs:
def __init__(self, channel: Union[discord.TextChannel, dict], guild: Any):
@@ -82,9 +81,7 @@ class ChannelLogs:
done = 0
async for message in channel.history(
limit=CHUNK_SIZE,
before=discord.MessageReference(
self.first_message_id, self.id, guild_id=self.guild.id
)
before=FakeMessage(self.first_message_id)
if self.first_message_id is not None
else None,
oldest_first=False,
@@ -110,9 +107,7 @@ class ChannelLogs:
tmp_message_id = self.last_message_id
async for message in channel.history(
limit=CHUNK_SIZE,
after=discord.MessageReference(
self.first_message_id, self.id, guild_id=self.guild.id
),
after=FakeMessage(self.first_message_id),
oldest_first=True,
):
last_message_date = message.created_at
@@ -121,7 +116,7 @@ class ChannelLogs:
await m.load(message)
self.messages.insert(0, m)
yield len(self.messages), False
except discord.errors.HTTPException:
except discord.errors.HTTPException as e:
yield -1, True
return # When an exception occurs (like Forbidden)
self.start_date = (
@@ -130,8 +125,6 @@ class ChannelLogs:
yield len(self.messages), True
def dict(self) -> dict:
channel = dict(self.__dict__)
for key in NOT_SERIALIZED:
channel.pop(key, None)
channel = serialize(self, not_serialized=["channel", "guild", "start_date"])
channel["messages"] = [message.dict() for message in self.messages]
return channel
+4 -12
View File
@@ -2,13 +2,10 @@ from typing import Union, Any
import discord
from datetime import datetime
from utils import is_extension
from utils import is_extension, serialize
IMAGE_FORMAT = [".gif", ".gifv", ".png", ".jpg", ".jpeg", ".bmp"]
EMBED_IMAGES = ["image", "gifv"]
NOT_SERIALIZED = ["channel"]
EMBED_IMAGES = ["image", "gifv", "gif"]
class MessageLog:
@@ -81,11 +78,6 @@ class MessageLog:
self.reactions[str(reaction.emoji)] += [user.id]
def dict(self) -> dict:
message = dict(self.__dict__)
for key in NOT_SERIALIZED:
message.pop(key, None)
message["created_at"] = self.created_at.isoformat()
message["edited_at"] = (
self.edited_at.isoformat() if self.edited_at is not None else None
return serialize(
self, not_serialized=["channel"], dates=["created_at", "edited_at"]
)
return message
+20 -1
View File
@@ -30,7 +30,7 @@ def generate_help(
replace_args=[],
):
arg_list = "* " + "\n* ".join(
replace_args + COMMON_HELP_ARGS[len(replace_args) :] + args
args + replace_args + COMMON_HELP_ARGS[len(replace_args) :]
)
return f"""```
%{cmd}: {info}
@@ -84,6 +84,10 @@ def escape_text(text: str) -> str:
return discord.utils.escape_markdown(discord.utils.escape_mentions(text))
class FakeMessage:
def __init__(self, id: int):
self.id = id
# FILE
@@ -132,6 +136,21 @@ def val_sum(d: Dict[Any, int]) -> int:
return sum(d.values())
def serialize(
obj: Any, *, not_serialized: List[str] = [], dates: List[str] = []
) -> Dict:
output = dict(obj.__dict__)
for key in not_serialized:
output.pop(key, None)
for key in dates:
if output[key]:
try:
output[key] = getattr(obj, key).isoformat()
except AttributeError:
pass
return output
# MESSAGE FORMATTING