diff options
| author | Rapptz <[email protected]> | 2021-03-30 20:29:03 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-03-30 20:29:03 -0400 |
| commit | 8a24a9301033c64c2c8fa2d940bb99a58b22ede5 (patch) | |
| tree | ec6af6cbb9428d62c0359416f394f86cf521ad0a /examples | |
| parent | Fix background_task example to actually work (diff) | |
| download | discord.py-8a24a9301033c64c2c8fa2d940bb99a58b22ede5.tar.xz discord.py-8a24a9301033c64c2c8fa2d940bb99a58b22ede5.zip | |
Bring back older background task example under a new name
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/background_task_asyncio.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/background_task_asyncio.py b/examples/background_task_asyncio.py new file mode 100644 index 00000000..a72862fb --- /dev/null +++ b/examples/background_task_asyncio.py @@ -0,0 +1,28 @@ +import discord +import asyncio + +class MyClient(discord.Client): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # 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(self): + print('Logged in as') + print(self.user.name) + print(self.user.id) + print('------') + + 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') |