Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.7k stars 458 forks source link

Extension Unloading does not stop slash_commands from functioning. #2015

Open Vox314 opened 1 year ago

Vox314 commented 1 year ago

Summary

Commands don't get removed from the internal cache on extension unloading.

Reproduction Steps

  1. Make a normal Bot.
  2. Make a Cog loader (like I did shown below as the for filename loop.)
  3. When the bot is running, try the /ping command --> everything works fine.
  4. Now execute the /unload utility command.
  5. Try the /ping command.
  6. It still works and does not respond with "unknown interaction" or any error defined before.

Minimal Reproducible Code

# utility.py cog
import discord

class utility(discord.Cog, name = 'utility', description = "Bot utility commands."):
    def __init__(self, bot):
        self.bot = bot

    @discord.slash_command(name = 'ping', description = f"Displays the bot latency.")
    async def ping(self, ctx): 
        await ctx.respond(f'**Pong!**\n``{round(self.bot.latency * 1000)}ms``')

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

# main.py
import discord, os
from dotenv import load_dotenv

debug_server = ID

load_dotenv()
intents = discord.Intents.default() # These are all the Intents of my actual Bot.
intents.message_content = True # Makes sure the bot can read messages
intents.members = True
bot = discord.Bot(intents = intents, auto_sync_commands = True, debug_guilds = debug_server)
colorama.init(autoreset=True)

@bot.slash_command(name = "unload", description = "Unloads a category.")
async def unload(ctx, category: Option(str, required = True)):
    try:
        bot.unload_extension(f'cogs.{category}')
        print(colorama.Fore.RED + f'Unloaded : {category}')
        await ctx.respond(f':white_check_mark:  **Unloaded {category}!**  :white_check_mark:')
    except:
        em = discord.Embed()
        em.title = ('Invalid category!')
        em.description = (f'This category does not exist or has already been unloaded.')
        em.color = 0xe74c3c
        await ctx.respond(embed = em)

for filename in os.listdir('./cogs'): # Loads all files (*.py)
    if filename.endswith('.py'):
        bot.load_extension(f'cogs.{filename[:-3]}') # Loads the file without ".py" for example: cogs.fun
        print(colorama.Fore.BLUE + f'Loaded : {filename[:-3]}')

if __name__ == '__main__':
    bot.run(os.getenv('token'))

Expected Results

The bot should have responded with "The application did not respond", because it could not find the /ping command in the internal cache.

image

Actual Results

On unloading the Cog which contains the /ping command, it still works fine. "still works fine" as in the /ping command still responds with the normal (not unloaded) response.

Intents

intents = discord.Intents.default(message_content=True, members=True)

System Information

Checklist

Additional Context

I just want to note that this is a known issue already, but it wasn't listed on GitHub as an issue and @JustaSqu1d allowed me to make an issue post here.

image image
Kusarigama90 commented 1 year ago

Has someone started looking at this yet? Or have an solution that can be done temporary, so I can use the unloading

Vox314 commented 1 year ago

Nope, there are a lot of other Issues with this library which have to be fixed before this one too... so it's just waiting I guess. I don't have any solution to this yet, also this can take a very very very long time as I stated before.

Kusarigama90 commented 1 year ago

Nope, there are a lot of other Issues with this library which have to be fixed before this one too... so it's just waiting I guess. I don't have any solution to this yet, also this can take a very very very long time as I stated before.

Alright thanks for the reply, because I've even tried the await sync_commands but apparently it still wont sync like in discord.py. Would rather not convert as it feels more complicated when im new