summaryrefslogtreecommitdiff
path: root/bot.py
diff options
context:
space:
mode:
Diffstat (limited to 'bot.py')
-rw-r--r--bot.py43
1 files changed, 36 insertions, 7 deletions
diff --git a/bot.py b/bot.py
index 75f8aea..6d8ad9a 100644
--- a/bot.py
+++ b/bot.py
@@ -1,5 +1,6 @@
from os import getenv
+import asyncio
from commands import answer, await_n, bot_commands
import config
@@ -13,6 +14,24 @@ class Cupido(commands.Bot):
self.meta_channel = None
self.lobby_channel = None
self.pair_channels = []
+ self.task = None
+
+ async def await_coroutine(self, co):
+ if self.task is not None:
+ return 'already running'
+ self.task = asyncio.create_task(co)
+ try:
+ result = await self.task
+ except asyncio.CancelledError:
+ self.task = None
+ return 'cancelled'
+ finally:
+ self.task = None
+ return result
+
+ async def cancel_coroutine(self):
+ if self.task is not None:
+ self.task.cancel()
async def on_ready(self):
print(f'the bot {config.NAME} is logged in as "{self.user}"')
@@ -42,16 +61,24 @@ class Cupido(commands.Bot):
category=meta_channel
))
+ def get_pair_channels_no_cache(self, ctx, meta_channel):
+ return sorted((channel for channel in ctx.guild.voice_channels
+ if channel.category == meta_channel
+ and channel.name.isdigit()),
+ key=lambda c: c.name)
+
def get_pair_channels(self, ctx, meta_channel):
- return (self.pair_channels
- or sorted((channel for channel in ctx.guild.voice_channels
- if channel.category == meta_channel
- and channel.name.isdigit()),
- key=lambda c: c.name,
- ))
+ return self.pair_channels or self.get_pair_channels_no_cache(ctx, meta_channel)
+
+ async def try_delete_channel(self, channel):
+ try:
+ await channel.delete()
+ return True
+ except discord.errors.NotFound:
+ return False
async def destroy_pair_channels(self, ctx, meta_channel):
- await await_n(channel.delete() for channel in self.get_pair_channels(ctx, meta_channel))
+ await await_n(map(self.try_delete_channel, self.get_pair_channels_no_cache(ctx, meta_channel)))
self.pair_channels = []
async def create_pair_channels(self, ctx, meta_channel, n):
@@ -86,6 +113,8 @@ if __name__ == '__main__':
while True:
try:
main()
+ except KeyboardInterrupt:
+ break
except Exception as e:
print(f'[DEBUG] encountered exception: {type(e)}: {e}')
if e.args == ('Event loop is closed',):