diff options
| author | Rapptz <[email protected]> | 2017-01-03 20:57:41 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-01-03 20:58:11 -0500 |
| commit | f8a5d890fed1e2c7105964dfd1d310d6d7fa22ee (patch) | |
| tree | 73365f2e688746a029f1ae8293aa8eca965ffb11 /examples/background_task.py | |
| parent | Fix Messageable.typing context manager. (diff) | |
| download | discord.py-f8a5d890fed1e2c7105964dfd1d310d6d7fa22ee.tar.xz discord.py-f8a5d890fed1e2c7105964dfd1d310d6d7fa22ee.zip | |
Update examples to match the new rewrite API.
Diffstat (limited to 'examples/background_task.py')
| -rw-r--r-- | examples/background_task.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/examples/background_task.py b/examples/background_task.py index c1e4c7e2..f6d7abcf 100644 --- a/examples/background_task.py +++ b/examples/background_task.py @@ -1,23 +1,28 @@ import discord import asyncio -client = discord.Client() +class MyClient(discord.Client): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) -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 + # create the background task and run it in the background + self.bg_task = self.loop.create_task(self.my_background_task()) -async def on_ready(): - print('Logged in as') - print(client.user.name) - print(client.user.id) - print('------') + async def on_ready(self): + print('Logged in as') + print(self.user.name) + print(self.user.id) + print('------') -client.loop.create_task(my_background_task()) + async def my_background_task(self): + await self.wait_until_ready() + counter = 0 + channel = self.get_channel(1234567) # channel ID goes here + while not self.is_closed: + counter += 1 + await channel.send(counter) + await asyncio.sleep(60) # task runs every 60 seconds + + +client = MyClient() client.run('token') |