Skip to content
Merged
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
63 changes: 63 additions & 0 deletions techsupport_bot/commands/nicknamefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""This holds a command to manually adjust someones nickname
Uses the same filter as the automatic nickname filter"""

from __future__ import annotations

from typing import TYPE_CHECKING

import discord
from core import auxiliary, cogs
from discord import app_commands
from functions import nickname

if TYPE_CHECKING:
import bot


async def setup(bot: bot.TechSupportBot):
"""Registers the nicknamefixer cog

Args:
bot (bot.TechSupportBot): The bot to register the cog to
"""
await bot.add_cog(NicknameFixer(bot=bot))


class NicknameFixer(cogs.BaseCog):
"""The class that holds the nickname fixer"""

@app_commands.checks.has_permissions(manage_nicknames=True)
@app_commands.command(
name="nicknamefix",
description="Auto adjusts a nickname of the given member",
extras={
"usage": "member",
"module": "nicknamefix",
},
)
async def fixnickname(
self, interaction: discord.Interaction, member: discord.Member
):
"""Manually adjusts someones nickname to comply with the nickname filter
Does not send a DM

Args:
interaction (discord.Interaction): The interaction the command was called at
member (discord.Member): The member to update the nickname for
"""
if member.bot:
embed = auxiliary.prepare_deny_embed("Bots don't get new nicknames")
return
new_nickname = nickname.format_username(member.display_name)
if new_nickname == member.display_name:
embed = auxiliary.prepare_deny_embed(
f"{member.mention} doesn't need a new nickname"
)
await interaction.response.send_message(embed=embed)
return
await member.edit(nick=new_nickname)
embed = auxiliary.prepare_confirm_embed(
f"{member.mention} name changed to {new_nickname}"
)
await interaction.response.send_message(embed=embed)
return
3 changes: 3 additions & 0 deletions techsupport_bot/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Functions are commandless cogs"""

from .nickname import *
75 changes: 38 additions & 37 deletions techsupport_bot/functions/nickname.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,54 @@ async def setup(bot):
await bot.add_cog(AutoNickName(bot=bot))


class AutoNickName(cogs.BaseCog):
"""
The class that holds the listener and functions to auto change peoples nicknames
"""
def format_username(username: str) -> str:
"""Formats a username to be all ascii and easily readable and pingable

Args:
username (str): The original users username

def format_username(self, username: str) -> str:
"""Formats a username to be all ascii and easily readable and pingable
Returns:
str: The new username with all formatting applied
"""

Args:
username (str): The original users username
# Prepare a random string, just in case
random_string = "".join(random.choice(string.ascii_letters) for _ in range(10))

Returns:
str: The new username with all formatting applied
"""
# Step 1 - Force all ascii
username = unidecode(username)

# Prepare a random string, just in case
random_string = "".join(random.choice(string.ascii_letters) for _ in range(10))
# Step 2 - Remove all markdown
markdown_pattern = r"(\*\*|__|\*|_|\~\~|`|#+|-{3,}|\|{3,}|>)"
username = re.sub(markdown_pattern, "", username)

# Step 1 - Force all ascii
username = unidecode(username)
# Step 3 - Strip
username = username.strip()

# Step 2 - Remove all markdown
markdown_pattern = r"(\*\*|__|\*|_|\~\~|`|#+|-{3,}|\|{3,}|>)"
username = re.sub(markdown_pattern, "", username)
# Step 4 - Fix dumb spaces
username = re.sub(r"\s+", " ", username)
username = re.sub(r"(\b\w) ", r"\1", username)

# Step 3 - Strip
username = username.strip()
# Step 5 - Start with letter
match = re.search(r"[A-Za-z]", username)
if match:
username = username[match.start() :]
else:
username = ""

# Step 4 - Fix dumb spaces
username = re.sub(r"\s+", " ", username)
username = re.sub(r"(\b\w) ", r"\1", username)
# Step 6 - Length check
if len(username) < 3 and len(username) > 0:
username = f"{username}-USER-{random_string}"
elif len(username) == 0:
username = f"USER-{random_string}"
username = username[:32]

# Step 5 - Start with letter
match = re.search(r"[A-Za-z]", username)
if match:
username = username[match.start() :]
else:
username = ""
return username

# Step 6 - Length check
if len(username) < 3 and len(username) > 0:
username = f"{username}-USER-{random_string}"
elif len(username) == 0:
username = f"USER-{random_string}"
username = username[:32]

return username
class AutoNickName(cogs.BaseCog):
"""
The class that holds the listener and functions to auto change peoples nicknames
"""

@commands.Cog.listener()
async def on_member_join(self, member: discord.Member) -> None:
Expand All @@ -79,7 +80,7 @@ async def on_member_join(self, member: discord.Member) -> None:
if not config.get("nickname_filter", False):
return

modified_name = self.format_username(member.display_name)
modified_name = format_username(member.display_name)

# If the name didn't change for the user, do nothing
if modified_name == member.display_name:
Expand Down