Skip to content

Fix multi-guild nickname channel names. #3368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading