我有一个带有异步函数的类:
from discord.ext import commands
class CLASSa():
def __init__(self, bot: commands.Bot):
self.bot = bot
self.voice_states = {}
@commands.command(name='cmd', invoke_without_subcommand=True)
async def _cmd(self, ctx: commands.Context):
"""Omit"""
我想在外部_cmd
调用此函数。我当前编写的代码和结构是这样的:
def function(bot, ctx):
"""!!!CALL_cmd!!!"""
obj = CLASSA(bot)
obj.bot.loop.create_task(obj._cmd(obj,ctx))
class CLASSb():
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, ctx):
function(self.bot, ctx)
错误:
Task exception was never retrieved
future: <Task finished name='Task-15' coro=<Command.__call__() done, defined at /opt/virtualenvs/python3/project_name/python3.8/site-packages/discord/ext/commands/core.py:358> exception=TypeError("_cmd() missing 1 required positional argument: 'ctx'")>
Traceback (most recent call last):
File "/opt/virtualenvs/python3/project_name/python3.8/site-packages/discord/ext/commands/core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
TypeError: _cmd() missing 1 required positional argument: 'ctx'
多谢。
我认为@command
装饰器正在更改函数的签名。
您可能希望重构核心功能,并且只调用该裸函数:
from discord.ext import commands
class CLASSA():
def __init__(self, bot: commands.Bot):
self.bot = bot
self.voice_states = {}
async def core_functionality(self, ctx: commands.Context):
"""Omit"""
@commands.command(name='cmd', invoke_without_subcommand=True):
async def _cmd(self, ctx: commands.Context):
return self.core_functionality(ctx)
def function(bot, ctx):
"""!!!CALL_cmd!!!"""
obj.bot.loop.create_task(CLASSA(bot).core_functionality(ctx))