Skip to content

Commit

Permalink
Add canrun command
Browse files Browse the repository at this point in the history
  • Loading branch information
raidensakura committed Apr 17, 2024
1 parent 2c830bf commit 0dd9c0b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ however, insignificant breaking changes do not guarantee a major version bump, s

- Modmail dependencies can now be installed using [PDM](https://pdm-project.org/latest/).
- Added `IS_RAIDEN_FORK` as environmental variable to indicate user is on this version of Modmail.
- Added `?perms canrun` command to check if a user is able to run a specific command.

### Fixed

Expand Down
33 changes: 33 additions & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import traceback
from contextlib import redirect_stdout
from copy import copy
from difflib import get_close_matches
from io import BytesIO, StringIO
from itertools import takewhile, zip_longest
Expand Down Expand Up @@ -1263,6 +1264,38 @@ def _parse_level(name):
}
return transform.get(name, PermissionLevel.INVALID)

@permissions.command(name="canrun")
@checks.has_permissions(PermissionLevel.OWNER)
async def canrun(self, ctx: commands.Context, user: discord.Member, *, command: str):
"""
Check if a user is allowed to run a given command.
This will take the current context into account, such as the
server and text channel.
"""
# Thanks Red Bot
# https://github.com/Cog-Creators/Red-DiscordBot/blob/00e41d38f9c28d459d5d45ad70d65f5e175df0df/redbot/cogs/permissions/permissions.py#L235
fake_message = copy(ctx.message)
fake_message.author = user
fake_message.content = "{}{}".format(ctx.prefix, command)

com = ctx.bot.get_command(command)
if com is None:
out = "No such command"
else:
fake_context = await ctx.bot.get_context(fake_message)
try:
can = await com.can_run(fake_context)
except commands.CommandError:
can = False

out = (
("✅ That user can run the specified command.")
if can
else ("⛔ That user cannot run the specified command.")
)
await ctx.send(out)

@permissions.command(name="override")
@checks.has_permissions(PermissionLevel.OWNER)
async def permissions_override(self, ctx, command_name: str.lower, *, level_name: str):
Expand Down

0 comments on commit 0dd9c0b

Please sign in to comment.