Skip to content

Commit db13e24

Browse files
authored
- Normalize channel names before comparing against prefixes (#244)
1 parent d690d14 commit db13e24

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

modules/discord/discord_utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import discord
44

55
import modules.logs as logging
6-
from modules.utils import quote
6+
from modules.utils import quote, strip_phantom_space
77

88

99
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,
118118
channel_type: discord.ChannelType,
119119
category: discord.CategoryChannel = None) -> \
120120
Union[discord.VoiceChannel, discord.TextChannel, None]:
121+
starting_channel_name_normalized = normalize_channel_name(channel_name=starting_channel_name)
121122
channels = await get_all_discord_channels(client=client, guild_id=guild_id, channel_type=channel_type)
122123
for channel in channels:
123-
name = channel.name.strip() # Phantom spaces are the worst
124+
name_normalized = normalize_channel_name(channel_name=channel.name)
124125

125-
if name.startswith(starting_channel_name):
126+
if name_normalized.startswith(starting_channel_name_normalized):
126127
if category and channel.category != category:
127128
continue
128129
return channel
@@ -234,3 +235,16 @@ def is_valid_reaction(reaction_emoji: discord.PartialEmoji,
234235
if valid_user_ids and reaction_user_id not in valid_user_ids:
235236
return False
236237
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

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
discord.py==2.3.*
1+
discord.py==2.5.*
22
asyncio~=3.4
33
tautulli==4.6.*,>=4.6.0.2142
44
confuse==2.0.1

0 commit comments

Comments
 (0)