Added auto role add #54
This commit is contained in:
		| @@ -1,10 +1,13 @@ | ||||
| from cpl_core.configuration import ConfigurationABC | ||||
| from cpl_core.dependency_injection import ServiceCollectionABC | ||||
| from cpl_core.environment import ApplicationEnvironmentABC | ||||
| from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum | ||||
| from cpl_discord.service.discord_collection_abc import DiscordCollectionABC | ||||
|  | ||||
| from bot_core.abc.module_abc import ModuleABC | ||||
| from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum | ||||
| from modules.autorole.events.auto_role_on_raw_reaction_add import AutoRoleOnRawReactionAddEvent | ||||
| from modules.autorole.helper.reaction_handler import ReactionHandler | ||||
|  | ||||
|  | ||||
| class AutoRoleModule(ModuleABC): | ||||
| @@ -16,6 +19,7 @@ class AutoRoleModule(ModuleABC): | ||||
|         pass | ||||
|  | ||||
|     def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): | ||||
|         services.add_transient(ReactionHandler) | ||||
|         # commands | ||||
|         # events | ||||
|         pass | ||||
|         self._dc.add_event(DiscordEventTypesEnum.on_raw_reaction_add.value, AutoRoleOnRawReactionAddEvent) | ||||
|   | ||||
							
								
								
									
										0
									
								
								src/modules/autorole/events/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/modules/autorole/events/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										34
									
								
								src/modules/autorole/events/auto_role_on_raw_reaction_add.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/modules/autorole/events/auto_role_on_raw_reaction_add.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.events.on_raw_reaction_add_abc import OnRawReactionAddABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from discord import RawReactionActionEvent | ||||
|  | ||||
| from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
| from modules.autorole.helper.reaction_handler import ReactionHandler | ||||
|  | ||||
|  | ||||
| class AutoRoleOnRawReactionAddEvent(OnRawReactionAddABC): | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             servers: ServerRepositoryABC, | ||||
|             auto_roles: AutoRoleRepositoryABC, | ||||
|             reaction_handler: ReactionHandler | ||||
|     ): | ||||
|         OnRawReactionAddABC.__init__(self) | ||||
|  | ||||
|         self._logger = logger | ||||
|         self._bot = bot | ||||
|         self._servers = servers | ||||
|         self._auto_roles = auto_roles | ||||
|         self._reaction_handler = reaction_handler | ||||
|  | ||||
|     async def on_raw_reaction_add(self, payload: RawReactionActionEvent): | ||||
|         self._logger.debug(__name__, f'Module {type(self)} started') | ||||
|  | ||||
|         await self._reaction_handler.handle(payload, 'add') | ||||
|  | ||||
|         self._logger.debug(__name__, f'Module {type(self)} stopped') | ||||
							
								
								
									
										0
									
								
								src/modules/autorole/helper/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/modules/autorole/helper/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										52
									
								
								src/modules/autorole/helper/reaction_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/modules/autorole/helper/reaction_handler.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| import discord | ||||
| from cpl_core.logging import LoggerABC | ||||
| from cpl_discord.service import DiscordBotServiceABC | ||||
| from cpl_query.extension import List | ||||
| from discord import RawReactionActionEvent | ||||
|  | ||||
| from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC | ||||
| from bot_data.abc.server_repository_abc import ServerRepositoryABC | ||||
| from bot_data.model.auto_role import AutoRole | ||||
| from bot_data.model.auto_role_rule import AutoRoleRule | ||||
|  | ||||
|  | ||||
| class ReactionHandler: | ||||
|  | ||||
|     def __init__( | ||||
|             self, | ||||
|             logger: LoggerABC, | ||||
|             bot: DiscordBotServiceABC, | ||||
|             servers: ServerRepositoryABC, | ||||
|             auto_roles: AutoRoleRepositoryABC | ||||
|     ): | ||||
|         self._logger = logger | ||||
|         self._bot = bot | ||||
|         self._servers = servers | ||||
|         self._auto_roles = auto_roles | ||||
|  | ||||
|         self._message_ids = self._auto_roles.get_auto_roles().select(lambda x: x.discord_message_id) | ||||
|         self._roles = self._auto_roles.get_auto_roles() | ||||
|  | ||||
|     async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: | ||||
|         if payload.message_id not in self._message_ids: | ||||
|             return | ||||
|  | ||||
|         guild = self._bot.get_guild(payload.guild_id) | ||||
|         user = await guild.fetch_member(payload.user_id) | ||||
|         if user.bot: | ||||
|             return | ||||
|  | ||||
|         emoji = payload.emoji.name | ||||
|         auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id).first_or_default() | ||||
|         if auto_role is None: | ||||
|             return | ||||
|  | ||||
|         rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id) | ||||
|  | ||||
|         for rule in rules: | ||||
|             if emoji != rule.emoji_name: | ||||
|                 continue | ||||
|  | ||||
|             role = guild.get_role(rule.role_id) | ||||
|             self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') | ||||
|             await user.add_roles(role) | ||||
		Reference in New Issue
	
	Block a user