Reviewed-on: sh-edraft.de/kd_discord_bot#49 Reviewed-by: edraft-dev <dev.sven.heidemann@sh-edraft.de> Closes #42
This commit is contained in:
commit
03497b6d5a
@ -37,7 +37,8 @@
|
|||||||
"extension_already_loaded": "Fehler: Erweiterung wurde bereits geladen!",
|
"extension_already_loaded": "Fehler: Erweiterung wurde bereits geladen!",
|
||||||
"extension_not_loaded": "Fehler: Erweiterung wurde nicht geladen!",
|
"extension_not_loaded": "Fehler: Erweiterung wurde nicht geladen!",
|
||||||
"no_entry_point_error": "Fehler: Kein Eintrittspunkt!",
|
"no_entry_point_error": "Fehler: Kein Eintrittspunkt!",
|
||||||
"extension_failed": "Fehler: Erweiterung ist fehlgeschlagen!"
|
"extension_failed": "Fehler: Erweiterung ist fehlgeschlagen!",
|
||||||
|
"bot_not_ready_yet": "Ey Alter! Gedulde dich doch mal! ..."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"modules": {
|
"modules": {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
|
||||||
class ClientUtilsServiceABC(ABC):
|
class ClientUtilsServiceABC(ABC):
|
||||||
|
|
||||||
@ -14,3 +16,6 @@ class ClientUtilsServiceABC(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_client(self, dc_ic: int, guild_id: int): pass
|
def get_client(self, dc_ic: int, guild_id: int): pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: pass
|
||||||
|
@ -12,16 +12,18 @@ class BotSettings(ConfigurationModelABC):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
ConfigurationModelABC.__init__(self)
|
ConfigurationModelABC.__init__(self)
|
||||||
|
|
||||||
self._servers: List[ServerSettings] = List()
|
self._servers: List[ServerSettings] = List(ServerSettings)
|
||||||
self._technicians: list[int] = []
|
self._technicians: List[int] = List(int)
|
||||||
self._deploy_file_path = './'
|
self._deploy_file_path = './'
|
||||||
|
self._wait_for_restart = 2
|
||||||
|
self._wait_for_shutdown = 2
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def servers(self) -> List[ServerSettings]:
|
def servers(self) -> List[ServerSettings]:
|
||||||
return self._servers
|
return self._servers
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def technicians(self) -> list[int]:
|
def technicians(self) -> List[int]:
|
||||||
return self._technicians
|
return self._technicians
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
|
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_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
from cpl_translation import TranslatePipe
|
||||||
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
|
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
|
||||||
|
from bot_core.abc.message_service_abc import MessageServiceABC
|
||||||
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
||||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||||
|
|
||||||
@ -11,16 +16,24 @@ class ClientUtilsService(ClientUtilsServiceABC):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
config: ConfigurationABC,
|
||||||
|
logger: LoggerABC,
|
||||||
bot: DiscordBotServiceABC,
|
bot: DiscordBotServiceABC,
|
||||||
servers: ServerRepositoryABC,
|
servers: ServerRepositoryABC,
|
||||||
clients: ClientRepositoryABC,
|
clients: ClientRepositoryABC,
|
||||||
db: DatabaseContextABC
|
message_service: MessageServiceABC,
|
||||||
|
db: DatabaseContextABC,
|
||||||
|
t: TranslatePipe
|
||||||
):
|
):
|
||||||
ClientUtilsServiceABC.__init__(self)
|
ClientUtilsServiceABC.__init__(self)
|
||||||
|
self._config = config
|
||||||
|
self._logger = logger
|
||||||
self._bot = bot
|
self._bot = bot
|
||||||
self._servers = servers
|
self._servers = servers
|
||||||
self._clients = clients
|
self._clients = clients
|
||||||
|
self._message_service = message_service
|
||||||
self._db = db
|
self._db = db
|
||||||
|
self._t = t
|
||||||
|
|
||||||
def received_command(self, guild_id: int):
|
def received_command(self, guild_id: int):
|
||||||
server = self._servers.get_server_by_discord_id(guild_id)
|
server = self._servers.get_server_by_discord_id(guild_id)
|
||||||
@ -40,3 +53,11 @@ class ClientUtilsService(ClientUtilsServiceABC):
|
|||||||
server = self._servers.get_server_by_discord_id(guild_id)
|
server = self._servers.get_server_by_discord_id(guild_id)
|
||||||
client = self._clients.find_client_by_discord_id_and_server_id(self._bot.user.id, server.server_id)
|
client = self._clients.find_client_by_discord_id_and_server_id(self._bot.user.id, server.server_id)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool:
|
||||||
|
if self._config.get_configuration('IS_READY') == 'true':
|
||||||
|
return True
|
||||||
|
|
||||||
|
self._logger.debug(__name__, f'Bot is not ready yet {self._t.transform("common.errors.bot_not_ready_yet")}')
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'), without_tracking=True)
|
||||||
|
return False
|
||||||
|
@ -40,6 +40,7 @@ class MessageService(MessageServiceABC):
|
|||||||
guild_id = message.guild.id
|
guild_id = message.guild.id
|
||||||
try:
|
try:
|
||||||
await message.delete()
|
await message.delete()
|
||||||
|
await asyncio.sleep(server_st.message_delete_timer)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Deleting message failed', e)
|
self._logger.error(__name__, f'Deleting message failed', e)
|
||||||
else:
|
else:
|
||||||
@ -105,8 +106,7 @@ class MessageService(MessageServiceABC):
|
|||||||
await asyncio.sleep(wait_before_delete)
|
await asyncio.sleep(wait_before_delete)
|
||||||
|
|
||||||
if is_persistent:
|
if is_persistent:
|
||||||
await self.delete_message(ctx.message, without_tracking)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if ctx.guild is not None:
|
if ctx.guild is not None:
|
||||||
await self.delete_messages(List(discord.Message, [msg, ctx.message]), ctx.guild.id)
|
await self.delete_message(msg, without_tracking)
|
||||||
|
@ -35,10 +35,13 @@ class RestartCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def restart(self, ctx: Context):
|
async def restart(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command restart {ctx}')
|
self._logger.debug(__name__, f'Received command restart {ctx}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
|
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
|
|
||||||
if not self._permissions.is_member_moderator(ctx.author):
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
@ -35,10 +35,13 @@ class ShutdownCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def shutdown(self, ctx: Context):
|
async def shutdown(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command shutdown {ctx}')
|
self._logger.debug(__name__, f'Received command shutdown {ctx}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
|
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
|
|
||||||
if not self._permissions.is_member_moderator(ctx.author):
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
@ -34,10 +34,12 @@ class AFKCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def afk(self, ctx: Context):
|
async def afk(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command afk {ctx}')
|
self._logger.debug(__name__, f'Received command afk {ctx}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
|
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
|
||||||
|
|
||||||
|
@ -30,10 +30,12 @@ class HelpCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def help(self, ctx: Context, persistent_flag: str = None):
|
async def help(self, ctx: Context, persistent_flag: str = None):
|
||||||
self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}')
|
self._logger.debug(__name__, f'Received command help {ctx}:{persistent_flag}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
|
settings: BaseServerSettings = self._config.get_configuration(f'BaseServerSettings_{ctx.guild.id}')
|
||||||
is_persistent = persistent_flag == '--stay'
|
is_persistent = persistent_flag == '--stay'
|
||||||
|
@ -36,10 +36,12 @@ class InfoCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def info(self, ctx: Context, *, wait: int = None):
|
async def info(self, ctx: Context, *, wait: int = None):
|
||||||
self._logger.debug(__name__, f'Received command info {ctx},{wait}')
|
self._logger.debug(__name__, f'Received command info {ctx},{wait}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id)
|
client = self._client_utils.get_client(self._bot.user.id, ctx.guild.id)
|
||||||
|
|
||||||
@ -58,7 +60,7 @@ class InfoCommand(DiscordCommandABC):
|
|||||||
embed.add_field(name=self._t.transform('modules.base.info.fields.deleted_message_count'), value=client.deleted_message_count, inline=False)
|
embed.add_field(name=self._t.transform('modules.base.info.fields.deleted_message_count'), value=client.deleted_message_count, inline=False)
|
||||||
embed.add_field(name=self._t.transform('modules.base.info.fields.received_command_count'), value=client.received_command_count)
|
embed.add_field(name=self._t.transform('modules.base.info.fields.received_command_count'), value=client.received_command_count)
|
||||||
embed.add_field(name=self._t.transform('modules.base.info.fields.moved_users_count'), value=client.moved_users_count)
|
embed.add_field(name=self._t.transform('modules.base.info.fields.moved_users_count'), value=client.moved_users_count)
|
||||||
modules = ['Base', 'BootLog', 'Database', 'Permission']
|
modules = ['Admin', 'Base', 'BootLog', 'Database', 'Moderator', 'Permission']
|
||||||
embed.add_field(name=self._t.transform('modules.base.info.fields.modules'), value='\n'.join(modules), inline=False)
|
embed.add_field(name=self._t.transform('modules.base.info.fields.modules'), value='\n'.join(modules), inline=False)
|
||||||
|
|
||||||
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
|
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
|
||||||
|
@ -29,10 +29,12 @@ class PingCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def ping(self, ctx: Context):
|
async def ping(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command ping {ctx}')
|
self._logger.debug(__name__, f'Received command ping {ctx}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.base.pong'))
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.base.pong'))
|
||||||
self._logger.trace(__name__, f'Finished ping command')
|
self._logger.trace(__name__, f'Finished ping command')
|
||||||
|
@ -50,10 +50,12 @@ class UserInfoCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command(name='user-info')
|
@commands.hybrid_command(name='user-info')
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def user_info(self, ctx: Context, member: Optional[discord.Member] = None, *, wait: int = None):
|
async def user_info(self, ctx: Context, member: Optional[discord.Member] = None, *, wait: int = None):
|
||||||
self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}')
|
self._logger.debug(__name__, f'Received command user-info {ctx}:{member},{wait}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
|
|
||||||
if not self._permissions.is_member_moderator(ctx.author):
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
@ -72,6 +72,7 @@ class BootLogOnReadyEvent(OnReadyABC):
|
|||||||
),
|
),
|
||||||
self._t.transform('modules.boot_log.login_message').format(init_time)
|
self._t.transform('modules.boot_log.login_message').format(init_time)
|
||||||
)
|
)
|
||||||
self._logger.info(__name__, 'Bot is ready')
|
|
||||||
|
|
||||||
|
self._config.add_configuration('IS_READY', 'true')
|
||||||
|
self._logger.info(__name__, 'Bot is ready')
|
||||||
self._logger.trace(__name__, f'Module {type(self)} stopped')
|
self._logger.trace(__name__, f'Module {type(self)} stopped')
|
||||||
|
@ -35,12 +35,14 @@ class PurgeCommand(DiscordCommandABC):
|
|||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@commands.command()
|
@commands.hybrid_command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def purge(self, ctx: Context):
|
async def purge(self, ctx: Context):
|
||||||
self._logger.debug(__name__, f'Received command purge {ctx}')
|
self._logger.debug(__name__, f'Received command purge {ctx}')
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
|
|
||||||
|
self._client_utils.received_command(ctx.guild.id)
|
||||||
server_settings: ServerSettings = self._config.get_configuration(f'ServerSettings_{ctx.guild.id}')
|
server_settings: ServerSettings = self._config.get_configuration(f'ServerSettings_{ctx.guild.id}')
|
||||||
|
|
||||||
if not self._permissions.is_member_moderator(ctx.author):
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
@ -106,7 +106,7 @@ class PermissionService(PermissionServiceABC):
|
|||||||
return self._moderators[g_id]
|
return self._moderators[g_id]
|
||||||
|
|
||||||
def is_member_admin(self, member: discord.Member) -> bool:
|
def is_member_admin(self, member: discord.Member) -> bool:
|
||||||
return member in self._admins[member.guild.id]
|
return member.guild.id in self._admins and member in self._admins[member.guild.id]
|
||||||
|
|
||||||
def is_member_moderator(self, member: discord.Member) -> bool:
|
def is_member_moderator(self, member: discord.Member) -> bool:
|
||||||
return member in self._moderators[member.guild.id] or self.is_member_admin(member)
|
return member.guild.id in self._moderators and member in self._moderators[member.guild.id] or self.is_member_admin(member)
|
||||||
|
Loading…
Reference in New Issue
Block a user