updated stuff

This commit is contained in:
JDJG Inc. Official 2021-10-03 22:00:17 -04:00
parent 4277f65051
commit 5133a58d6d

View File

@ -84,6 +84,8 @@ __all__ = (
"escape_mentions",
"as_chunks",
"format_dt",
"generate_snowflake",
"quick_snowflake"
)
DISCORD_EPOCH = 1420070400000
@ -248,14 +250,17 @@ def deprecated(instead: Optional[str] = None) -> Callable[[Callable[P, T]], Call
def actual_decorator(func: Callable[P, T]) -> Callable[P, T]:
@functools.wraps(func)
def decorated(*args: P.args, **kwargs: P.kwargs) -> T:
warnings.simplefilter("always", DeprecationWarning) # turn off filter
warnings.simplefilter(
"always", DeprecationWarning) # turn off filter
if instead:
fmt = "{0.__name__} is deprecated, use {1} instead."
else:
fmt = "{0.__name__} is deprecated."
warnings.warn(fmt.format(func, instead), stacklevel=3, category=DeprecationWarning)
warnings.simplefilter("default", DeprecationWarning) # reset filter
warnings.warn(fmt.format(func, instead),
stacklevel=3, category=DeprecationWarning)
warnings.simplefilter(
"default", DeprecationWarning) # reset filter
return func(*args, **kwargs)
return decorated
@ -309,7 +314,8 @@ def oauth_url(
if redirect_uri is not MISSING:
from urllib.parse import urlencode
url += "&response_type=code&" + urlencode({"redirect_uri": redirect_uri})
url += "&response_type=code&" + \
urlencode({"redirect_uri": redirect_uri})
if disable_guild_select:
url += "&disable_guild_select=true"
return url
@ -440,7 +446,8 @@ def get(iterable: Iterable[T], **attrs: Any) -> Optional[T]:
return elem
return None
converted = [(attrget(attr.replace("__", ".")), value) for attr, value in attrs.items()]
converted = [(attrget(attr.replace("__", ".")), value)
for attr, value in attrs.items()]
for elem in iterable:
if _all(pred(elem) == value for pred, value in converted):
@ -501,7 +508,8 @@ def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
if use_clock or not reset_after:
utc = datetime.timezone.utc
now = datetime.datetime.now(utc)
reset = datetime.datetime.fromtimestamp(float(request.headers["X-Ratelimit-Reset"]), utc)
reset = datetime.datetime.fromtimestamp(
float(request.headers["X-Ratelimit-Reset"]), utc)
return (reset - now).total_seconds()
else:
return float(reset_after)
@ -611,7 +619,8 @@ class SnowflakeList(array.array):
...
def __new__(cls, data: Iterable[int], *, is_sorted: bool = False):
return array.array.__new__(cls, "Q", data if is_sorted else sorted(data)) # type: ignore
# type: ignore
return array.array.__new__(cls, "Q", data if is_sorted else sorted(data))
def add(self, element: int) -> None:
i = bisect_left(self, element)
@ -694,7 +703,8 @@ def resolve_template(code: Union[Template, str]) -> str:
return code
_MARKDOWN_ESCAPE_SUBREGEX = "|".join(r"\{0}(?=([\s\S]*((?<!\{0})\{0})))".format(c) for c in ("*", "`", "_", "~", "|"))
_MARKDOWN_ESCAPE_SUBREGEX = "|".join(
r"\{0}(?=([\s\S]*((?<!\{0})\{0})))".format(c) for c in ("*", "`", "_", "~", "|"))
_MARKDOWN_ESCAPE_COMMON = r"^>(?:>>)?\s|\[.+\]\(.+\)"
@ -944,7 +954,8 @@ def evaluate_annotation(
)
if is_literal and not all(isinstance(x, (str, int, bool, type(None))) for x in evaluated_args):
raise TypeError("Literal arguments must be of type str, int, bool, or NoneType.")
raise TypeError(
"Literal arguments must be of type str, int, bool, or NoneType.")
if evaluated_args == args:
return tp
@ -1020,3 +1031,22 @@ def format_dt(dt: datetime.datetime, /, style: Optional[TimestampStyle] = None)
if style is None:
return f"<t:{int(dt.timestamp())}>"
return f"<t:{int(dt.timestamp())}:{style}>"
def generate_snowflake(dt: datetime.datetime) -> int:
"""
-----------
dt: :class:`datetime.datetime`
A datetime object to convert to a snowflake.
If naive, the timezone is assumed to be local time.
Returns :class:`int` as The snowflake representing the time given.
"""
return int(dt.timestamp() * 1000 - DISCORD_EPOCH) << 22 | 0x3fffff
def quick_snowflake() -> int:
""":class:`int` Returns with The snowflake representing a snowflake from the immediate time.
"""
return int(utcnow().timestamp() * 1000 - DISCORD_EPOCH) << 22 | 0x3fffff