Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: let unload_extension() pop regex pattern listeners #1683

Merged

Conversation

mifuyutsuki
Copy link
Contributor

Pull Request Type

  • Feature addition
  • Bugfix
  • Documentation update
  • Code refactor
  • Tests improvement
  • CI/CD pipeline enhancement
  • Other: [Replace with a description]

Description

This PR fixes an error while calling Client.unload_extension() due to the extension containing a callback (component or modal) that uses a regex pattern (re.Pattern) listener.

Changes

  • Edited Extension.drop() to allow for dropping re.Pattern listeners, which are stored in a different dict inside Client

Related Issues

interactions.py Discord server help thread: https://discord.com/channels/789032594456576001/1240745583636119562

Test Scenarios

The command /test-unload should run without errors with the extension's command /test-a unloaded. You may need to reload your Discord client (hotkey ^R) to refresh the list of commands.

test.py (main)

from interactions import Client, Intents, slash_command, SlashContext
bot = Client(intents=Intents.DEFAULT)

@slash_command(name="test-unload", description="Unload test extension")
async def unload_cmd(ctx: SlashContext):
  bot.unload_extension("test_ext")
  await ctx.send("Extension is unloaded")

bot.load_extension("test_ext")
bot.start("token")

test_ext.py (extension)

The command in this extension has two different component callbacks: a str one and a re.Pattern one.

from interactions import (
  Extension,
  slash_command,
  SlashContext,
  Button,
  ButtonStyle,
  component_callback,
  ComponentContext,
)
import re

regex_callback = re.compile(r"button-[0-9]+")

class TestExtension(Extension):
  @slash_command(name="test-a", description="Test command")
  async def test_cmd_a(self, ctx: SlashContext):
    btn_1 = Button(label="1", style=ButtonStyle.BLURPLE, custom_id="button-0001")
    btn_2 = Button(label="2", style=ButtonStyle.BLURPLE, custom_id="button-0002")
    btn_a = Button(label="A", style=ButtonStyle.BLURPLE, custom_id="button-a")
    await ctx.send(components=[btn_1, btn_2, btn_a])

  @component_callback(regex_callback)
  async def re_btn(self, ctx: ComponentContext):
    await ctx.send(f"Clicked button with id `{ctx.custom_id}`")

  @component_callback("button-a")
  async def str_btn(self, ctx: ComponentContext):
    await ctx.send(f"Clicked button with id `button-a`")

Python Compatibility

  • I've ensured my code works on Python 3.10.x
  • I've ensured my code works on Python 3.11.x

Checklist

  • I've run the pre-commit code linter over all edited files
  • I've tested my changes on supported Python versions
  • I've added tests for my code, if applicable
  • I've updated / added documentation, where applicable

@silasary silasary merged commit cfe3199 into interactions-py:unstable Jun 7, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants