title: How to loop a task in discord.py
body: <p>I am experimenting with making my own little discord bot that can get information from Twitch, but I'm stumped on how to make the bot loop and check for a condition.</p>
  
  <p>I want the bot to loop a section of code every few seconds that checks if the specified twitch channel is live.</p>
  
  <hr>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import discord
  from discord.ext import commands, tasks
  from twitch import TwitchClient
  from pprint import pformat
  
  
  client = TwitchClient(client_id='&lt;twitch token&gt;')
  
  bot = commands.Bot(command_prefix='$')
  
  @bot.event
  async def on_ready():
      print('We have logged in as {0.user}'.format(bot))
  
  @bot.command()
  async def info(ctx, username):
      response = await ctx.send("Querying twitch database...")
      try:
          users = client.users.translate_usernames_to_ids(username)
          for user in users:
              print(user.id)
              userid = user.id
          twitchinfo = client.users.get_by_id(userid)
          status = client.streams.get_stream_by_user(userid)
          if status == None:
              print("Not live")
              livestat = twitchinfo.display_name + "is not live"
          else:
              livestat = twitchinfo.display_name + " is " + status.stream_type
          responsemsg = pformat(twitchinfo) + "\n" + livestat
          await response.edit(content=responsemsg)
      except:
          await response.edit(content="Invalid username")
  
  bot.run("&lt;discord token&gt;")
  </code></pre>
  
  <hr>
  
  <p>I want the bot to run the following code every 10 seconds, for example:</p>
  
  <pre><code>status = client.streams.get_stream_by_user(&lt;channel id&gt;)
  if status == None:
       print("Not live")
       livestat = twitchinfo.display_name + "is not live"
  else:
       livestat = twitchinfo.display_name + " is " + status.stream_type
  </code></pre>
  
  <hr>
  
  <p>I've tried using <code>@tasks.loop(seconds=10)</code> to try and make a custom <code>async def</code> repeat every 10 seconds but it didn't seem to work.</p>
  
  <p>Any ideas?</p>