Skip to content

Commit bc45118

Browse files
committed
move format_channel_name to Bot, resolve #2982
1 parent fcc3fad commit bc45118

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
This project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html);
77
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.
88

9-
# v3.10.0-dev6
9+
# v3.10.0-dev7
1010

1111
v3.10 adds group conversations while resolving other bugs and QOL changes. It is potentially breaking to some plugins that adds functionality to threads.
1212

1313
### Breaking
1414

1515
- `Thread.recipient` (`str`) is now `Thread.recipients` (`List[str]`).
16-
- `thread.reply` now returns mod_message, user_message1, user_message2... It is no longer limited at a size 2 tuple.
16+
- `Thread.reply` now returns mod_message, user_message1, user_message2... It is no longer limited at a size 2 tuple.
1717

1818
### Added
1919

@@ -41,6 +41,7 @@ v3.10 adds group conversations while resolving other bugs and QOL changes. It is
4141

4242
- Fix return types, type hints and unresolved references ([PR #3009](https://github.com/kyb3r/modmail/pull/3009))
4343
- Reload thread cache only when it's the first on_ready trigger. ([GH #3037](https://github.com/kyb3r/modmail/issues/3037))
44+
- `format_channel_name` is now extendable to plugins. Modify `Bot.format_channel_name(bot, author, exclude_channel=None, force_null=False):`. ([GH #2982](https://github.com/kyb3r/modmail/issues/2982))
4445

4546
# v3.9.5
4647

bot.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "v3.10.0-dev6"
1+
__version__ = "3.10.0-dev7"
22

33

44
import asyncio
@@ -1645,6 +1645,36 @@ async def before_autoupdate(self):
16451645
logger.warning("Autoupdates disabled.")
16461646
self.autoupdate_loop.cancel()
16471647

1648+
def format_channel_name(self, author, exclude_channel=None, force_null=False):
1649+
"""Sanitises a username for use with text channel names
1650+
1651+
Placed in main bot class to be extendable to plugins"""
1652+
guild = self.modmail_guild
1653+
1654+
if force_null:
1655+
name = new_name = "null"
1656+
else:
1657+
if self.config["use_user_id_channel_name"]:
1658+
name = new_name = str(author.id)
1659+
elif self.config["use_timestamp_channel_name"]:
1660+
name = new_name = author.created_at.isoformat(sep="-", timespec="minutes")
1661+
else:
1662+
name = author.name.lower()
1663+
if force_null:
1664+
name = "null"
1665+
1666+
name = new_name = (
1667+
"".join(l for l in name if l not in string.punctuation and l.isprintable()) or "null"
1668+
) + f"-{author.discriminator}"
1669+
1670+
counter = 1
1671+
existed = set(c.name for c in guild.text_channels if c != exclude_channel)
1672+
while new_name in existed:
1673+
new_name = f"{name}_{counter}" # multiple channels with same name
1674+
counter += 1
1675+
1676+
return new_name
1677+
16481678

16491679
def main():
16501680
try:

cogs/modmail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ async def repair(self, ctx):
18291829
)
18301830
if len(users) == 1:
18311831
user = users.pop()
1832-
name = format_channel_name(self.bot, user, exclude_channel=ctx.channel)
1832+
name = self.bot.format_channel_name(user, exclude_channel=ctx.channel)
18331833
recipient = self.bot.get_user(user.id)
18341834
if user.id in self.bot.threads.cache:
18351835
thread = self.bot.threads.cache[user.id]

core/utils.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"format_description",
3131
"trigger_typing",
3232
"escape_code_block",
33-
"format_channel_name",
3433
"tryint",
3534
"get_top_hoisted_role",
3635
"get_joint_id",
@@ -364,35 +363,6 @@ def escape_code_block(text):
364363
return re.sub(r"```", "`\u200b``", text)
365364

366365

367-
def format_channel_name(bot, author, exclude_channel=None, force_null=False):
368-
"""Sanitises a username for use with text channel names"""
369-
guild = bot.modmail_guild
370-
371-
if force_null:
372-
name = new_name = "null"
373-
else:
374-
if bot.config["use_user_id_channel_name"]:
375-
name = new_name = str(author.id)
376-
elif bot.config["use_timestamp_channel_name"]:
377-
name = new_name = author.created_at.isoformat(sep="-", timespec="minutes")
378-
else:
379-
name = author.name.lower()
380-
if force_null:
381-
name = "null"
382-
383-
name = new_name = (
384-
"".join(l for l in name if l not in string.punctuation and l.isprintable()) or "null"
385-
) + f"-{author.discriminator}"
386-
387-
counter = 1
388-
existed = set(c.name for c in guild.text_channels if c != exclude_channel)
389-
while new_name in existed:
390-
new_name = f"{name}_{counter}" # multiple channels with same name
391-
counter += 1
392-
393-
return new_name
394-
395-
396366
def tryint(x):
397367
try:
398368
return int(x)
@@ -408,7 +378,7 @@ def get_top_hoisted_role(member: discord.Member):
408378

409379

410380
async def create_thread_channel(bot, recipient, category, overwrites, *, name=None, errors_raised=[]):
411-
name = name or format_channel_name(bot, recipient)
381+
name = name or bot.format_channel_name(recipient)
412382
try:
413383
channel = await bot.modmail_guild.create_text_channel(
414384
name=name,
@@ -446,7 +416,7 @@ async def create_thread_channel(bot, recipient, category, overwrites, *, name=No
446416
recipient,
447417
category,
448418
overwrites,
449-
name=format_channel_name(bot, recipient, force_null=True),
419+
name=bot.format_channel_name(recipient, force_null=True),
450420
errors_raised=errors_raised,
451421
)
452422

0 commit comments

Comments
 (0)