|
3 | 3 | import discord |
4 | 4 |
|
5 | 5 | import modules.logs as logging |
6 | | -from modules.utils import quote |
| 6 | +from modules.utils import quote, strip_phantom_space |
7 | 7 |
|
8 | 8 |
|
9 | 9 | async def get_guild(client: discord.Client, guild_id: int) -> discord.Guild: |
@@ -118,11 +118,12 @@ async def get_or_create_discord_channel_by_starting_name(client: discord.Client, |
118 | 118 | channel_type: discord.ChannelType, |
119 | 119 | category: discord.CategoryChannel = None) -> \ |
120 | 120 | Union[discord.VoiceChannel, discord.TextChannel, None]: |
| 121 | + starting_channel_name_normalized = normalize_channel_name(channel_name=starting_channel_name) |
121 | 122 | channels = await get_all_discord_channels(client=client, guild_id=guild_id, channel_type=channel_type) |
122 | 123 | for channel in channels: |
123 | | - name = channel.name.strip() # Phantom spaces are the worst |
| 124 | + name_normalized = normalize_channel_name(channel_name=channel.name) |
124 | 125 |
|
125 | | - if name.startswith(starting_channel_name): |
| 126 | + if name_normalized.startswith(starting_channel_name_normalized): |
126 | 127 | if category and channel.category != category: |
127 | 128 | continue |
128 | 129 | return channel |
@@ -234,3 +235,16 @@ def is_valid_reaction(reaction_emoji: discord.PartialEmoji, |
234 | 235 | if valid_user_ids and reaction_user_id not in valid_user_ids: |
235 | 236 | return False |
236 | 237 | return True |
| 238 | + |
| 239 | + |
| 240 | +def normalize_channel_name(channel_name: str) -> str: |
| 241 | + """ |
| 242 | + Normalize a Discord channel name. |
| 243 | + :param channel_name: The original channel name |
| 244 | + :return: Normalized channel name |
| 245 | + """ |
| 246 | + channel_name = channel_name.strip() # Remove leading and trailing spaces |
| 247 | + channel_name = strip_phantom_space(string=channel_name) # Remove phantom spaces |
| 248 | + channel_name = channel_name.replace(" ", "") # Remove any weird space characters in the name |
| 249 | + channel_name = channel_name.lower() |
| 250 | + return channel_name |
0 commit comments