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
class LoopCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
# whatever else you want to do
@tasks.loop(seconds=1)
async def test_loop(self, filename):
# do your file thingy here
@commands.command(name="start_loop"):
async def start_loop(self,*, filename: str):
# check that the file exists
self.test_loop.start(filename)
@commands.command(name="stop_loop"):
async def stop_loop(self):
self.test_loop()
def setup(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"