Skip to content

Commit 095d804

Browse files
committed
Format bot.py
1 parent ee7c6ef commit 095d804

File tree

1 file changed

+122
-47
lines changed

1 file changed

+122
-47
lines changed

discord/bot.py

Lines changed: 122 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def add_application_command(self, command: ApplicationCommand) -> None:
100100
command.guild_ids = self.debug_guilds
101101
self._pending_application_commands.append(command)
102102

103-
def remove_application_command(self, command: ApplicationCommand) -> Optional[ApplicationCommand]:
103+
def remove_application_command(
104+
self, command: ApplicationCommand
105+
) -> Optional[ApplicationCommand]:
104106
"""Remove a :class:`.ApplicationCommand` from the internal list
105107
of commands.
106108
@@ -155,7 +157,9 @@ async def register_commands(self) -> None:
155157
global_permissions: List = []
156158

157159
registered_commands = await self.http.get_global_commands(self.user.id)
158-
for command in [cmd for cmd in self.pending_application_commands if cmd.guild_ids is None]:
160+
for command in [
161+
cmd for cmd in self.pending_application_commands if cmd.guild_ids is None
162+
]:
159163
as_dict = command.to_dict()
160164
if len(registered_commands) > 0:
161165
matches = [
@@ -185,16 +189,21 @@ async def register_commands(self) -> None:
185189
update_guild_commands = {}
186190
async for guild in self.fetch_guilds(limit=None):
187191
update_guild_commands[guild.id] = []
188-
for command in [cmd for cmd in self.pending_application_commands if cmd.guild_ids is not None]:
192+
for command in [
193+
cmd
194+
for cmd in self.pending_application_commands
195+
if cmd.guild_ids is not None
196+
]:
189197
as_dict = command.to_dict()
190198
for guild_id in command.guild_ids:
191199
to_update = update_guild_commands[guild_id]
192200
update_guild_commands[guild_id] = to_update + [as_dict]
193201

194202
for guild_id in update_guild_commands:
195203
try:
196-
cmds = await self.http.bulk_upsert_guild_commands(self.user.id, guild_id,
197-
update_guild_commands[guild_id])
204+
cmds = await self.http.bulk_upsert_guild_commands(
205+
self.user.id, guild_id, update_guild_commands[guild_id]
206+
)
198207

199208
# Permissions for this Guild
200209
guild_permissions: List = []
@@ -206,20 +215,39 @@ async def register_commands(self) -> None:
206215
raise
207216
else:
208217
for i in cmds:
209-
cmd = get(self.pending_application_commands, name=i["name"], description=i["description"],
210-
type=i['type'])
218+
cmd = get(
219+
self.pending_application_commands,
220+
name=i["name"],
221+
description=i["description"],
222+
type=i["type"],
223+
)
211224
self.application_commands[i["id"]] = cmd
212225

213226
# Permissions
214-
permissions = [perm.to_dict() for perm in cmd.permissions if perm.guild_id is None or (
215-
perm.guild_id == guild_id and perm.guild_id in cmd.guild_ids)]
216-
guild_permissions.append({"id": i["id"], "permissions": permissions})
227+
permissions = [
228+
perm.to_dict()
229+
for perm in cmd.permissions
230+
if perm.guild_id is None
231+
or (
232+
perm.guild_id == guild_id and perm.guild_id in cmd.guild_ids
233+
)
234+
]
235+
guild_permissions.append(
236+
{"id": i["id"], "permissions": permissions}
237+
)
217238

218239
for global_command in global_permissions:
219-
permissions = [perm.to_dict() for perm in global_command['permissions'] if
220-
perm.guild_id is None or (
221-
perm.guild_id == guild_id and perm.guild_id in cmd.guild_ids)]
222-
guild_permissions.append({"id": global_command["id"], "permissions": permissions})
240+
permissions = [
241+
perm.to_dict()
242+
for perm in global_command["permissions"]
243+
if perm.guild_id is None
244+
or (
245+
perm.guild_id == guild_id and perm.guild_id in cmd.guild_ids
246+
)
247+
]
248+
guild_permissions.append(
249+
{"id": global_command["id"], "permissions": permissions}
250+
)
223251

224252
# Collect & Upsert Permissions for Each Guild
225253
# Command Permissions for this Guild
@@ -231,47 +259,80 @@ async def register_commands(self) -> None:
231259

232260
# Replace Role / Owner Names with IDs
233261
for permission in item["permissions"]:
234-
if isinstance(permission['id'], str):
262+
if isinstance(permission["id"], str):
235263
# Replace Role Names
236-
if permission['type'] == 1 and isinstance(permission['id'], str):
237-
role = get(self.get_guild(guild_id).roles, name=permission['id'])
264+
if permission["type"] == 1 and isinstance(
265+
permission["id"], str
266+
):
267+
role = get(
268+
self.get_guild(guild_id).roles,
269+
name=permission["id"],
270+
)
238271

239272
# If not missing
240273
if not role is None:
241274
new_cmd_perm["permissions"].append(
242-
{"id": role.id, "type": 1, "permission": permission['permission']})
275+
{
276+
"id": role.id,
277+
"type": 1,
278+
"permission": permission["permission"],
279+
}
280+
)
243281
else:
244-
print("No Role ID found in Guild ({guild_id}) for Role ({role})".format(
245-
guild_id=guild_id, role=permission['id']))
282+
print(
283+
"No Role ID found in Guild ({guild_id}) for Role ({role})".format(
284+
guild_id=guild_id, role=permission["id"]
285+
)
286+
)
246287
# Add Owner IDs
247-
elif permission['type'] == 2 and permission['id'] == "owner":
288+
elif (
289+
permission["type"] == 2 and permission["id"] == "owner"
290+
):
248291
app = await self.application_info() # type: ignore
249292
if app.team:
250293
for m in app.team.members:
251294
new_cmd_perm["permissions"].append(
252-
{"id": m.id, "type": 2, "permission": permission['permission']})
295+
{
296+
"id": m.id,
297+
"type": 2,
298+
"permission": permission["permission"],
299+
}
300+
)
253301
else:
254302
new_cmd_perm["permissions"].append(
255-
{"id": app.owner.id, "type": 2, "permission": permission['permission']})
303+
{
304+
"id": app.owner.id,
305+
"type": 2,
306+
"permission": permission["permission"],
307+
}
308+
)
256309
# Add the Rest
257310
else:
258311
new_cmd_perm["permissions"].append(permission)
259312

260313
# Make sure we don't have over 10 overwrites
261-
if len(new_cmd_perm['permissions']) > 10:
314+
if len(new_cmd_perm["permissions"]) > 10:
262315
print(
263316
"Command '{name}' has more than 10 permission overrides in guild ({guild_id}).\nwill only use the first 10 permission overrides.".format(
264-
name=self.application_commands[new_cmd_perm['id']].name, guild_id=guild_id))
265-
new_cmd_perm['permissions'] = new_cmd_perm['permissions'][:10]
317+
name=self.application_commands[new_cmd_perm["id"]].name,
318+
guild_id=guild_id,
319+
)
320+
)
321+
new_cmd_perm["permissions"] = new_cmd_perm["permissions"][:10]
266322

267323
# Append to guild_cmd_perms
268324
guild_cmd_perms.append(new_cmd_perm)
269325

270326
# Upsert
271327
try:
272-
await self.http.bulk_upsert_command_permissions(self.user.id, guild_id, guild_cmd_perms)
328+
await self.http.bulk_upsert_command_permissions(
329+
self.user.id, guild_id, guild_cmd_perms
330+
)
273331
except Forbidden:
274-
print(f"Failed to add command permissions to guild {guild_id}", file=sys.stderr)
332+
print(
333+
f"Failed to add command permissions to guild {guild_id}",
334+
file=sys.stderr,
335+
)
275336
raise
276337

277338
async def process_application_commands(self, interaction: Interaction) -> None:
@@ -306,16 +367,16 @@ async def process_application_commands(self, interaction: Interaction) -> None:
306367
else:
307368
ctx = await self.get_application_context(interaction)
308369
ctx.command = command
309-
self.dispatch('application_command', ctx)
370+
self.dispatch("application_command", ctx)
310371
try:
311372
if await self.can_run(ctx, call_once=True):
312373
await ctx.command.invoke(ctx)
313374
else:
314-
raise CheckFailure('The global check once functions failed.')
375+
raise CheckFailure("The global check once functions failed.")
315376
except DiscordException as exc:
316377
await ctx.command.dispatch_error(ctx, exc)
317378
else:
318-
self.dispatch('application_command_completion', ctx)
379+
self.dispatch("application_command_completion", ctx)
319380

320381
def slash_command(self, **kwargs):
321382
"""A shortcut decorator that invokes :func:`.ApplicationCommandMixin.command` and adds it to
@@ -400,7 +461,9 @@ def command(self, **kwargs):
400461
"""
401462
return self.application_command(**kwargs)
402463

403-
def command_group(self, name: str, description: str, guild_ids=None) -> SlashCommandGroup:
464+
def command_group(
465+
self, name: str, description: str, guild_ids=None
466+
) -> SlashCommandGroup:
404467
# TODO: Write documentation for this. I'm not familiar enough with what this function does to do it myself.
405468
group = SlashCommandGroup(name, description, guild_ids)
406469
self.add_application_command(group)
@@ -451,24 +514,30 @@ def __init__(self, description=None, *args, **options):
451514
self._check_once = []
452515
self._before_invoke = None
453516
self._after_invoke = None
454-
self.description = inspect.cleandoc(description) if description else ''
455-
self.owner_id = options.get('owner_id')
456-
self.owner_ids = options.get('owner_ids', set())
517+
self.description = inspect.cleandoc(description) if description else ""
518+
self.owner_id = options.get("owner_id")
519+
self.owner_ids = options.get("owner_ids", set())
457520

458-
self.debug_guild = options.pop("debug_guild", None) # TODO: remove or reimplement
521+
self.debug_guild = options.pop(
522+
"debug_guild", None
523+
) # TODO: remove or reimplement
459524
self.debug_guilds = options.pop("debug_guilds", None)
460525

461526
if self.owner_id and self.owner_ids:
462-
raise TypeError('Both owner_id and owner_ids are set.')
527+
raise TypeError("Both owner_id and owner_ids are set.")
463528

464-
if self.owner_ids and not isinstance(self.owner_ids, collections.abc.Collection):
465-
raise TypeError(f'owner_ids must be a collection not {self.owner_ids.__class__!r}')
529+
if self.owner_ids and not isinstance(
530+
self.owner_ids, collections.abc.Collection
531+
):
532+
raise TypeError(
533+
f"owner_ids must be a collection not {self.owner_ids.__class__!r}"
534+
)
466535

467536
if self.debug_guild:
468537
if self.debug_guilds is None:
469538
self.debug_guilds = [self.debug_guild]
470539
else:
471-
raise TypeError('Both debug_guild and debug_guilds are set.')
540+
raise TypeError("Both debug_guild and debug_guilds are set.")
472541

473542
self._checks = []
474543
self._check_once = []
@@ -481,7 +550,9 @@ async def on_connect(self):
481550
async def on_interaction(self, interaction):
482551
await self.process_application_commands(interaction)
483552

484-
async def on_application_command_error(self, context: ApplicationContext, exception: DiscordException) -> None:
553+
async def on_application_command_error(
554+
self, context: ApplicationContext, exception: DiscordException
555+
) -> None:
485556
"""|coro|
486557
487558
The default command error handler provided by the bot.
@@ -504,8 +575,10 @@ async def on_application_command_error(self, context: ApplicationContext, except
504575
# if cog and cog.has_error_handler():
505576
# return
506577

507-
print(f'Ignoring exception in command {context.command}:', file=sys.stderr)
508-
traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr)
578+
print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
579+
traceback.print_exception(
580+
type(exception), exception, exception.__traceback__, file=sys.stderr
581+
)
509582

