Added permission check to command

This commit is contained in:
Sven Heidemann 2021-12-31 12:05:10 +01:00
parent ab44aa55bc
commit bed3f07347
5 changed files with 30 additions and 2 deletions

View File

@ -10,6 +10,7 @@
910199452915093593, 910199452915093593,
910199452915093594 910199452915093594
], ],
"PurgeMessage": "Ja mein Herr, ich lösche alle Nachrichten!" "PurgeMessage": "Ja mein Herr, ich lösche alle Nachrichten!",
"NoPermissionsMessage": "Nein!\nIch höre nicht auf dich ¯\\_(ツ)_/¯"
} }
} }

View File

@ -17,6 +17,7 @@ class BaseSettings(ConfigurationModelABC):
self._xp_per_ontime_hour: int = 0 self._xp_per_ontime_hour: int = 0
self._afk_channel_ids: list[int] = [] self._afk_channel_ids: list[int] = []
self._purge_message: str = '' self._purge_message: str = ''
self._no_permissions_message: str = ''
@property @property
def welcome_message(self) -> str: def welcome_message(self) -> str:
@ -50,6 +51,10 @@ class BaseSettings(ConfigurationModelABC):
def purge_message(self) -> str: def purge_message(self) -> str:
return self._purge_message return self._purge_message
@property
def no_permissions_message(self) -> str:
return self._no_permissions_message
def from_dict(self, settings: dict): def from_dict(self, settings: dict):
try: try:
self._welcome_message = settings['WelcomeMessage'] self._welcome_message = settings['WelcomeMessage']
@ -61,6 +66,7 @@ class BaseSettings(ConfigurationModelABC):
for id in settings['AFKChannelIds']: for id in settings['AFKChannelIds']:
self._afk_channel_ids.append(int(id)) self._afk_channel_ids.append(int(id))
self._purge_message = settings['PurgeMessage'] self._purge_message = settings['PurgeMessage']
self._no_permissions_message = settings['NoPermissionsMessage']
except Exception as e: except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -10,6 +10,7 @@ from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings from gismo_core.configuration.server_settings import ServerSettings
from modules.base.base_settings import BaseSettings from modules.base.base_settings import BaseSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class BaseCommandService(CommandABC): class BaseCommandService(CommandABC):
@ -18,13 +19,15 @@ class BaseCommandService(CommandABC):
self, self,
logger: LoggerABC, logger: LoggerABC,
config: ConfigurationABC, config: ConfigurationABC,
message_service: MessageServiceABC message_service: MessageServiceABC,
permissions: PermissionServiceABC
): ):
CommandABC.__init__(self) CommandABC.__init__(self)
self._logger = logger self._logger = logger
self._config = config self._config = config
self._message_service = message_service self._message_service = message_service
self._permissions = permissions
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@ -40,6 +43,11 @@ class BaseCommandService(CommandABC):
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}') settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}') server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
if (not self._permissions.is_member_moderator(ctx.author)):
await self._message_service.send_ctx_msg(ctx, settings.no_permissions_message)
self._logger.trace(__name__, f'Finished purge command')
return
await self._message_service.send_ctx_msg(ctx, settings.purge_message) await self._message_service.send_ctx_msg(ctx, settings.purge_message)
await asyncio.sleep(server_settings.message_delete_timer) await asyncio.sleep(server_settings.message_delete_timer)
await ctx.channel.purge() await ctx.channel.purge()

View File

@ -30,3 +30,9 @@ class PermissionServiceABC(ABC):
@abstractmethod @abstractmethod
def get_moderators(self, g_id: int) -> list[discord.Member]: pass def get_moderators(self, g_id: int) -> list[discord.Member]: pass
@abstractmethod
def is_member_admin(member: discord.Member) -> bool: pass
@abstractmethod
def is_member_moderator(member: discord.Member) -> bool: pass

View File

@ -105,3 +105,10 @@ class PermissionService(PermissionServiceABC):
def get_moderators(self, g_id: int) -> list[discord.Member]: def get_moderators(self, g_id: int) -> list[discord.Member]:
return self._moderators[g_id] return self._moderators[g_id]
def is_member_admin(self, member: discord.Member) -> bool:
return member in self._admins[member.guild.id]
def is_member_moderator(self, member: discord.Member) -> bool:
return member in self._moderators[member.guild.id] or self.is_member_admin(member)