Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deduplicate owner_ids logic #1109

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions disnake/ext/commands/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ class Bot(BotBase, InteractionBotBase, disnake.Client):
This can be provided as a parameter at creation.

owner_id: Optional[:class:`int`]
The user ID that owns the bot. If this is not set and is then queried via
The ID of the user that owns the bot. If this is not set and is then queried via
:meth:`.is_owner` then it is fetched automatically using
:meth:`~.Bot.application_info`.

This can be provided as a parameter at creation.

owner_ids: Optional[Collection[:class:`int`]]
The user IDs that owns the bot. This is similar to :attr:`owner_id`.
The IDs of the users that own the bot. This is similar to :attr:`owner_id`.
If this is not set and the application is team based, then it is
fetched automatically using :meth:`~.Bot.application_info`.
For performance reasons it is recommended to use a :class:`set`
Expand Down Expand Up @@ -394,14 +394,14 @@ class InteractionBot(InteractionBotBase, disnake.Client):
Attributes
----------
owner_id: Optional[:class:`int`]
The user ID that owns the bot. If this is not set and is then queried via
The ID of the user that owns the bot. If this is not set and is then queried via
:meth:`.is_owner` then it is fetched automatically using
:meth:`~.Bot.application_info`.

This can be provided as a parameter at creation.

owner_ids: Optional[Collection[:class:`int`]]
The user IDs that owns the bot. This is similar to :attr:`owner_id`.
The IDs of the users that own the bot. This is similar to :attr:`owner_id`.
If this is not set and the application is team based, then it is
fetched automatically using :meth:`~.Bot.application_info`.
For performance reasons it is recommended to use a :class:`set`
Expand Down
30 changes: 12 additions & 18 deletions disnake/ext/commands/common_bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ async def _fill_owners(self) -> None:
if self.owner_id or self.owner_ids:
return

await self.wait_until_first_connect() # type: ignore

app = await self.application_info() # type: ignore
app: disnake.AppInfo = await self.application_info() # type: ignore
if app.team:
self.owners = set(app.team.members)
self.owner_ids = {m.id for m in app.team.members}
Expand Down Expand Up @@ -111,19 +109,22 @@ async def close(self) -> None:

@disnake.utils.copy_doc(disnake.Client.login)
async def login(self, token: str) -> None:
self.loop.create_task(self._fill_owners()) # type: ignore
await super().login(token=token) # type: ignore

loop: asyncio.AbstractEventLoop = self.loop # type: ignore
if self.reload:
self.loop.create_task(self._watchdog()) # type: ignore
await super().login(token=token) # type: ignore
loop.create_task(self._watchdog())

# prefetch
loop.create_task(self._fill_owners())

async def is_owner(self, user: Union[disnake.User, disnake.Member]) -> bool:
"""|coro|

Checks if a :class:`~disnake.User` or :class:`~disnake.Member` is the owner of
this bot.

If an :attr:`owner_id` is not set, it is fetched automatically
If :attr:`owner_id` and :attr:`owner_ids` are not set, they are fetched automatically
through the use of :meth:`~.Bot.application_info`.

.. versionchanged:: 1.3
Expand All @@ -140,20 +141,13 @@ async def is_owner(self, user: Union[disnake.User, disnake.Member]) -> bool:
:class:`bool`
Whether the user is the owner.
"""
if not self.owner_id and not self.owner_ids:
await self._fill_owners()

if self.owner_id:
return user.id == self.owner_id
elif self.owner_ids:
return user.id in self.owner_ids
else:
app = await self.application_info() # type: ignore
if app.team:
self.owners = set(app.team.members)
self.owner_ids = ids = {m.id for m in app.team.members}
return user.id in ids
else:
self.owner = app.owner
self.owner_id = owner_id = app.owner.id
return user.id == owner_id
return user.id in self.owner_ids

def add_cog(self, cog: Cog, *, override: bool = False) -> None:
"""Adds a "cog" to the bot.
Expand Down