Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Analysis of the problem
Problem Analysis
Based on the error traceback and codebase investigation, here's what's happening:
The Error
This error occurs during the bot shutdown process when red.close() is called. The issue is that the Red class (which inherits from discord.ext.commands.AutoShardedBot) is trying to access an internal attribute __queue that doesn't exist.
Root Cause
The problem stems from the mixin initialization order in the Red class:
Looking at the initialization chain:
Red.init (line 104-241 in bot.py) does extensive setup work BEFORE calling super().init() at line 241
RPCMixin.init (line 144-146 in _rpc.py) calls super().init(**kwargs)
commands.GroupMixin doesn't define init, so it passes through
Finally reaches AutoShardedBot.init which sets up internal attributes like __queue
The issue is that during the --dry-run execution:
The bot starts initialization
At line 366 in main.py, when --dry-run is detected, it calls sys.exit(ExitCodes.SHUTDOWN)
This triggers the shutdown handler at line 539
The shutdown handler calls await red.close() at line 441
But red.close() → super().close() (line 2278 in bot.py) tries to access AutoShardedClient's internal __queue attribute
This attribute was never properly initialized because the initialization was interrupted
Why This Happens Specifically with --dry-run
The --dry-run flag causes an early exit at line 365-366 in main.py:
This happens before await red.start(token) is called (line 369), which means:
The bot's connection lifecycle never begins
Discord.py's internal sharding infrastructure (including __queue) is never fully initialized
But the shutdown handler still tries to call close() which expects these internals to exist
try, catch might not be the best solution but I can't think of a better one. This only happens when it is not fully initialized and thus sharding is not possible.
closes #6572
the original issue is also addressed.