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

Time:01-21

I have an auto-channel-deleter that uses commands.task.loop() in order to check the time passed since the last message was sent. The loop function has my own custom function that requires Context (this custom function uses await commands.EmojiConverter().convert(), which needs context as one of its arguments)

I don't know if I need to send the code, but I think what I've wrote is good enough hopefully

CodePudding user response:

When starting the loop by invoking start method you can pass it any arguments you would like. For example:

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)

by adding this cog to your bot you can use start command to begin a loop called spammer that will have the context from the command because it was passed as an argument to the start method: self.spamer.start(ctx)

If you have any more questions feel free to ask in comment :)

  •  Tags:  
  • Related