Skip to content

Commit 4a2753c

Browse files
committed
♻️ move oauth_url and Undefined class to public.py; update imports
1 parent 09a11a9 commit 4a2753c

File tree

3 files changed

+74
-67
lines changed

3 files changed

+74
-67
lines changed

discord/utils/__init__.py

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
)
5959

6060
from ..errors import HTTPException
61-
from .public import basic_autocomplete, generate_snowflake, utcnow, snowflake_time
61+
from .public import basic_autocomplete, generate_snowflake, utcnow, snowflake_time, oauth_url, Undefined, MISSING
6262

6363
try:
6464
import msgspec
@@ -88,19 +88,11 @@
8888
"format_dt",
8989
"generate_snowflake",
9090
"basic_autocomplete",
91+
"Undefined",
92+
"MISSING",
9193
)
9294

9395

94-
class Undefined(Enum):
95-
MISSING = auto()
96-
97-
def __bool__(self) -> Literal[False]:
98-
return False
99-
100-
101-
MISSING: Literal[Undefined.MISSING] = Undefined.MISSING
102-
103-
10496
class _cached_property:
10597
def __init__(self, function):
10698
self.function = function
@@ -236,58 +228,6 @@ def decorator(overridden: T) -> T:
236228
return decorator
237229

238230

239-
def oauth_url(
240-
client_id: int | str,
241-
*,
242-
permissions: Permissions | Undefined = MISSING,
243-
guild: Snowflake | Undefined = MISSING,
244-
redirect_uri: str | Undefined = MISSING,
245-
scopes: Iterable[str] | Undefined = MISSING,
246-
disable_guild_select: bool = False,
247-
) -> str:
248-
"""A helper function that returns the OAuth2 URL for inviting the bot
249-
into guilds.
250-
251-
Parameters
252-
----------
253-
client_id: Union[:class:`int`, :class:`str`]
254-
The client ID for your bot.
255-
permissions: :class:`~discord.Permissions`
256-
The permissions you're requesting. If not given then you won't be requesting any
257-
permissions.
258-
guild: :class:`~discord.abc.Snowflake`
259-
The guild to pre-select in the authorization screen, if available.
260-
redirect_uri: :class:`str`
261-
An optional valid redirect URI.
262-
scopes: Iterable[:class:`str`]
263-
An optional valid list of scopes. Defaults to ``('bot',)``.
264-
265-
.. versionadded:: 1.7
266-
disable_guild_select: :class:`bool`
267-
Whether to disallow the user from changing the guild dropdown.
268-
269-
.. versionadded:: 2.0
270-
271-
Returns
272-
-------
273-
:class:`str`
274-
The OAuth2 URL for inviting the bot into guilds.
275-
"""
276-
url = f"https://discord.com/oauth2/authorize?client_id={client_id}"
277-
url += f"&scope={'+'.join(scopes or ('bot',))}"
278-
if permissions is not MISSING:
279-
url += f"&permissions={permissions.value}"
280-
if guild is not MISSING:
281-
url += f"&guild_id={guild.id}"
282-
if redirect_uri is not MISSING:
283-
from urllib.parse import urlencode
284-
285-
url += f"&response_type=code&{urlencode({'redirect_uri': redirect_uri})}"
286-
if disable_guild_select:
287-
url += "&disable_guild_select=true"
288-
return url
289-
290-
291231
def find(predicate: Callable[[T], Any], seq: Iterable[T]) -> T | None:
292232
"""A helper to return the first element found in the sequence
293233
that meets the predicate. For example: ::

discord/utils/private.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import unicodedata
77
import warnings
88
from base64 import b64encode
9-
from typing import TYPE_CHECKING, Any, overload, Callable
9+
from typing import TYPE_CHECKING, Any, overload, Callable, TypeVar, ParamSpec
1010

11-
from . import P, T
1211
from ..errors import InvalidArgument
1312

1413
if TYPE_CHECKING:
@@ -208,6 +207,10 @@ def warn_deprecated(
208207
warnings.simplefilter("default", DeprecationWarning) # reset filter
209208

210209

210+
P = ParamSpec("P")
211+
T = TypeVar("T")
212+
213+
211214
def deprecated(
212215
instead: str | None = None,
213216
since: str | None = None,

discord/utils/public.py

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@
22

33
import asyncio
44
import datetime
5+
from enum import Enum, auto
56
import itertools
67
from collections.abc import Awaitable, Callable, Iterable
7-
from typing import TYPE_CHECKING, Any, Literal
8+
from typing import TYPE_CHECKING, Any, Literal, Iterable
89

9-
from . import DISCORD_EPOCH
1010

1111
if TYPE_CHECKING:
12+
from .abc import Snowflake
1213
from ..commands.context import AutocompleteContext
1314
from ..commands.options import OptionChoice
15+
from ..permissions import Permissions
16+
17+
18+
class Undefined(Enum):
19+
MISSING = auto()
20+
21+
def __bool__(self) -> Literal[False]:
22+
return False
23+
24+
25+
MISSING: Literal[Undefined.MISSING] = Undefined.MISSING
1426

1527
DISCORD_EPOCH = 1420070400000
1628

@@ -195,3 +207,55 @@ def snowflake_time(id: int) -> datetime.datetime:
195207
"""
196208
timestamp = ((id >> 22) + DISCORD_EPOCH) / 1000
197209
return datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc)
210+
211+
212+
def oauth_url(
213+
client_id: int | str,
214+
*,
215+
permissions: Permissions | Undefined = MISSING,
216+
guild: Snowflake | Undefined = MISSING,
217+
redirect_uri: str | Undefined = MISSING,
218+
scopes: Iterable[str] | Undefined = MISSING,
219+
disable_guild_select: bool = False,
220+
) -> str:
221+
"""A helper function that returns the OAuth2 URL for inviting the bot
222+
into guilds.
223+
224+
Parameters
225+
----------
226+
client_id: Union[:class:`int`, :class:`str`]
227+
The client ID for your bot.
228+
permissions: :class:`~discord.Permissions`
229+
The permissions you're requesting. If not given then you won't be requesting any
230+
permissions.
231+
guild: :class:`~discord.abc.Snowflake`
232+
The guild to pre-select in the authorization screen, if available.
233+
redirect_uri: :class:`str`
234+
An optional valid redirect URI.
235+
scopes: Iterable[:class:`str`]
236+
An optional valid list of scopes. Defaults to ``('bot',)``.
237+
238+
.. versionadded:: 1.7
239+
disable_guild_select: :class:`bool`
240+
Whether to disallow the user from changing the guild dropdown.
241+
242+
.. versionadded:: 2.0
243+
244+
Returns
245+
-------
246+
:class:`str`
247+
The OAuth2 URL for inviting the bot into guilds.
248+
"""
249+
url = f"https://discord.com/oauth2/authorize?client_id={client_id}"
250+
url += f"&scope={'+'.join(scopes or ('bot',))}"
251+
if permissions is not MISSING:
252+
url += f"&permissions={permissions.value}"
253+
if guild is not MISSING:
254+
url += f"&guild_id={guild.id}"
255+
if redirect_uri is not MISSING:
256+
from urllib.parse import urlencode
257+
258+
url += f"&response_type=code&{urlencode({'redirect_uri': redirect_uri})}"
259+
if disable_guild_select:
260+
url += "&disable_guild_select=true"
261+
return url

0 commit comments

Comments
 (0)