是否可以在循环中使用 Context discord.py

is it possible to have Context in a loop discord.py

我有一个自动频道删除器,它使用 commands.task.loop() 来检查自上次发送消息以来经过的时间。循环函数有我自己的需要上下文的自定义函数(这个自定义函数使用 await commands.EmojiConverter().convert(),它需要上下文作为它的参数之一)

我不知道是否需要发送代码,但我认为我写的已经足够好了希望

通过调用 start 方法启动循环时,您可以向其传递任何您想要的参数。例如:

class Test(commands.Cog):
    @tasks.loop(seconds=2)
    async def spammer(self, ctx: commands.Context):
        await ctx.send(".")

    @commands.command()
    async def start(self, ctx):
        self.spammer.start(ctx)

通过将此 cog 添加到您的机器人,您可以使用 start 命令开始一个名为 spammer 的循环,该循环将具有来自命令的上下文,因为它作为参数传递给 start方法:self.spamer.start(ctx)

如果您还有其他问题,请随时在评论中提问:)