Added logic to remove short role name for all members by deleting
Some checks failed
Some checks failed
This commit is contained in:
parent
4553490266
commit
be3b0029ab
@ -1 +1 @@
|
||||
Subproject commit be5b15f227e7b2b12cc73c0edd7f6fa95dbdd52f
|
||||
Subproject commit eeebd13f80c6ceecc922ede5771e55212a884019
|
@ -7,6 +7,7 @@ from bot_data.model.short_role_name import ShortRoleName
|
||||
from bot_data.model.user_role_enum import UserRoleEnum
|
||||
from bot_graphql.abc.query_abc import QueryABC
|
||||
from bot_core.service.permission_service import PermissionService
|
||||
from modules.short_role_name.service.short_role_name_service import ShortRoleNameService
|
||||
|
||||
|
||||
class ShortRoleNameMutation(QueryABC):
|
||||
@ -17,6 +18,7 @@ class ShortRoleNameMutation(QueryABC):
|
||||
bot: DiscordBotServiceABC,
|
||||
db: DatabaseContextABC,
|
||||
permissions: PermissionService,
|
||||
short_role_name_service: ShortRoleNameService,
|
||||
):
|
||||
QueryABC.__init__(self, "ShortRoleNameMutation")
|
||||
|
||||
@ -25,6 +27,7 @@ class ShortRoleNameMutation(QueryABC):
|
||||
self._bot = bot
|
||||
self._db = db
|
||||
self._permissions = permissions
|
||||
self._short_role_name_service = short_role_name_service
|
||||
|
||||
self.set_field("createShortRoleName", self.resolve_create_short_role_name)
|
||||
self.set_field("updateShortRoleName", self.resolve_update_short_role_name)
|
||||
@ -79,6 +82,7 @@ class ShortRoleNameMutation(QueryABC):
|
||||
short_role_name = self._short_role_names.get_short_role_name_by_id(id)
|
||||
self._can_user_mutate_data(short_role_name.server, UserRoleEnum.admin)
|
||||
|
||||
self._bot.loop.create_task(self._short_role_name_service.remove_short_role_from_members(short_role_name))
|
||||
self._short_role_names.delete_short_role_name(short_role_name)
|
||||
self._db.save_changes()
|
||||
|
||||
|
@ -30,6 +30,41 @@ class ShortRoleNameService:
|
||||
self._server = server
|
||||
self._short_role_names = short_role_names
|
||||
|
||||
def _remove_short_role_name_from_name(self, short_role_name: ShortRoleName, name: str) -> str:
|
||||
return (
|
||||
name.replace(f" [{short_role_name.short_name}]", "")
|
||||
.replace(f"[{short_role_name.short_name}] ", "")
|
||||
.replace(f"[{short_role_name.short_name}]", "")
|
||||
)
|
||||
|
||||
async def remove_short_role_from_members(self, short_role_name: ShortRoleName):
|
||||
guild = self._bot.get_guild(short_role_name.server.discord_id)
|
||||
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}")
|
||||
for member in guild.members:
|
||||
try:
|
||||
member_roles = List(discord.Role, member.roles)
|
||||
highest_role: Optional[discord.Role] = (
|
||||
member_roles.last_or_default() if settings.short_role_name_only_set_highest_role else None
|
||||
)
|
||||
member_role_ids = member_roles.select(lambda x: x.id)
|
||||
|
||||
if (
|
||||
short_role_name.role_id not in member_role_ids
|
||||
or highest_role
|
||||
and highest_role.id != short_role_name.role_id
|
||||
):
|
||||
continue
|
||||
|
||||
self._logger.debug(__name__, f"Update member {member.id}")
|
||||
await member.edit(
|
||||
nick=self._remove_short_role_name_from_name(
|
||||
short_role_name, member.nick if member.nick is not None else member.name
|
||||
)
|
||||
)
|
||||
self._logger.debug(__name__, f"Updated member {member.id} {member.name}")
|
||||
except Exception as e:
|
||||
self._logger.error(__name__, f"Renaming member {member.name} failed", e)
|
||||
|
||||
async def check_short_role_names(self, member: discord.Member):
|
||||
self._logger.debug(__name__, f"Started short role name check for {member.id}")
|
||||
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{member.guild.id}")
|
||||
@ -53,9 +88,7 @@ class ShortRoleNameService:
|
||||
for short_role_name in self._short_role_names.get_short_role_names_by_server_id(server.id):
|
||||
short_role_name: ShortRoleName = short_role_name
|
||||
|
||||
new_nick = new_nick.replace(f" [{short_role_name.short_name}]", "")
|
||||
new_nick = new_nick.replace(f"[{short_role_name.short_name}] ", "")
|
||||
new_nick = new_nick.replace(f"[{short_role_name.short_name}]", "")
|
||||
new_nick = self._remove_short_role_name_from_name(short_role_name, new_nick)
|
||||
|
||||
if (
|
||||
short_role_name.role_id not in member_role_ids
|
||||
|
Loading…
Reference in New Issue
Block a user