-
-
Notifications
You must be signed in to change notification settings - Fork 482
feat: ✨ Allow usage of functools.partial
as slash or ext command callback
#2724
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
feat: ✨ Allow usage of functools.partial
as slash or ext command callback
#2724
Conversation
partial
as slash or ext command callback
Here is a usage example: from typing import final
from dotenv import load_dotenv
import os
import discord
from discord.ext import bridge, commands
from functools import partial
import logging
logging.basicConfig(level=logging.INFO)
load_dotenv() # pyright: ignore [reportUnusedCallResult]
intents = discord.Intents.default()
intents.message_content = True
bot = bridge.Bot(command_prefix='!', intents=intents) # pyright: ignore [reportAny]
async def anime_command(self, ctx: bridge.BridgeExtContext | bridge.BridgeApplicationContext, action: str, member: discord.Member | None= None):
await ctx.respond(f"You used the {action} command on {member.mention if member else None}!") # pyright: ignore [reportUnknownMemberType, reportUnusedCallResult]
ACTIONS = [
"baka",
"bite",
]
@final
class AnimeSetupCog(commands.Cog):
def __init__(self, bot):
self.bot: bridge.Bot = bot
super().__init__()
@bridge.bridge_group(name="action") # pyright: ignore [reportUnknownMemberType]
async def action_group(self, ctx: bridge.BridgeApplicationContext):
pass
for action in ACTIONS:
func = partial(anime_command, action=action)
cmd = action_group.command(name=action)(func) # pyright: ignore [reportUnknownMemberType]
locals()[action] = cmd
bot.add_cog(AnimeSetupCog(bot))
if __name__ == "__main__":
bot.run(os.getenv("TOKEN_2")) |
cc96d2a
to
ff902cd
Compare
ff902cd
to
6c62503
Compare
partial
as slash or ext command callbackfunctools.partial
as slash or ext command callback
I'm not a big fan of this. We kept allowing new types for slash command options and it became a mess. While freedom is good, development-wise I think it's better to stick to a very specific command scheme, next major version will hopefully clean this up. |
Tbh I agree with doru |
Fair enough, makes sense. For anyone reading this, if you still for some reason want to do this without this pr, you should use a constrructor instead of async def anime_command(self: "AnimeSetupCog", ctx: bridge.BridgeContext, action: str, member: discord.Member | None= None):
await ctx.respond(f"You used the {action} command on {member.mention if member else None}!") # pyright: ignore [reportUnknownMemberType, reportUnusedCallResult]
ACTIONS = [
"baka",
"bite",
]
def create_action_func(action_name: str) -> Callable[["AnimeSetupCog", bridge.BridgeContext, discord.Member | None], Coroutine[None, None, None]]:
async def func(self, ctx: bridge.BridgeContext, member: discord.Member | None = None) -> None: # pyright: ignore [reportMissingParameterType, reportUnknownParameterType]
await anime_command(self, ctx, action=action_name, member=member) # pyright: ignore [reportUnknownArgumentType]
return func # pyright: ignore [reportUnknownVariableType]
@final
class AnimeSetupCog(commands.Cog):
def __init__(self, bot: bridge.Bot) -> None:
self.bot: bridge.Bot = bot
super().__init__()
@bridge.bridge_group(name="action") # pyright: ignore [reportUnknownMemberType]
async def action_group(self, ctx: bridge.BridgeApplicationContext):
pass
for action in ACTIONS:
cmd = action_group.command(name=action)(create_action_func(action)) # pyright: ignore [reportUnknownMemberType]
locals()[action] = cmd |
Summary
Allow to use a partial as a slash or ext command. See comments for example usage. I might have missed some places where other checks for partial should be used but didn't find any other based on e my testing. I will add them later if needed.
Information
examples, ...).
Checklist
type: ignore
comments were used, a comment is also left explaining why.