From d00b562a8f34d4943272cc3e499b1c7c10f57e62 Mon Sep 17 00:00:00 2001 From: Jia Rong Yee <28086837+fourjr@users.noreply.github.com> Date: Thu, 14 Jan 2021 15:26:51 +0800 Subject: [PATCH] Retry with diff name if channel cant be created resolves #2934 --- CHANGELOG.md | 10 ++++++++-- bot.py | 2 +- core/thread.py | 33 +++++++++++++++++++++------------ core/utils.py | 5 ++++- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf57a18a7f..ad59acb814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html); however, insignificant breaking changes do not guarantee a major version bump, see the reasoning [here](https://github.com/kyb3r/modmail/issues/319). If you're a plugin developer, note the "BREAKING" section. +# v3.8.2 + +### Fixed + +- Retry with `null-discrim` if channel could not be created. ([GH #2934](https://github.com/kyb3r/modmail/issues/2934)) +- Fix update notifications. + # v3.8.1 ### Fixed -- Additional image uploads now render properly. ([PR #2933](https://github.com/kyb3r/modmail/pull/2933))) +- Additional image uploads now render properly. ([PR #2933](https://github.com/kyb3r/modmail/pull/2933)) - `confirm_thread_creation` no longer raises unnecessary errors. ([GH #2931](https://github.com/kyb3r/modmail/issues/2931), [PR #2933](https://github.com/kyb3r/modmail/pull/2933)) - Autotriggers no longer sends attachments back. ([GH #2932](https://github.com/kyb3r/modmail/issues/2932)) - # v3.8.0 ### Added diff --git a/bot.py b/bot.py index 0986880004..bd9f7eff1e 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,4 @@ -__version__ = "3.8.1" +__version__ = "3.8.2" import asyncio diff --git a/core/thread.py b/core/thread.py index 9baa0c921c..17eaaa34dc 100644 --- a/core/thread.py +++ b/core/thread.py @@ -125,18 +125,27 @@ async def setup(self, *, creator=None, category=None, initial_message=None): overwrites=overwrites, reason="Creating a thread channel.", ) - except discord.HTTPException as e: # Failed to create due to missing perms. - logger.critical("An error occurred while creating a thread.", exc_info=True) - self.manager.cache.pop(self.id) - - embed = discord.Embed(color=self.bot.error_color) - embed.title = "Error while trying to create a thread." - embed.description = str(e) - embed.add_field(name="Recipient", value=recipient.mention) - - if self.bot.log_channel is not None: - await self.bot.log_channel.send(embed=embed) - return + except discord.HTTPException as e: + # try again but null-discrim (name could be banned) + try: + channel = await self.bot.modmail_guild.create_text_channel( + name=format_channel_name(recipient, self.bot.modmail_guild, force_null=True), + category=category, + overwrites=overwrites, + reason="Creating a thread channel.", + ) + except discord.HTTPException as e: # Failed to create due to missing perms. + logger.critical("An error occurred while creating a thread.", exc_info=True) + self.manager.cache.pop(self.id) + + embed = discord.Embed(color=self.bot.error_color) + embed.title = "Error while trying to create a thread." + embed.description = str(e) + embed.add_field(name="Recipient", value=recipient.mention) + + if self.bot.log_channel is not None: + await self.bot.log_channel.send(embed=embed) + return self._channel = channel diff --git a/core/utils.py b/core/utils.py index 6803b97d1e..b34d253baf 100644 --- a/core/utils.py +++ b/core/utils.py @@ -339,9 +339,12 @@ def escape_code_block(text): return re.sub(r"```", "`\u200b``", text) -def format_channel_name(author, guild, exclude_channel=None): +def format_channel_name(author, guild, exclude_channel=None, force_null=False): """Sanitises a username for use with text channel names""" name = author.name.lower() + if force_null: + name = "null" + name = new_name = ( "".join(l for l in name if l not in string.punctuation and l.isprintable()) or "null" ) + f"-{author.discriminator}"