Added logic to add commands
This commit is contained in:
parent
7dd2d13b58
commit
ed2dab6ff8
@ -11,6 +11,7 @@ from cpl_core.environment import ApplicationEnvironment
|
|||||||
from cpl_core.logging import LoggerABC
|
from cpl_core.logging import LoggerABC
|
||||||
|
|
||||||
from gismo_core.abc.bot_service_abc import BotServiceABC
|
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.abc.message_service_abc import MessageServiceABC
|
||||||
from gismo_core.service.bot_service import BotService
|
from gismo_core.service.bot_service import BotService
|
||||||
from gismo_core.service.message_service import MessageService
|
from gismo_core.service.message_service import MessageService
|
||||||
@ -35,6 +36,7 @@ from gismo_data.service.user_joined_server_repository_service import UserJoinedS
|
|||||||
from gismo_data.service.user_joined_voice_channel_service import UserJoinedVoiceChannelRepositoryService
|
from gismo_data.service.user_joined_voice_channel_service import UserJoinedVoiceChannelRepositoryService
|
||||||
from gismo_data.service.user_repository_service import UserRepositoryService
|
from gismo_data.service.user_repository_service import UserRepositoryService
|
||||||
from modules.base.base import Base
|
from modules.base.base import Base
|
||||||
|
from modules.base.service.base_command_service import BaseCommandService
|
||||||
from modules.boot_log.boot_log import BootLog
|
from modules.boot_log.boot_log import BootLog
|
||||||
from modules.database.database import Database
|
from modules.database.database import Database
|
||||||
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
||||||
@ -88,13 +90,15 @@ class Startup(StartupABC):
|
|||||||
# module services
|
# module services
|
||||||
services.add_singleton(PermissionServiceABC, PermissionService)
|
services.add_singleton(PermissionServiceABC, PermissionService)
|
||||||
|
|
||||||
|
# commands
|
||||||
|
services.add_singleton(CommandABC, BaseCommandService)
|
||||||
|
|
||||||
# modules
|
# modules
|
||||||
services.add_transient(ModuleABC, Database)
|
services.add_transient(ModuleABC, Database)
|
||||||
services.add_transient(ModuleABC, BootLog)
|
services.add_transient(ModuleABC, BootLog)
|
||||||
services.add_singleton(ModuleABC, Permission)
|
services.add_singleton(ModuleABC, Permission)
|
||||||
services.add_singleton(ModuleABC, Base)
|
services.add_singleton(ModuleABC, Base)
|
||||||
|
|
||||||
|
|
||||||
# migrations
|
# migrations
|
||||||
services.add_transient(MigrationABC, InitialMigration)
|
services.add_transient(MigrationABC, InitialMigration)
|
||||||
services.add_transient(MigrationABC, Migration_0_3)
|
services.add_transient(MigrationABC, Migration_0_3)
|
||||||
|
11
src/gismo_core/abc/command_abc.py
Normal file
11
src/gismo_core/abc/command_abc.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
from gismo_core.commands_meta import CommandsMeta
|
||||||
|
|
||||||
|
|
||||||
|
class CommandABC(ABC, commands.Cog, metaclass=CommandsMeta):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self): pass
|
5
src/gismo_core/commands_meta.py
Normal file
5
src/gismo_core/commands_meta.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from abc import ABCMeta
|
||||||
|
from discord.ext import commands
|
||||||
|
|
||||||
|
|
||||||
|
class CommandsMeta(ABCMeta, commands.CogMeta): pass
|
@ -26,11 +26,14 @@ class MessageService(MessageServiceABC):
|
|||||||
async def delete_messages(self, messages: List[discord.Message]):
|
async def delete_messages(self, messages: List[discord.Message]):
|
||||||
self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
|
self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
|
||||||
for message in messages:
|
for message in messages:
|
||||||
await self.delete_message(message)
|
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}')
|
||||||
|
await asyncio.sleep(server_st.message_delete_timer)
|
||||||
|
await self.delete_message(message, mass_delete=True)
|
||||||
self._logger.debug(__name__, 'Deleting messages finished')
|
self._logger.debug(__name__, 'Deleting messages finished')
|
||||||
|
|
||||||
async def delete_message(self, message: discord.Message):
|
async def delete_message(self, message: discord.Message, mass_delete=False):
|
||||||
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}')
|
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}')
|
||||||
|
if not mass_delete:
|
||||||
await asyncio.sleep(server_st.message_delete_timer)
|
await asyncio.sleep(server_st.message_delete_timer)
|
||||||
self._logger.debug(__name__, f'Try to delete message:\n\t{message}\n\t{message.content}')
|
self._logger.debug(__name__, f'Try to delete message:\n\t{message}\n\t{message.content}')
|
||||||
guild_id = message.guild.id
|
guild_id = message.guild.id
|
||||||
@ -86,4 +89,4 @@ class MessageService(MessageServiceABC):
|
|||||||
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')
|
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')
|
||||||
self._clients.apppend_sent_message_count(self._bot.user.id, ctx.guild.id, 1)
|
self._clients.apppend_sent_message_count(self._bot.user.id, ctx.guild.id, 1)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
await self.delete_message(msg)
|
await self.delete_messages(List(discord.Message, [msg, ctx.message]))
|
||||||
|
@ -59,12 +59,10 @@ from gismo_core.abc.events.on_voice_state_update_abc import OnVoiceStateUpdateAB
|
|||||||
from gismo_core.abc.events.on_webhooks_update_abc import OnWebhooksUpdateABC
|
from gismo_core.abc.events.on_webhooks_update_abc import OnWebhooksUpdateABC
|
||||||
from gismo_core.abc.module_abc import ModuleABC
|
from gismo_core.abc.module_abc import ModuleABC
|
||||||
from gismo_core.abc.module_service_abc import ModuleServiceABC
|
from gismo_core.abc.module_service_abc import ModuleServiceABC
|
||||||
|
from gismo_core.commands_meta import CommandsMeta
|
||||||
|
|
||||||
|
|
||||||
class _MetaCogABC(ABCMeta, commands.CogMeta): pass
|
class ModuleService(ModuleServiceABC, commands.Cog, metaclass=CommandsMeta):
|
||||||
|
|
||||||
|
|
||||||
class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC):
|
|
||||||
|
|
||||||
def __init__(self, logger: LoggerABC, config: ConfigurationABC, services: ServiceProviderABC, env: ApplicationEnvironmentABC):
|
def __init__(self, logger: LoggerABC, config: ConfigurationABC, services: ServiceProviderABC, env: ApplicationEnvironmentABC):
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
@ -90,7 +88,7 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC):
|
|||||||
settings: ConfigurationModelABC = module.settings_type()
|
settings: ConfigurationModelABC = module.settings_type()
|
||||||
settings.from_dict(json_cfg[id])
|
settings.from_dict(json_cfg[id])
|
||||||
self._config.add_configuration(f'{type(module).__name__}_{id}', settings)
|
self._config.add_configuration(f'{type(module).__name__}_{id}', settings)
|
||||||
self._logger.info(__name__, f'Added config: {type(module).__name__}_{id}')
|
self._logger.debug(__name__, f'Added config: {type(module).__name__}_{id}')
|
||||||
|
|
||||||
modules.append(module)
|
modules.append(module)
|
||||||
|
|
||||||
|
@ -16,10 +16,12 @@ from gismo_data.abc.user_joined_server_repository_abc import \
|
|||||||
from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC
|
from gismo_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC
|
||||||
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
from gismo_data.abc.user_repository_abc import UserRepositoryABC
|
||||||
from gismo_data.model.known_user import KnownUser
|
from gismo_data.model.known_user import KnownUser
|
||||||
|
from gismo_data.model.server import Server
|
||||||
from gismo_data.model.user import User
|
from gismo_data.model.user import User
|
||||||
from gismo_data.model.user_joined_server import UserJoinedServer
|
from gismo_data.model.user_joined_server import UserJoinedServer
|
||||||
from gismo_data.model.user_joined_voice_channel import UserJoinedVoiceChannel
|
from gismo_data.model.user_joined_voice_channel import UserJoinedVoiceChannel
|
||||||
from modules.base.base_settings import BaseSettings
|
from modules.base.base_settings import BaseSettings
|
||||||
|
from modules.base.service.base_command_service import BaseCommandService
|
||||||
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
||||||
from gismo_core.abc.events.on_member_join_abc import OnMemberJoinABC
|
from gismo_core.abc.events.on_member_join_abc import OnMemberJoinABC
|
||||||
from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC
|
from gismo_core.abc.events.on_member_remove_abc import OnMemberRemoveABC
|
||||||
@ -44,7 +46,8 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
bot: BotServiceABC,
|
bot: BotServiceABC,
|
||||||
db: DatabaseContextABC,
|
db: DatabaseContextABC,
|
||||||
messenger: MessageServiceABC,
|
messenger: MessageServiceABC,
|
||||||
permission_service: PermissionServiceABC
|
permission_service: PermissionServiceABC,
|
||||||
|
base_command_service: BaseCommandService
|
||||||
):
|
):
|
||||||
self._config = config
|
self._config = config
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
@ -69,6 +72,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
},
|
},
|
||||||
BaseSettings
|
BaseSettings
|
||||||
)
|
)
|
||||||
|
self._bot.add_cog(base_command_service)
|
||||||
self._logger.info(__name__, f'Module {type(self)} loaded')
|
self._logger.info(__name__, f'Module {type(self)} loaded')
|
||||||
|
|
||||||
def _get_config(self, g_id: int) -> BaseSettings:
|
def _get_config(self, g_id: int) -> BaseSettings:
|
||||||
@ -103,7 +107,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
|
|
||||||
async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]):
|
async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]):
|
||||||
self._logger.debug(__name__, f'Check if user exists {member}')
|
self._logger.debug(__name__, f'Check if user exists {member}')
|
||||||
settings: BaseSettings = self._get_config()
|
settings: BaseSettings = self._get_config(member.guild.id)
|
||||||
await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member)
|
await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member)
|
||||||
|
|
||||||
for admin in self._permission_service.get_admins():
|
for admin in self._permission_service.get_admins():
|
||||||
@ -131,7 +135,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
|
|
||||||
async def _remove_user(self, member: Union[discord.User, discord.Member]):
|
async def _remove_user(self, member: Union[discord.User, discord.Member]):
|
||||||
self._logger.debug(__name__, f'Remove user {member}')
|
self._logger.debug(__name__, f'Remove user {member}')
|
||||||
settings: BaseSettings = self._get_config()
|
settings: BaseSettings = self._get_config(member.guild.id)
|
||||||
await self._messenger.send_dm_message(settings.goodbye_message, member)
|
await self._messenger.send_dm_message(settings.goodbye_message, member)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -149,10 +153,10 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot get user {member.id}', e)
|
self._logger.error(__name__, f'Cannot get user {member.id}', e)
|
||||||
|
|
||||||
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, srv_id: int):
|
def _update_voice_state(self, joined: bool, dc_user_id: int, dc_channel_id: int, server: Server):
|
||||||
user: User = None
|
user: User = None
|
||||||
try:
|
try:
|
||||||
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, srv_id)
|
user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot get user {dc_user_id}', e)
|
self._logger.error(__name__, f'Cannot get user {dc_user_id}', e)
|
||||||
return
|
return
|
||||||
@ -168,7 +172,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
return
|
return
|
||||||
|
|
||||||
settings: BaseSettings = self._get_config()
|
settings: BaseSettings = self._get_config(server.discord_server_id)
|
||||||
|
|
||||||
join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id)
|
join = self._user_joins_vc.get_active_user_joined_voice_channel_by_user_id(user.user_id)
|
||||||
join.leaved_on = datetime.now()
|
join.leaved_on = datetime.now()
|
||||||
@ -205,7 +209,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._logger.error(__name__, f'User not found {dc_user_id}')
|
self._logger.error(__name__, f'User not found {dc_user_id}')
|
||||||
return
|
return
|
||||||
|
|
||||||
settings: BaseSettings = self._get_config()
|
settings: BaseSettings = self._get_config(message.guild.id)
|
||||||
old_xp = user.xp
|
old_xp = user.xp
|
||||||
user.xp += settings._xp_per_message
|
user.xp += settings._xp_per_message
|
||||||
self._users.update_user(user)
|
self._users.update_user(user)
|
||||||
@ -235,7 +239,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
|
|||||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||||
self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}')
|
self._logger.trace(__name__, f'Detected on_voice_state_update {member.id} from {before} to {after}')
|
||||||
u: discord.User = member
|
u: discord.User = member
|
||||||
settings: BaseSettings = self._get_config()
|
settings: BaseSettings = self._get_config(member.guild.id)
|
||||||
server = self._servers.get_server_by_discord_id(member.guild.id)
|
server = self._servers.get_server_by_discord_id(member.guild.id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
1
src/modules/base/service/__init__.py
Normal file
1
src/modules/base/service/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
# imports
|
27
src/modules/base/service/base_command_service.py
Normal file
27
src/modules/base/service/base_command_service.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from discord.ext.commands import Context
|
||||||
|
|
||||||
|
from cpl_core.logging import LoggerABC
|
||||||
|
from gismo_core.abc.command_abc import CommandABC
|
||||||
|
from gismo_core.abc.message_service_abc import MessageServiceABC
|
||||||
|
|
||||||
|
|
||||||
|
class BaseCommandService(CommandABC):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
logger: LoggerABC,
|
||||||
|
message_service: MessageServiceABC
|
||||||
|
):
|
||||||
|
CommandABC.__init__(self)
|
||||||
|
|
||||||
|
self._logger = logger
|
||||||
|
self._message_service = message_service
|
||||||
|
|
||||||
|
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')
|
@ -79,5 +79,6 @@ class BootLog(ModuleABC, OnReadyABC):
|
|||||||
),
|
),
|
||||||
module_settings.login_message.format(init_time)
|
module_settings.login_message.format(init_time)
|
||||||
)
|
)
|
||||||
|
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')
|
||||||
|
Reference in New Issue
Block a user