510583
# global check registration
511584
# TODO: Remove these from commands.Bot
@@ -611,7 +684,9 @@ def whitelist(ctx):
611684
self.add_check(func, call_once=True)
612685
return func
613686

614-
async def can_run(self, ctx: ApplicationContext, *, call_once: bool = False) -> bool:
687+
async def can_run(
688+
self, ctx: ApplicationContext, *, call_once: bool = False
689+
) -> bool:
615690
data = self._check_once if call_once else self._checks
616691

617692
if len(data) == 0:
@@ -620,7 +695,6 @@ async def can_run(self, ctx: ApplicationContext, *, call_once: bool = False) ->
620695
# type-checker doesn't distinguish between functions and methods
621696
return await async_all(f(ctx) for f in data) # type: ignore
622697

623-
624698
def before_invoke(self, coro):
625699
"""A decorator that registers a coroutine as a pre-invoke hook.
626700
A pre-invoke hook is called directly before the command is
@@ -646,7 +720,7 @@ def before_invoke(self, coro):
646720
The coroutine passed is not actually a coroutine.
647721
"""
648722
if not asyncio.iscoroutinefunction(coro):
649-
raise TypeError('The pre-invoke hook must be a coroutine.')
723+
raise TypeError("The pre-invoke hook must be a coroutine.")
650724

651725
self._before_invoke = coro
652726
return coro
@@ -678,11 +752,12 @@ def after_invoke(self, coro):
678752
679753
"""
680754
if not asyncio.iscoroutinefunction(coro):
681-
raise TypeError('The post-invoke hook must be a coroutine.')
755+
raise TypeError("The post-invoke hook must be a coroutine.")
682756

683757
self._after_invoke = coro
684758
return coro
685759

760+
686761
class Bot(BotBase, Client):
687762
"""Represents a discord bot.
688763

0 commit comments

Comments
 (0)