Skip to content

Conversation

plun1331
Copy link
Member

Summary

Fixes: #816

Originally #817, uses a fix from #826

I searched around in the issues tab for other problems people found with this and only found #825, if there are others please let me know.

This was tested with:

  • discord.Bot
    • Slash Commands
    • Slash Command Groups
    • Message & user commands
  • ext.commands.Bot
    • Slash Commands
    • Slash Command Groups
    • Message & user application commands
    • Prefixed command groups
    • Prefixed commands

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, typehinting, examples, ...)

Signed-off-by: plun1331 <plun1331@gmail.com>
Signed-off-by: plun1331 <plun1331@gmail.com>
@krittick
Copy link
Contributor

This PR still causes the same error with reloading extensions that I mentioned in the original revert PR (#829):

File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 816, in reload_extension
   self._remove_module_references(lib.__name__)
 File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 614, in _remove_module_references
   if cmd.module is not None and _is_submodule(name, cmd.module):
AttributeError: 'SlashCommand' object has no attribute 'module' 

@plun1331
Copy link
Member Author

This PR still causes the same error with reloading extensions that I mentioned in the original revert PR (#829):

File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 816, in reload_extension
   self._remove_module_references(lib.__name__)
 File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 614, in _remove_module_references
   if cmd.module is not None and _is_submodule(name, cmd.module):
AttributeError: 'SlashCommand' object has no attribute 'module' 

I wasn't experiencing this during testing, I'll look into it more tomorrow.

Do you have a code sample for reproducing?

@krittick
Copy link
Contributor

This PR still causes the same error with reloading extensions that I mentioned in the original revert PR (#829):

File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 816, in reload_extension
   self._remove_module_references(lib.__name__)
 File "/home/ubuntu/cbot/mj3.10/lib/python3.10/site-packages/discord/cog.py", line 614, in _remove_module_references
   if cmd.module is not None and _is_submodule(name, cmd.module):
AttributeError: 'SlashCommand' object has no attribute 'module' 

I wasn't experiencing this during testing, I'll look into it more tomorrow.

Do you have a code sample for reproducing?

Sure:

import discord
from discord.commands import slash_command
from discord.ext import commands


class Temptests(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @slash_command(name="test838")
    async def test838(self, ctx: discord.ApplicationContext):
        """Test for #838"""
        await ctx.respond("Test for #838")


def setup(bot):
    bot.add_cog(Temptests(bot))

Running this command causes the error:

bot.reload_extension("extensions.temptests")  # or wherever the extension is located

@plun1331
Copy link
Member Author

bot.reload_extension("extensions.temptests")

I can't seem to reproduce this, although I am on Python 3.9 instead of 3.10

@krittick
Copy link
Contributor

bot.reload_extension("extensions.temptests")

I can't seem to reproduce this, although I am on Python 3.9 instead of 3.10

I've tried with 3.9 as well and can reproduce the error there too.

@plun1331
Copy link
Member Author

bot.reload_extension("extensions.temptests")

I can't seem to reproduce this, although I am on Python 3.9 instead of 3.10

I've tried with 3.9 as well and can reproduce the error there too.

Do you have a sample of the code in the bot's main file?

@VincentRPS VincentRPS added this to the v2.0 milestone Jan 26, 2022
@VincentRPS VincentRPS added bug Something isn't working Merge normally priority: medium Medium Priority status: awaiting review Awaiting review from a maintainer labels Jan 26, 2022
@Lulalaby Lulalaby added priority: high High Priority and removed priority: medium Medium Priority labels Jan 26, 2022
@Lulalaby
Copy link
Member

Lulalaby commented Jan 26, 2022

⚠️ Raising prio to high

@Lulalaby Lulalaby enabled auto-merge (squash) January 27, 2022 02:37
auto-merge was automatically disabled January 27, 2022 04:33

Head branch was pushed to by a user without write access

@Lulalaby Lulalaby enabled auto-merge (squash) January 27, 2022 04:48
Lulalaby
Lulalaby previously approved these changes Jan 27, 2022
@Lulalaby Lulalaby requested a review from krittick January 27, 2022 04:49
auto-merge was automatically disabled January 27, 2022 18:04

Head branch was pushed to by a user without write access

@krittick
Copy link
Contributor

krittick commented Jan 27, 2022

I found a way to reliably reproduce the module error on a new bot. It just requires creating two extensions: one containing with a slash command group and a non-grouped slash command, and the other containing a prefix or slash command.

To trigger the error, just run /test838.

Extension 1 (extensions/ext_temptests.py):

import discord
from discord.commands import slash_command, SlashCommandGroup
from discord.ext import commands


class Temptests(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    gtest = SlashCommandGroup("gtest", "gtest")

    @gtest.command(name="dothing", description="gtest")
    async def gtest_dothing(self, ctx: discord.ApplicationContext):
        await ctx.respond("gtest_dothing")

    @slash_command(name="test838", guild_ids=[...])
    async def test838(self, ctx: discord.ApplicationContext):
        """Test for #838"""
        await ctx.respond("Test for #838")
        self.bot.reload_extension("extensions.ext_temptests2")


def setup(bot):
    bot.add_cog(Temptests(bot))

Extension 2 (extensions/ext_temptests2.py):

from discord.ext import commands
from discord.commands import slash_command


class Temptests2(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @slash_command()
    async def testing838(self, ctx):
        await ctx.respond("Test for #838")
        self.bot.reload_extension("extensions.ext_temptests")


def setup(bot):
    bot.add_cog(Temptests2(bot))

Main bot code:

from os import listdir
from os.path import isfile, join
from discord.ext import commands
import discord
intents = discord.Intents.all()


bot = commands.Bot(command_prefix=[">", "/"], intents=intents, owner_ids=[...])
ext_list = [f for f in listdir("extensions/") if isfile(join("extensions/", f))]
extensions = [item.replace(".py", "") for item in ext_list]
for extension in extensions:
    bot.load_extension(f"extensions.{extension}")
bot.run("token here")

@plun1331
Copy link
Member Author

from os import listdir
from os.path import isfile, join
from discord.ext import commands
import discord
intents = discord.Intents.all()


bot = commands.Bot(command_prefix=[">", "/"], intents=intents, owner_ids=[...])
ext_list = [f for f in listdir("extensions/") if isfile(join("extensions/", f))]
extensions = [item.replace(".py", "") for item in ext_list]
for extension in extensions:
    bot.load_extension(f"extensions.{extension}")
bot.run("token here")

The slash command in the second extension appears to have bad indentation

@krittick
Copy link
Contributor

from os import listdir
from os.path import isfile, join
from discord.ext import commands
import discord
intents = discord.Intents.all()


bot = commands.Bot(command_prefix=[">", "/"], intents=intents, owner_ids=[...])
ext_list = [f for f in listdir("extensions/") if isfile(join("extensions/", f))]
extensions = [item.replace(".py", "") for item in ext_list]
for extension in extensions:
    bot.load_extension(f"extensions.{extension}")
bot.run("token here")

The slash command in the second extension appears to have bad indentation

Good catch, updated to clarify that command registration isn't affected. The module AttributeError still occurs though.

@plun1331
Copy link
Member Author

from os import listdir
from os.path import isfile, join
from discord.ext import commands
import discord
intents = discord.Intents.all()


bot = commands.Bot(command_prefix=[">", "/"], intents=intents, owner_ids=[...])
ext_list = [f for f in listdir("extensions/") if isfile(join("extensions/", f))]
extensions = [item.replace(".py", "") for item in ext_list]
for extension in extensions:
    bot.load_extension(f"extensions.{extension}")
bot.run("token here")

The slash command in the second extension appears to have bad indentation

Good catch, updated to clarify that command registration isn't affected. The module AttributeError still occurs though.

I believe I fixed the module AttributeError, however I may have uncovered another issue.

When testing I was printing out bot._application_commands as command removal was acting up, and noticed a command with ID None:

{
    None: <discord.commands.SlashCommand name=test838>, 
    '936472246011183114': <discord.commands.SlashCommandGroup name=gtest>, 
    '936472246011183115': <discord.commands.SlashCommand name=test838>, 
    '936472246011183116': <discord.commands.SlashCommand name=testing838>
}

Unsure why this happened, I don't believe it's related to my PR though. Should most likely check if this can be reproduced on master.
You can see the code I was using in 19f9e45, because I accidentally committed it.

@Lulalaby
Copy link
Member

@plun1331 state

@plun1331
Copy link
Member Author

@plun1331 state

I don't believe there are any more issues

@Lulalaby Lulalaby requested a review from Dorukyum January 28, 2022 19:33
@Lulalaby Lulalaby enabled auto-merge (squash) January 28, 2022 19:33
@Lulalaby Lulalaby merged commit f314302 into Pycord-Development:master Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: high High Priority status: awaiting review Awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors with unloading extensions (discord.Bot)
5 participants