Added logic to set default role #360

This commit is contained in:
Sven Heidemann 2023-09-24 17:05:35 +02:00
parent b4be6b4ac2
commit 20747001b0
4 changed files with 39 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from datetime import datetime from datetime import datetime
from typing import Callable from typing import Callable, Union
import discord import discord
from cpl_query.extension import List from cpl_query.extension import List
@ -67,3 +67,7 @@ class ClientUtilsABC(ABC):
self, discord_channel_id: int, discord_message_id: int, rule: AutoRoleRule, guild: discord.Guild self, discord_channel_id: int, discord_message_id: int, rule: AutoRoleRule, guild: discord.Guild
): ):
pass pass
@abstractmethod
async def check_default_role(self, member: Union[discord.User, discord.Member]):
pass

View File

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Callable from typing import Callable, Union
import discord import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
@ -218,3 +218,20 @@ class ClientUtilsService(ClientUtilsABC):
f"Cannot add reaction {rule.emoji_name} to message: {discord_message_id}", f"Cannot add reaction {rule.emoji_name} to message: {discord_message_id}",
e, e,
) )
async def check_default_role(self, member: Union[discord.User, discord.Member]):
try:
server = self._servers.get_server_by_discord_id(member.guild.id)
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}")
if settings.default_role_id is None:
return
default_role = member.guild.get_role(settings.default_role_id)
if default_role in member.roles:
return
await member.add_roles(default_role)
except Exception as e:
self._logger.error(__name__, f"Cannot check for default role for member {member.id}", e)

View File

@ -6,6 +6,7 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe
from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC
@ -44,6 +45,7 @@ class DataIntegrityService:
user_joins_vc: UserJoinedVoiceChannelRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC,
user_joined_gs: UserJoinedGameServerRepositoryABC, user_joined_gs: UserJoinedGameServerRepositoryABC,
achievement_service: AchievementService, achievement_service: AchievementService,
client_utils: ClientUtilsABC,
dtp: DateTimeOffsetPipe, dtp: DateTimeOffsetPipe,
): ):
self._config = config self._config = config
@ -60,6 +62,7 @@ class DataIntegrityService:
self._user_joins_vc = user_joins_vc self._user_joins_vc = user_joins_vc
self._user_joined_gs = user_joined_gs self._user_joined_gs = user_joined_gs
self._achievements = achievement_service self._achievements = achievement_service
self._client_utils = client_utils
self._dtp = dtp self._dtp = dtp
self._is_for_shutdown = False self._is_for_shutdown = False
@ -382,10 +385,16 @@ class DataIntegrityService:
await self._achievements.validate_achievements_for_user(user) await self._achievements.validate_achievements_for_user(user)
async def _check_default_role(self):
for guild in self._bot.guilds:
for member in guild.members:
await self._client_utils.check_default_role(member)
async def check_data_integrity(self, is_for_shutdown=False): async def check_data_integrity(self, is_for_shutdown=False):
if is_for_shutdown != self._is_for_shutdown: if is_for_shutdown != self._is_for_shutdown:
self._is_for_shutdown = is_for_shutdown self._is_for_shutdown = is_for_shutdown
await self._check_default_role()
self._check_known_users() self._check_known_users()
self._check_servers() self._check_servers()
self._check_clients() self._check_clients()

View File

@ -6,8 +6,10 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_discord.events import OnMemberJoinABC from cpl_discord.events import OnMemberJoinABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe from cpl_translation import TranslatePipe
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.helper.event_checks import EventChecks from bot_core.helper.event_checks import EventChecks
from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC
@ -28,10 +30,12 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC):
messenger: MessageServiceABC, messenger: MessageServiceABC,
permissions: PermissionServiceABC, permissions: PermissionServiceABC,
db: DatabaseContextABC, db: DatabaseContextABC,
bot: DiscordBotServiceABC,
known_users: KnownUserRepositoryABC, known_users: KnownUserRepositoryABC,
users: UserRepositoryABC, users: UserRepositoryABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
user_joins: UserJoinedServerRepositoryABC, user_joins: UserJoinedServerRepositoryABC,
client_utils: ClientUtilsABC,
translate: TranslatePipe, translate: TranslatePipe,
): ):
OnMemberJoinABC.__init__(self) OnMemberJoinABC.__init__(self)
@ -40,10 +44,12 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC):
self._messenger = messenger self._messenger = messenger
self._permission_service = permissions self._permission_service = permissions
self._db = db self._db = db
self._bot = bot
self._known_users = known_users self._known_users = known_users
self._users = users self._users = users
self._servers = servers self._servers = servers
self._user_joins = user_joins self._user_joins = user_joins
self._client_utils = client_utils
self._t = translate self._t = translate
def _check_for_known_user(self, member: Union[discord.User, discord.Member]): def _check_for_known_user(self, member: Union[discord.User, discord.Member]):
@ -98,5 +104,6 @@ class BaseOnMemberJoinEvent(OnMemberJoinABC):
@EventChecks.check_is_ready() @EventChecks.check_is_ready()
async def on_member_join(self, member: discord.Member): async def on_member_join(self, member: discord.Member):
self._logger.debug(__name__, f"Module {type(self)} started") self._logger.debug(__name__, f"Module {type(self)} started")
await self._client_utils.check_default_role(member)
self._check_for_known_user(member) self._check_for_known_user(member)
await self._add_if_not_exists_user_async(member) await self._add_if_not_exists_user_async(member)