forked from sh-edraft.de/sh_discord_bot
Added auto-role remove command & some refactoring #54
This commit is contained in:
parent
5795028fdb
commit
b75777a339
@ -67,6 +67,12 @@
|
|||||||
"already_exists": "auto-role für die Nachricht {} existiert bereits!"
|
"already_exists": "auto-role für die Nachricht {} existiert bereits!"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"remove": {
|
||||||
|
"success": "auto-role {} wurde entfernt :D",
|
||||||
|
"error": {
|
||||||
|
"not_found": "auto-role {} nicht gefunden!"
|
||||||
|
}
|
||||||
|
},
|
||||||
"rule": {
|
"rule": {
|
||||||
"list": {
|
"list": {
|
||||||
"title": "auto-role Regeln:",
|
"title": "auto-role Regeln:",
|
||||||
|
@ -5,7 +5,6 @@ from cpl_query.extension import List
|
|||||||
|
|
||||||
from bot_data.model.auto_role import AutoRole
|
from bot_data.model.auto_role import AutoRole
|
||||||
from bot_data.model.auto_role_rule import AutoRoleRule
|
from bot_data.model.auto_role_rule import AutoRoleRule
|
||||||
from bot_data.model.server import Server
|
|
||||||
|
|
||||||
|
|
||||||
class AutoRoleRepositoryABC(ABC):
|
class AutoRoleRepositoryABC(ABC):
|
||||||
@ -19,6 +18,9 @@ class AutoRoleRepositoryABC(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_auto_role_by_id(self, id: int) -> AutoRole: pass
|
def get_auto_role_by_id(self, id: int) -> AutoRole: pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass
|
def get_auto_roles_by_server_id(self, id: int) -> AutoRole: pass
|
||||||
|
|
||||||
@ -32,13 +34,13 @@ class AutoRoleRepositoryABC(ABC):
|
|||||||
def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass
|
def find_auto_roles_by_message_id(self, id: int) -> Optional[AutoRole]: pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_auto_role(self, server: AutoRole): pass
|
def add_auto_role(self, auto_role: AutoRole): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def update_auto_role(self, server: AutoRole): pass
|
def update_auto_role(self, auto_role: AutoRole): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def delete_auto_role(self, server: AutoRole): pass
|
def delete_auto_role(self, auto_role: AutoRole): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_auto_role_rules(self) -> List[AutoRoleRule]: pass
|
def get_auto_role_rules(self) -> List[AutoRoleRule]: pass
|
||||||
@ -50,10 +52,10 @@ class AutoRoleRepositoryABC(ABC):
|
|||||||
def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass
|
def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_auto_role_rule(self, server: AutoRoleRule): pass
|
def add_auto_role_rule(self, auto_role: AutoRoleRule): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def update_auto_role_rule(self, server: AutoRoleRule): pass
|
def update_auto_role_rule(self, auto_role: AutoRoleRule): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def delete_auto_role_rule(self, server: AutoRoleRule): pass
|
def delete_auto_role_rule(self, auto_role: AutoRoleRule): pass
|
||||||
|
@ -17,11 +17,19 @@ class AutoRoleRule(TableABC):
|
|||||||
self._modified_at = modified_at if modified_at is not None else self._modified_at
|
self._modified_at = modified_at if modified_at is not None else self._modified_at
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def emoji_name(self):
|
def auto_role_rule_id(self) -> int:
|
||||||
|
return self._auto_role_rule_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def auto_role_id(self) -> int:
|
||||||
|
return self._auto_role_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def emoji_name(self) -> str:
|
||||||
return self._discord_emoji_name
|
return self._discord_emoji_name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def role_id(self):
|
def role_id(self) -> int:
|
||||||
return self._discord_role_id
|
return self._discord_role_id
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -43,6 +43,22 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
|
|||||||
id=result[0]
|
id=result[0]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]:
|
||||||
|
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_id_string(id)}')
|
||||||
|
result = self._context.select(AutoRole.get_select_by_id_string(id))
|
||||||
|
if result is None or len(result) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
result = result[0]
|
||||||
|
|
||||||
|
return AutoRole(
|
||||||
|
result[1],
|
||||||
|
result[2],
|
||||||
|
result[3],
|
||||||
|
result[4],
|
||||||
|
id=result[0]
|
||||||
|
)
|
||||||
|
|
||||||
def get_auto_roles_by_server_id(self, id: int) -> AutoRole:
|
def get_auto_roles_by_server_id(self, id: int) -> AutoRole:
|
||||||
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}')
|
self._logger.trace(__name__, f'Send SQL command: {AutoRole.get_select_by_server_id_string(id)}')
|
||||||
result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0]
|
result = self._context.select(AutoRole.get_select_by_server_id_string(id))[0]
|
||||||
|
@ -17,6 +17,7 @@ from bot_core.logging.command_logger import CommandLogger
|
|||||||
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
|
||||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||||
from bot_data.model.auto_role import AutoRole
|
from bot_data.model.auto_role import AutoRole
|
||||||
|
from modules.permission.abc.permission_service_abc import PermissionServiceABC
|
||||||
|
|
||||||
|
|
||||||
class AutoRoleGroup(DiscordCommandABC):
|
class AutoRoleGroup(DiscordCommandABC):
|
||||||
@ -30,7 +31,8 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
translate: TranslatePipe,
|
translate: TranslatePipe,
|
||||||
servers: ServerRepositoryABC,
|
servers: ServerRepositoryABC,
|
||||||
auto_roles: AutoRoleRepositoryABC,
|
auto_roles: AutoRoleRepositoryABC,
|
||||||
db_context: DatabaseContextABC
|
db_context: DatabaseContextABC,
|
||||||
|
permission_service: PermissionServiceABC,
|
||||||
):
|
):
|
||||||
DiscordCommandABC.__init__(self)
|
DiscordCommandABC.__init__(self)
|
||||||
|
|
||||||
@ -42,6 +44,7 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
self._servers = servers
|
self._servers = servers
|
||||||
self._auto_roles = auto_roles
|
self._auto_roles = auto_roles
|
||||||
self._db_context = db_context
|
self._db_context = db_context
|
||||||
|
self._permissions = permission_service
|
||||||
|
|
||||||
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
|
||||||
|
|
||||||
@ -56,7 +59,11 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
self._logger.debug(__name__, f'Received command auto-role list {ctx}')
|
self._logger.debug(__name__, f'Received command auto-role list {ctx}')
|
||||||
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
return
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
|
||||||
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
|
||||||
|
self._logger.trace(__name__, f'Finished purge command')
|
||||||
|
return
|
||||||
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=self._t.transform('modules.auto_role.list.title'),
|
title=self._t.transform('modules.auto_role.list.title'),
|
||||||
@ -84,6 +91,14 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def add(self, ctx: Context, message_id: str):
|
async def add(self, ctx: Context, message_id: str):
|
||||||
self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}')
|
self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
|
|
||||||
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
|
||||||
|
self._logger.trace(__name__, f'Finished purge command')
|
||||||
|
return
|
||||||
|
|
||||||
message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default()
|
message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default()
|
||||||
if message is None:
|
if message is None:
|
||||||
self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}')
|
self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}')
|
||||||
@ -110,6 +125,40 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
messages = [message async for message in channel.history(limit=10)]
|
messages = [message async for message in channel.history(limit=10)]
|
||||||
return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)]
|
return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)]
|
||||||
|
|
||||||
|
@auto_role.command()
|
||||||
|
@commands.guild_only()
|
||||||
|
async def remove(self, ctx: Context, auto_role: int):
|
||||||
|
self._logger.debug(__name__, f'Received command auto-role remove {ctx} {auto_role}')
|
||||||
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
|
return
|
||||||
|
|
||||||
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
|
||||||
|
self._logger.trace(__name__, f'Finished purge command')
|
||||||
|
return
|
||||||
|
|
||||||
|
auto_role_from_db = self._auto_roles.find_auto_role_by_id(auto_role)
|
||||||
|
if auto_role_from_db is None:
|
||||||
|
self._logger.debug(__name__, f'AutoRole {auto_role} not found')
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.error.not_found').format(auto_role))
|
||||||
|
self._logger.trace(__name__, f'Finished command auto-role remove')
|
||||||
|
return
|
||||||
|
|
||||||
|
for rule in self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_from_db.auto_role_id):
|
||||||
|
self._auto_roles.delete_auto_role_rule(rule)
|
||||||
|
self._logger.info(__name__, f'Removed AutoRole rule {rule.role_id}')
|
||||||
|
|
||||||
|
self._auto_roles.delete_auto_role(auto_role_from_db)
|
||||||
|
self._db_context.save_changes()
|
||||||
|
self._logger.info(__name__, f'Removed AutoRole {auto_role}')
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.remove.success').format(auto_role))
|
||||||
|
self._logger.trace(__name__, f'Finished command auto-role remove')
|
||||||
|
|
||||||
|
@remove.autocomplete('auto_role')
|
||||||
|
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
|
||||||
|
auto_roles = self._auto_roles.get_auto_roles().select(lambda x: x.auto_role_id)
|
||||||
|
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
|
||||||
|
|
||||||
@auto_role.group()
|
@auto_role.group()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
async def rule(self, ctx: Context):
|
async def rule(self, ctx: Context):
|
||||||
@ -121,7 +170,11 @@ class AutoRoleGroup(DiscordCommandABC):
|
|||||||
self._logger.debug(__name__, f'Received command auto-role-rule list {ctx}')
|
self._logger.debug(__name__, f'Received command auto-role-rule list {ctx}')
|
||||||
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
|
||||||
return
|
return
|
||||||
self._client_utils.received_command(ctx.guild.id)
|
|
||||||
|
if not self._permissions.is_member_moderator(ctx.author):
|
||||||
|
await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message'))
|
||||||
|
self._logger.trace(__name__, f'Finished purge command')
|
||||||
|
return
|
||||||
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=self._t.transform('modules.auto_role.list.title'),
|
title=self._t.transform('modules.auto_role.list.title'),
|
||||||
|
@ -48,9 +48,9 @@ class BaseOnCommandEvent(OnCommandABC):
|
|||||||
self._clients = clients
|
self._clients = clients
|
||||||
self._servers = servers
|
self._servers = servers
|
||||||
|
|
||||||
def _append_received_message_count(self, g_id: int):
|
def _append_received_command_count(self, g_id: int):
|
||||||
try:
|
try:
|
||||||
self._clients.append_received_message_count(self._bot.user.id, g_id, 1)
|
self._clients.append_received_command_count(self._bot.user.id, g_id, 1)
|
||||||
self._db.save_changes()
|
self._db.save_changes()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e)
|
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}', e)
|
||||||
@ -87,4 +87,4 @@ class BaseOnCommandEvent(OnCommandABC):
|
|||||||
self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}')
|
self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}')
|
||||||
if ctx is None or ctx.guild is None:
|
if ctx is None or ctx.guild is None:
|
||||||
return
|
return
|
||||||
self._append_received_message_count(ctx.guild.id)
|
self._append_received_command_count(ctx.guild.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user