diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ec54e0f8a..45b4138ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ 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/modmail-dev/modmail/issues/319). If you're a plugin developer, note the "BREAKING" section. +# v4.1.3 + +### Fixed +- `use_nickname_channel_name` option would produce errors when the member was not found in the guild leading to the failure of opening modmail threads. ([PR #3368](https://github.com/modmail-dev/Modmail/pull/3368)) + # v4.1.2 ### Fixed diff --git a/bot.py b/bot.py index 3f13ef7ced..bf6b97bd3a 100644 --- a/bot.py +++ b/bot.py @@ -1752,31 +1752,37 @@ def format_channel_name(self, author, exclude_channel=None, force_null=False): guild = self.modmail_guild if force_null: - name = new_name = "null" + return ensure_unique_channel_name("null", guild, exclude_channel) else: if self.config["use_random_channel_name"]: to_hash = self.token.split(".")[-1] + str(author.id) digest = hashlib.md5(to_hash.encode("utf8"), usedforsecurity=False) - name = new_name = digest.hexdigest()[-8:] + name = digest.hexdigest()[-8:] elif self.config["use_user_id_channel_name"]: - name = new_name = str(author.id) + name = str(author.id) elif self.config["use_timestamp_channel_name"]: - name = new_name = author.created_at.isoformat(sep="-", timespec="minutes") + name = author.created_at.isoformat(sep="-", timespec="minutes") else: if self.config["use_nickname_channel_name"]: author_member = self.guild.get_member(author.id) - name = author_member.display_name.lower() + if author_member is None: + name = author.display_name.lower() + else: + name = author_member.display_name.lower() else: name = author.name.lower() - if force_null: - name = "null" + sanitized_name = self.sanitize_name(name) + + if author.discriminator != "0": + sanitized_name += f"-{author.discriminator}" + + return ensure_unique_channel_name(sanitized_name, guild, exclude_channel) - name = "".join(l for l in name if l not in string.punctuation and l.isprintable()) or "null" - if author.discriminator != "0": - name += f"-{author.discriminator}" - new_name = name + def sanitize_name(self, name: str) -> str: + return "".join(l for l in name if l not in string.punctuation and l.isprintable()) or "null" + def ensure_unique_channel_name(self, name, guild, exclude_channel) -> str: counter = 1 existed = set(c.name for c in guild.text_channels if c != exclude_channel) while new_name in existed: