Skip to content

Commit 942bc24

Browse files
authored
[dpy2] (#9)
* add DPY2 constants and update asset access * remove MemberAdapter typing test * [dpy2] update discord adapters with some missing info fixes * [discordadapters] make guild.icon return empty if no icon set * [utils] fix dpy2 check + maybe_await typing * update version
1 parent f491bfc commit 942bc24

5 files changed

Lines changed: 25 additions & 16 deletions

File tree

TagScriptEngine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .utils import *
99
from .verb import Verb
1010

11-
__version__ = "2.6.5"
11+
__version__ = "2.6.4"
1212

1313

1414
class VersionInfo(namedtuple("VersionInfo", "major minor micro")):

TagScriptEngine/adapter/discordadapters.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from random import choice
22

3-
from discord import TextChannel
3+
import discord
44

55
from ..interface import Adapter
6-
from ..utils import escape_content
6+
from ..utils import DPY2, escape_content
77
from ..verb import Verb
88

99
__all__ = (
@@ -19,10 +19,11 @@ class AttributeAdapter(Adapter):
1919

2020
def __init__(self, base):
2121
self.object = base
22+
created_at = getattr(base, "created_at", None) or discord.utils.snowflake_time(base.id)
2223
self._attributes = {
2324
"id": base.id,
24-
"created_at": base.created_at,
25-
"timestamp": int(base.created_at.timestamp()),
25+
"created_at": created_at,
26+
"timestamp": int(created_at.timestamp()),
2627
"name": getattr(base, "name", str(base)),
2728
}
2829
self._methods = {}
@@ -107,18 +108,19 @@ class MemberAdapter(AttributeAdapter):
107108
"""
108109

109110
def update_attributes(self):
111+
avatar_url = self.object.display_avatar.url if DPY2 else self.object.avatar_url
110112
joined_at = getattr(self.object, "joined_at", self.object.created_at)
111113
additional_attributes = {
112114
"color": self.object.color,
113115
"colour": self.object.color,
114116
"nick": self.object.display_name,
115-
"avatar": (self.object.avatar_url, False),
117+
"avatar": (avatar_url, False),
116118
"discriminator": self.object.discriminator,
117119
"joined_at": joined_at,
118120
"joinstamp": int(joined_at.timestamp()),
119121
"mention": self.object.mention,
120122
"bot": self.object.bot,
121-
"top_role": getattr(self.object, "top_role", None),
123+
"top_role": getattr(self.object, "top_role", ""),
122124
}
123125
if roleids := getattr(self.object, "_roles", None):
124126
additional_attributes["roleids"] = " ".join(str(r) for r in roleids)
@@ -156,11 +158,11 @@ class ChannelAdapter(AttributeAdapter):
156158
"""
157159

158160
def update_attributes(self):
159-
if isinstance(self.object, TextChannel):
161+
if isinstance(self.object, discord.TextChannel):
160162
additional_attributes = {
161163
"nsfw": self.object.nsfw,
162164
"mention": self.object.mention,
163-
"topic": self.object.topic or None,
165+
"topic": self.object.topic or "",
164166
}
165167
self._attributes.update(additional_attributes)
166168

@@ -213,8 +215,9 @@ def update_attributes(self):
213215
else:
214216
humans += 1
215217
member_count = guild.member_count
218+
icon_url = getattr(guild.icon, "url", "") if DPY2 else guild.icon_url
216219
additional_attributes = {
217-
"icon": (guild.icon_url, False),
220+
"icon": (icon_url, False),
218221
"member_count": member_count,
219222
"members": member_count,
220223
"bots": bots,

TagScriptEngine/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import re
22
from inspect import isawaitable
3-
from typing import Any, Awaitable, Callable, Union
3+
from typing import Any, Awaitable, Callable, T, TypeVar, Union
44

5-
__all__ = ("escape_content", "maybe_await")
5+
import discord
6+
7+
__all__ = ("escape_content", "maybe_await", "DPY2")
8+
9+
T = TypeVar("T")
10+
11+
DPY2 = discord.version_info >= (2, 0, 0, "alpha", 0)
612

713
pattern = re.compile(r"(?<!\\)([{():|}])")
814

915

1016
def _sub_match(match: re.Match) -> str:
11-
return "\\" + match.group(1)
17+
return "\\" + match[1]
1218

1319

1420
def escape_content(string: str) -> str:
@@ -25,7 +31,7 @@ def escape_content(string: str) -> str:
2531
return pattern.sub(_sub_match, string)
2632

2733

28-
async def maybe_await(func: Union[Callable[..., Any], Awaitable[Any]], *args, **kwargs) -> Any:
34+
async def maybe_await(func: Callable[..., Union[T, Awaitable[T]]], *args: Any, **kwargs: Any) -> T:
2935
"""
3036
Await the given function if it is awaitable or call it synchronously.
3137

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
autoflake
22
black
3-
discord.py==1.7.3
3+
discord.py==2.2.3
44
isort
55
pyparsing

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ keywords = tagscript,
2121
[options]
2222
packages = find_namespace:
2323
install_requires =
24-
discord.py==1.7.3
24+
discord.py==2.2.3
2525
pyparsing
2626
python_requires = >=3.8
2727

0 commit comments

Comments
 (0)