Skip to content

Commit e7bf2b2

Browse files
typing: use ctx.app_permissions where possible (#752)
1 parent 66f6747 commit e7bf2b2

6 files changed

Lines changed: 11 additions & 19 deletions

File tree

monty/exts/core/error_handler.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def send_error(self, ctx: AnyContext, content: str | None = None, **kwargs
143143
)
144144
if isinstance(ctx, commands.Context):
145145
components.insert_item(0, DeleteButton(ctx.author, initial_message=ctx.message))
146-
app_permissions = ctx.channel.permissions_for(ctx.me) # pyright: ignore[reportArgumentType]
146+
app_permissions = ctx.app_permissions
147147
if app_permissions.manage_messages:
148148
send_error = functools.partial(
149149
ctx.reply,
@@ -223,13 +223,9 @@ async def handle_bot_missing_perms(
223223
) -> None:
224224
"""Handles bot missing permissing by dming the user if they have a permission which may be able to fix this."""
225225
embed = self.error_embed("Permissions Failure", str(error))
226-
if isinstance(ctx, commands.Context):
227-
app_permissions = ctx.channel.permissions_for(ctx.me) # pyright: ignore[reportArgumentType]
228-
else:
229-
app_permissions = ctx.app_permissions
230-
if app_permissions >= disnake.Permissions(send_messages=True, embed_links=True):
226+
if ctx.app_permissions >= disnake.Permissions(send_messages=True, embed_links=True):
231227
await self.send_error(ctx, embeds=[embed])
232-
elif app_permissions >= disnake.Permissions(send_messages=True):
228+
elif ctx.app_permissions >= disnake.Permissions(send_messages=True):
233229
# make a message as similar to the embed, using as few permissions as possible
234230
# this is the only place we send a standard message instead of an embed
235231
# so no helper methods are necessary

monty/exts/core/eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ async def ieval(self, ctx: commands.Context, *, body: str) -> None:
336336
if not response.components and not response.files:
337337
return
338338

339-
if ctx.guild is not None and ctx.channel.permissions_for(ctx.guild.me).manage_messages:
339+
if ctx.app_permissions.manage_messages:
340340
delete_contexts = (ctx.message, None)
341341
else:
342342
delete_contexts = (None,)
@@ -432,7 +432,7 @@ async def repl(self, ctx: commands.Context) -> None:
432432
if not response.components and not response.files:
433433
continue
434434

435-
if msg.guild is not None and msg.channel.permissions_for(msg.guild.me).manage_messages:
435+
if ctx.app_permissions.manage_messages:
436436
delete_contexts = (msg, None)
437437
else:
438438
delete_contexts = (None,)

monty/exts/core/gateway.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ async def gateway(self, ctx: commands.Context) -> None:
5252
DeleteButton(
5353
ctx.author.id,
5454
allow_manage_messages=False,
55-
initial_message=(
56-
ctx.message if ctx.guild and ctx.channel.permissions_for(ctx.guild.me).manage_messages else None
57-
),
55+
initial_message=(ctx.message if ctx.app_permissions.manage_messages else None),
5856
),
5957
]
6058
await ctx.send(components=components, allowed_mentions=disnake.AllowedMentions.none())

monty/exts/python/eval.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ async def continue_eval(self, ctx: commands.Context, response: disnake.Message)
321321
pass
322322

323323
# if we have permissions, delete the user's reaction
324-
app_permissions = ctx.channel.permissions_for(ctx.me) # type: ignore
325-
if app_permissions.manage_messages:
324+
if ctx.app_permissions.manage_messages:
326325
try:
327326
await ctx.message.clear_reaction(REEVAL_EMOJI)
328327
except disnake.Forbidden:

monty/exts/utils/bookmark.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ async def send_embed(
183183
]
184184
kwargs = {}
185185
if isinstance(ctx, commands.Context):
186-
app_permissions = ctx.channel.permissions_for(ctx.me) # type: ignore
187-
if ctx.channel == target_message.channel and app_permissions.read_message_history:
186+
if ctx.channel == target_message.channel and ctx.app_permissions.read_message_history:
188187
kwargs["reference"] = target_message.to_reference(fail_if_not_exists=False)
189188

190189
allowed_mentions = disnake.AllowedMentions.none()
@@ -278,7 +277,7 @@ async def _bookmark(
278277
if isinstance(ctx, disnake.Interaction):
279278
await ctx.send(embed=result, ephemeral=True)
280279
else:
281-
app_permissions = ctx.channel.permissions_for(ctx.me) # type: ignore
280+
app_permissions = ctx.app_permissions
282281
if app_permissions.read_message_history:
283282
components = DeleteButton(ctx.author, initial_message=ctx.message)
284283
await ctx.reply(embed=result, fail_if_not_exists=False, components=components)

uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)