mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-22 08:44:10 +00:00
On that note, fix the background_task example to use `Client.run` instead of manual loop management. That way the task will be cleaned up and cancelled inside `Client.run`.
24 lines
563 B
Python
24 lines
563 B
Python
import discord
|
|
import asyncio
|
|
|
|
client = discord.Client()
|
|
|
|
async def my_background_task():
|
|
await client.wait_until_ready()
|
|
counter = 0
|
|
channel = discord.Object(id='channel_id_here')
|
|
while not client.is_closed:
|
|
counter += 1
|
|
await client.send_message(channel, counter)
|
|
await asyncio.sleep(60) # task runs every 60 seconds
|
|
|
|
@client.event
|
|
async def on_ready():
|
|
print('Logged in as')
|
|
print(client.user.name)
|
|
print(client.user.id)
|
|
print('------')
|
|
|
|
client.loop.create_task(my_background_task())
|
|
client.run('token')
|