history commands

This commit is contained in:
Klemek
2021-04-06 23:01:51 +02:00
parent aa32382e1d
commit e9505fa082
14 changed files with 215 additions and 11 deletions
+6 -5
View File
@@ -1,4 +1,4 @@
from typing import Union, Tuple
from typing import Union, Tuple, Any
import discord
from . import MessageLog
@@ -9,7 +9,8 @@ FORMAT = 3
class ChannelLogs:
def __init__(self, channel: Union[discord.TextChannel, dict]):
def __init__(self, channel: Union[discord.TextChannel, dict], guild: Any):
self.guild = guild
if isinstance(channel, discord.TextChannel):
self.id = channel.id
self.name = channel.name
@@ -27,7 +28,7 @@ class ChannelLogs:
if channel["last_message_id"] is not None
else None
)
self.messages = [MessageLog(message) for message in channel["messages"]]
self.messages = [MessageLog(message, self) for message in channel["messages"]]
def is_format(self):
return self.format == FORMAT
@@ -44,7 +45,7 @@ class ChannelLogs:
oldest_first=True,
):
self.last_message_id = message.id
m = MessageLog(message)
m = MessageLog(message, self)
await m.load(message)
self.messages.insert(0, m)
yield len(self.messages), False
@@ -64,7 +65,7 @@ class ChannelLogs:
):
done += 1
last_message_id = message.id
m = MessageLog(message)
m = MessageLog(message, self)
await m.load(message)
self.messages += [m]
yield len(self.messages), False
+5 -2
View File
@@ -49,6 +49,7 @@ class Worker:
class GuildLogs:
def __init__(self, guild: discord.Guild):
self.id = guild.id
self.guild = guild
self.log_file = os.path.join(LOG_DIR, f"{guild.id}.logz")
self.channels = {}
@@ -104,7 +105,9 @@ class GuildLogs:
return CANCELLED, 0
await code_message(progress, "Reading saved history (4/4)...")
t0 = datetime.now()
self.channels = {int(id): ChannelLogs(channels[id]) for id in channels}
self.channels = {
int(id): ChannelLogs(channels[id], self) for id in channels
}
# remove invalid format
self.channels = {
id: self.channels[id]
@@ -154,7 +157,7 @@ class GuildLogs:
for channel in target_channels:
if channel.id not in self.channels or fresh:
loading_new += 1
self.channels[channel.id] = ChannelLogs(channel)
self.channels[channel.id] = ChannelLogs(channel, self)
workers += [Worker(self.channels[channel.id], channel)]
warning_msg = "(this might take a while)"
if len(target_channels) > 5 and loading_new > 5:
+3 -2
View File
@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, Any
import discord
from datetime import datetime
@@ -9,7 +9,8 @@ EMBED_IMAGES = ["image", "gifv"]
class MessageLog:
def __init__(self, message: Union[discord.Message, dict]):
def __init__(self, message: Union[discord.Message, dict], channel: Any):
self.channel = channel
if isinstance(message, discord.Message):
self.id = message.id
self.created_at = message.created_at