[WIP] Added client utils service & Split up base command service

This commit is contained in:
Sven Heidemann 2022-01-15 13:56:17 +01:00
parent 234a9a382a
commit 0ef2428b9d
6 changed files with 202 additions and 95 deletions

View File

@ -0,0 +1,13 @@
from abc import ABC, abstractmethod
class ClientUtilsServiceABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def received_command(self, guild_id: int): pass
@abstractmethod
def moved_user(self, guild_id: int): pass

View File

@ -0,0 +1,36 @@
from cpl_core.database.context import DatabaseContextABC
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
class ClientUtilsService(ClientUtilsServiceABC):
def __init__(
self,
bot: BotServiceABC,
servers: ServerRepositoryABC,
clients: ClientRepositoryABC,
db: DatabaseContextABC
):
ClientUtilsServiceABC.__init__(self)
self._bot = bot
self._servers = servers
self._clients = clients
self._db = db
def received_command(self, guild_id: int):
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.received_command_count += 1
self._clients.update_client(client)
self._db.save_changes()
def moved_user(self, guild_id: int):
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.moved_users_count += 1
self._clients.update_client(client)
self._db.save_changes()

View File

@ -0,0 +1,62 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContext
from cpl_core.logging import LoggerABC
from discord import VoiceChannel
from discord.ext import commands
from discord.ext.commands import Context
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from modules.base.base_settings import BaseSettings
class AFKCommandService(CommandABC):
def __init__(
self,
logger: LoggerABC,
config: ConfigurationABC,
message_service: MessageServiceABC,
clients: ClientRepositoryABC,
db: DatabaseContext,
bot: BotServiceABC,
client_utils: ClientUtilsServiceABC
):
CommandABC.__init__(self)
self._logger = logger
self._config = config
self._message_service = message_service
self._clients = clients
self._db = db
self._bot = bot
self._client_utils = client_utils
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command()
async def afk(self, ctx: Context):
self._logger.debug(__name__, f'Received command afk {ctx}')
self._client_utils.received_command(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}')
if ctx.author.voice is None or ctx.author.voice.channel is None:
await self._message_service.send_ctx_msg(ctx, settings.afk_command_channel_missing_message)
self._logger.trace(__name__, f'Finished afk command')
return
self._bot.loop.create_task(self._message_service.send_ctx_msg(ctx, settings.afk_command_move_message))
channel: VoiceChannel = ctx.guild.get_channel(settings.afk_command_channel_id)
try:
await ctx.author.move_to(channel)
self._client_utils.moved_user(ctx.guild.id)
except Exception as e:
self._logger.error(__name__, f'Cannot move user {ctx.author.id} to channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished afk command')

View File

@ -1,95 +0,0 @@
import asyncio
from cpl_core.database.context.database_context import DatabaseContext
import discord
from discord.channel import VoiceChannel
from discord.ext import commands
from discord.ext.commands import Context
from cpl_core.logging import LoggerABC
from cpl_core.configuration import ConfigurationABC
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from modules.base.base_settings import BaseSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class BaseCommandService(CommandABC):
def __init__(
self,
logger: LoggerABC,
config: ConfigurationABC,
message_service: MessageServiceABC,
permissions: PermissionServiceABC,
clients: ClientRepositoryABC,
db: DatabaseContext,
bot: BotServiceABC
):
CommandABC.__init__(self)
self._logger = logger
self._config = config
self._message_service = message_service
self._permissions = permissions
self._clients = clients
self._db = db
self._bot = bot
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command()
async def ping(self, ctx: Context):
self._logger.debug(__name__, f'Received command ping {ctx}')
await self._message_service.send_ctx_msg(ctx, 'Pong')
self._logger.trace(__name__, f'Finished ping command')
@commands.command()
async def purge(self, ctx: Context):
self._logger.debug(__name__, f'Received command purge {ctx}')
settings: BaseSettings = self._config.get_configuration(f'Base_{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 asyncio.sleep(server_settings.message_delete_timer)
try:
await ctx.channel.purge()
except Exception as e:
self._logger.error(__name__, f'Cannot purge channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished purge command')
@commands.command()
async def afk(self, ctx: Context):
self._logger.debug(__name__, f'Received command afk {ctx}')
settings: BaseSettings = self._config.get_configuration(f'Base_{ctx.guild.id}')
server_settings: ServerSettings = self._config.get_configuration(f'DSERVER_{ctx.guild.id}')
if ctx.author.voice is None or ctx.author.voice.channel is None:
await self._message_service.send_ctx_msg(ctx, settings.afk_command_channel_missing_message)
self._logger.trace(__name__, f'Finished afk command')
return
self._bot.loop.create_task(self._message_service.send_ctx_msg(ctx, settings.afk_command_move_message))
channel: VoiceChannel = ctx.guild.get_channel(settings.afk_command_channel_id)
try:
await ctx.author.move_to(channel)
client = self._clients.get_client_by_discord_id(self._bot.user.id)
client.moved_users_count += 1
self._clients.update_client(client)
self._db.save_changes()
except Exception as e:
self._logger.error(__name__, f'Cannot move user {ctx.author.id} to channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished afk command')

View File

@ -0,0 +1,34 @@
from cpl_core.logging import LoggerABC
from discord.ext import commands
from discord.ext.commands import Context
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
class PingCommandService(CommandABC):
def __init__(
self,
logger: LoggerABC,
message_service: MessageServiceABC,
bot: BotServiceABC,
client_utils: ClientUtilsServiceABC
):
CommandABC.__init__(self)
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command()
async def ping(self, ctx: Context):
self._logger.debug(__name__, f'Received command ping {ctx}')
self._client_utils.received_command(ctx.guild.id)
await self._message_service.send_ctx_msg(ctx, 'Pong')
self._logger.trace(__name__, f'Finished ping command')

View File

@ -0,0 +1,57 @@
import asyncio
from cpl_core.configuration import ConfigurationABC
from cpl_core.logging import LoggerABC
from discord.ext import commands
from discord.ext.commands import Context
from gismo_core.abc.client_utils_service_abc import ClientUtilsServiceABC
from gismo_core.abc.command_abc import CommandABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from modules.base.base_settings import BaseSettings
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class PurgeCommandService(CommandABC):
def __init__(
self,
logger: LoggerABC,
config: ConfigurationABC,
message_service: MessageServiceABC,
permissions: PermissionServiceABC,
client_utils: ClientUtilsServiceABC
):
CommandABC.__init__(self)
self._logger = logger
self._config = config
self._message_service = message_service
self._permissions = permissions
self._client_utils = client_utils
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
@commands.command()
async def purge(self, ctx: Context):
self._logger.debug(__name__, f'Received command purge {ctx}')
self._client_utils.received_command(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}')
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 asyncio.sleep(server_settings.message_delete_timer)
try:
await ctx.channel.purge()
except Exception as e:
self._logger.error(__name__, f'Cannot purge channel {ctx.channel.id}', e)
await self._message_service.send_ctx_msg(ctx, server_settings.bot_has_no_permission_message)
self._logger.trace(__name__, f'Finished purge command')