Skip to content Skip to sidebar Skip to footer

How To Loop A Function Using Discord.py

My goal is to 'toggle' a loop when a function is called inside of a cog. I want the function to take the argument of a filename. The function will print the line it has read from a

Solution 1:

You can use a task, provided by the discord.py API.

from discord.ext import commands, tasks

classLoopCog(commands.Cog):
    def__init__(self, bot):
        self.bot = bot
        # whatever else you want to do    @tasks.loop(seconds=1)asyncdeftest_loop(self, filename):
        # do your file thingy here    @commands.command(name="start_loop"):asyncdefstart_loop(self,*, filename: str):
        # check that the file exists
        self.test_loop.start(filename)
    @commands.command(name="stop_loop"):asyncdefstop_loop(self):
        self.test_loop()

defsetup(bot):
    bot.add_cog(LoopCog(bot))

I didn't test it as I cannot right now, there might be some errors above, but the loop thingy works that way.

Post a Comment for "How To Loop A Function Using Discord.py"