diff --git a/core/thread.py b/core/thread.py index 530e063e54..8eccb04c02 100644 --- a/core/thread.py +++ b/core/thread.py @@ -1115,16 +1115,16 @@ async def find( try: await thread.wait_until_ready() except asyncio.CancelledError: - logger.warning("Thread for %s cancelled, abort creating", recipient) + logger.warning("Thread for %s cancelled.", recipient) return thread else: - if not thread.channel or not self.bot.get_channel(thread.channel.id): + if not thread.cancelled and ( + not thread.channel or not self.bot.get_channel(thread.channel.id) + ): logger.warning( "Found existing thread for %s but the channel is invalid.", recipient_id ) - self.bot.loop.create_task( - thread.close(closer=self.bot.user, silent=True, delete_channel=False) - ) + await thread.close(closer=self.bot.user, silent=True, delete_channel=False) thread = None else: channel = discord.utils.get( @@ -1186,7 +1186,7 @@ async def create( try: await thread.wait_until_ready() except asyncio.CancelledError: - logger.warning("Thread for %s cancelled, abort creating", recipient) + logger.warning("Thread for %s cancelled, abort creating.", recipient) return thread else: if thread.channel and self.bot.get_channel(thread.channel.id): @@ -1195,9 +1195,7 @@ async def create( logger.warning( "Found an existing thread for %s, closing previous thread.", recipient ) - self.bot.loop.create_task( - thread.close(closer=self.bot.user, silent=True, delete_channel=False) - ) + await thread.close(closer=self.bot.user, silent=True, delete_channel=False) thread = Thread(self, recipient) @@ -1231,9 +1229,11 @@ async def create( ) accept_emoji = self.bot.config["confirm_thread_creation_accept"] deny_emoji = self.bot.config["confirm_thread_creation_deny"] - await confirm.add_reaction(accept_emoji) - await asyncio.sleep(0.2) - await confirm.add_reaction(deny_emoji) + emojis = [accept_emoji, deny_emoji] + for emoji in emojis: + await confirm.add_reaction(emoji) + await asyncio.sleep(0.2) + try: r, _ = await self.bot.wait_for( "reaction_add", @@ -1245,29 +1245,31 @@ async def create( ) except asyncio.TimeoutError: thread.cancelled = True - - await confirm.remove_reaction(accept_emoji, self.bot.user) - await asyncio.sleep(0.2) - await confirm.remove_reaction(deny_emoji, self.bot.user) - await destination.send( - embed=discord.Embed( - title="Cancelled", description="Timed out", color=self.bot.error_color + self.bot.loop.create_task( + destination.send( + embed=discord.Embed( + title="Cancelled", description="Timed out", color=self.bot.error_color + ) ) ) - del self.cache[recipient.id] - return thread else: if str(r.emoji) == deny_emoji: thread.cancelled = True + self.bot.loop.create_task( + destination.send( + embed=discord.Embed(title="Cancelled", color=self.bot.error_color) + ) + ) - await confirm.remove_reaction(accept_emoji, self.bot.user) + async def remove_reactions(): + for emoji in emojis: + await confirm.remove_reaction(emoji, self.bot.user) await asyncio.sleep(0.2) - await confirm.remove_reaction(deny_emoji, self.bot.user) - await destination.send( - embed=discord.Embed(title="Cancelled", color=self.bot.error_color) - ) - del self.cache[recipient.id] - return thread + + self.bot.loop.create_task(remove_reactions()) + if thread.cancelled: + del self.cache[recipient.id] + return thread self.bot.loop.create_task( thread.setup(creator=creator, category=category, initial_message=message)