This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.py
81 lines (63 loc) · 2.21 KB
/
message.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
71
72
73
74
75
76
77
78
79
80
81
from typing import Union
class MessageSource:
def __init__(self, platform: str, server: Union[int, str], channel: Union[int, str]):
self.__platform = platform
self.__server = server
self.__channel = channel
@property
def platform(self):
return self.__platform
@property
def server(self):
return self.__server
@property
def channel(self):
return self.__channel
class Attachment:
def __init__(self, fp: Union[bytes, str], filename: str = None, spoiler: bool = False):
if isinstance(fp, str):
with open(fp, "rb") as f:
fp = f.read()
self.fp = fp
self.filename = filename
self.spoiler = spoiler
class MessageFeatures:
def __init__(self, embeds: Union[list, object] = None, attachments: Union[list, object] = None,
reply: Union[int, str] = None):
if not isinstance(embeds, list):
embeds = [embeds]
if not isinstance(attachments, list):
attachments = [attachments]
self.embeds = embeds or []
self.attachments = attachments or []
self.reply = reply
class Message:
def __init__(self, message_id: Union[int, str, None], children: dict, source: Union[MessageSource, dict]):
if isinstance(source, dict):
source = MessageSource(**source)
self.__id = message_id
self.__children = children
self.__source = source
self.__orphan = message_id is None
@property
def id(self):
return self.__id
@property
def children(self):
return self.__children
@property
def source(self):
return self.__source
@property
def orphan(self):
return self.__orphan
async def fetch_id(self, server_id: Union[int, str], platform: str = None):
if server_id == self.__source.server:
return self.__id
# platform isn't really needed, only for minor speedups
if platform:
return self.__children[platform][server_id]
for platform in self.__children:
if server_id in self.__children[platform]:
return self.__children[platform][server_id]
return None