Command.can_run leaves ctx.command and ctx.permission_state changed when a parent or the cog refuses #6821
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot#6821
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What Red version are you using?
3.5.24
What were you trying to do?
Check whether the author of a command could run a different command, by calling
other_command.can_run(ctx, check_all_parents=True)on the current invocation's context. Help formatters do this to decide what to list, and so do cogs that gate one command on another.What did you expect to happen?
Expected:
can_runreturns True or False and leavesctxas it was. Its own comment says it only borrowsctx.command"so contexts invoking other commands can be checked with this command as well", and itsfinallyputsctx.commandandctx.permission_stateback.What actually happened?
When the check is refused by a parent (
check_all_parents=True) or by the cog (a top-level command),can_runreturnsFalsebefore reaching thetry/finally.ctx.commandstays set to the probed command, andctx.permission_statekeeps whatever the parent checks set.https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/core/commands/commands.py#L435-L462
The stale
ctx.commandthen changes what the rest of the invocation does.ctx.cogis read from it, so inon_command_errorRed's "the cog has its own error handler" check looks at the wrong cog. A cog that forwards an error withbot.on_command_error(ctx, error, unhandled_by_cog=True)gets that error handled twice, andctx.send_help()sends help for the wrong command.With the cog below, a plain member (not an admin, not the bot owner) runs
[p]needsargwith no argument. They get help for the admin-onlylocked subcommand twice, and never the help forneedsarg:Same result with
[p]helpset verifyexistson and off. The bot owner never sees it, becauseRequires.verifylets owners through before any refusal.How can we reproduce this error?
canrunrepro/__init__.pyin a cog path ([p]addpath <folder>), then[p]load canrunrepro.[p]probe: From the bot owner, the same command showsafter=probe.[p]needsargwith no argument. You get two help messages, both forlocked sub(see above).Anything else?
I'm happy to open a PR (with tests).
I've reproduced this and have a fix with regression tests ready (restores
ctx.commandandctx.permission_stateon every return path inCommand.can_run). I'll open a PR shortly unless someone is already working on it.Well, I had the PR ready as mentioned earlier, I was just waiting for an ORG member to comment on this
Thanks for the heads-up! Since you had yours ready, please go ahead and open it. I'll close mine in favor of yours.
Looks valid.
FYI, you should prefer
Command.can_seefor the help formatter case (it does still call out tocan_run()but also checks theCommand.hidden's value).You can make a PR. Not sure about adding the tests - we don't really have mocks or anything of the sort for anything command related.
Done!
And regarding the tests: I added two lightweight unit tests in
tests/core/test_commands.pythat only mockctx.command,permission_state, andrequires.verify(no full bot or Discord harness)(Though, If you'd prefer not to have mock-based tests for commands in the repo, let me know and I'm completely happy to drop the test commit!)