【发布时间】:2021-08-08 01:13:14
【问题描述】:
我正在使用通用框架从评分最高的答案 here 设置每日重复任务。如果我只想在target_channel_id 中指定的通道中打印内容,这运行良好。
但我正在尝试使用 invoke() 从 called_once_a_day 函数中调用另一个函数(带参数),如下所示:
@bot.command()
async def foo(ctx, *, arg):
await ctx.send('this works {}'.format(arg))
问题是,foo 中有一个ctx,但called_once_a_day 中没有 ctx。如果我将ctx 添加为called_once_a_day(ctx) 的参数并包含以下行以调用foo
await ctx.invoke(bot.get_command('foo'), arg='whatever')
我收到以下错误:
TypeError: called_once_a_day() missing 1 required positional argument: 'ctx'
我还尝试从 called_once_a_day 中删除 ctx 参数,而是更改调用 invoke() 的方式
await bot.invoke(bot.get_command('foo'), arg='whatever')
然后我得到这个错误:
TypeError: invoke() got an unexpected keyword argument 'arg'
tldr 我不确定如何在没有ctx 的情况下调用invoke(),但添加ctx 会引发错误。
编辑:
跑步
@tasks.loop(hours = 24)
async def called_once_a_day():
await ctx.invoke(bot.get_command('foo'), arg='whatever')
抛出以下错误:
NameError: name 'ctx' is not defined
【问题讨论】:
-
第一种方法 (
await ctx.invoke(bot.get_command('foo'), arg='whatever')) 对我有效,但arg必须是仅关键字参数。 -
我编辑了我的原始帖子,因为我遇到了这个错误
-
我看了这个,我同意需要定义一个 ctx 或某种通道目标,但在这种情况下没有
message。如果不必调用另一个命令,只需打印到频道,这对我有用:message_channel = bot.get_channel(id)后跟await message_channel.send(...)。但是我不能在没有得到错误AttributeError: 'TextChannel' object has no attribute 'invoke'的情况下做await message_channel.invoke(bot.get_command('foo'), arg='whatever')。我想我需要这样的东西,但我不知道到底是什么。
标签: python discord discord.py