Added auto-role add command #54
This commit is contained in:
		@@ -60,6 +60,13 @@
 | 
			
		||||
        "auto_role_id": "auto-role Id",
 | 
			
		||||
        "message_id": "Nachricht-Id"
 | 
			
		||||
      },
 | 
			
		||||
      "add": {
 | 
			
		||||
        "success": "auto-role für die Nachricht {} wurde hinzugefügt :D",
 | 
			
		||||
        "error": {
 | 
			
		||||
          "not_found": "auto-role für die Nachricht {} nicht gefunden!",
 | 
			
		||||
          "already_exists": "auto-role für die Nachricht {} existiert bereits!"
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      "rule": {
 | 
			
		||||
        "list": {
 | 
			
		||||
          "title": "auto-role Regeln:",
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,11 @@
 | 
			
		||||
from typing import List
 | 
			
		||||
from typing import List as TList, Optional
 | 
			
		||||
 | 
			
		||||
import discord
 | 
			
		||||
from cpl_core.database.context import DatabaseContextABC
 | 
			
		||||
from cpl_discord.command import DiscordCommandABC
 | 
			
		||||
from cpl_discord.container import TextChannel
 | 
			
		||||
from cpl_discord.service import DiscordBotServiceABC
 | 
			
		||||
from cpl_query.extension import List
 | 
			
		||||
from cpl_translation import TranslatePipe
 | 
			
		||||
from discord import app_commands
 | 
			
		||||
from discord.ext import commands
 | 
			
		||||
@@ -13,6 +16,7 @@ from bot_core.abc.message_service_abc import MessageServiceABC
 | 
			
		||||
from bot_core.logging.command_logger import CommandLogger
 | 
			
		||||
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
 | 
			
		||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
 | 
			
		||||
from bot_data.model.auto_role import AutoRole
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AutoRoleGroup(DiscordCommandABC):
 | 
			
		||||
@@ -25,7 +29,8 @@ class AutoRoleGroup(DiscordCommandABC):
 | 
			
		||||
            client_utils: ClientUtilsServiceABC,
 | 
			
		||||
            translate: TranslatePipe,
 | 
			
		||||
            servers: ServerRepositoryABC,
 | 
			
		||||
            auto_roles: AutoRoleRepositoryABC
 | 
			
		||||
            auto_roles: AutoRoleRepositoryABC,
 | 
			
		||||
            db_context: DatabaseContextABC
 | 
			
		||||
    ):
 | 
			
		||||
        DiscordCommandABC.__init__(self)
 | 
			
		||||
 | 
			
		||||
@@ -36,12 +41,14 @@ class AutoRoleGroup(DiscordCommandABC):
 | 
			
		||||
        self._t = translate
 | 
			
		||||
        self._servers = servers
 | 
			
		||||
        self._auto_roles = auto_roles
 | 
			
		||||
        self._db_context = db_context
 | 
			
		||||
 | 
			
		||||
        self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
 | 
			
		||||
 | 
			
		||||
    @commands.hybrid_group(name="auto-role")
 | 
			
		||||
    @commands.guild_only()
 | 
			
		||||
    async def auto_role(self, ctx: Context): pass
 | 
			
		||||
    async def auto_role(self, ctx: Context):
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @auto_role.command(alias='auto-roles')
 | 
			
		||||
    @commands.guild_only()
 | 
			
		||||
@@ -72,7 +79,36 @@ class AutoRoleGroup(DiscordCommandABC):
 | 
			
		||||
        embed.add_field(name=self._t.transform('modules.auto_role.list.message_id'), value=message_id, inline=True)
 | 
			
		||||
        await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
 | 
			
		||||
        self._logger.trace(__name__, f'Finished command auto-role list')
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    @auto_role.command()
 | 
			
		||||
    @commands.guild_only()
 | 
			
		||||
    async def add(self, ctx: Context, message_id: str):
 | 
			
		||||
        self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}')
 | 
			
		||||
        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:
 | 
			
		||||
            self._logger.debug(__name__, f'Message with id  {message_id} not found in  {ctx.channel.name}')
 | 
			
		||||
            await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id))
 | 
			
		||||
            self._logger.trace(__name__, f'Finished command auto-role add')
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if self._auto_roles.find_auto_roles_by_message_id(int(message_id)) is not None:
 | 
			
		||||
            self._logger.debug(__name__, f'AutoRole for message {message_id} already exists')
 | 
			
		||||
            await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.already_exists').format(message_id))
 | 
			
		||||
            self._logger.trace(__name__, f'Finished command auto-role add')
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id
 | 
			
		||||
        self._auto_roles.add_auto_role(AutoRole(server_id, int(message_id)))
 | 
			
		||||
        self._db_context.save_changes()
 | 
			
		||||
        self._logger.info(__name__, f'Saved AutoRole for message {message_id} at server {server_id}')
 | 
			
		||||
        await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.success').format(message_id))
 | 
			
		||||
        self._logger.trace(__name__, f'Finished command auto-role add')
 | 
			
		||||
 | 
			
		||||
    @add.autocomplete('message_id')
 | 
			
		||||
    async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
 | 
			
		||||
        channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id)
 | 
			
		||||
        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)]
 | 
			
		||||
 | 
			
		||||
    @auto_role.group()
 | 
			
		||||
    @commands.guild_only()
 | 
			
		||||
@@ -113,6 +149,6 @@ class AutoRoleGroup(DiscordCommandABC):
 | 
			
		||||
        self._logger.trace(__name__, f'Finished command auto-role-rule list')
 | 
			
		||||
 | 
			
		||||
    @list.autocomplete('auto_role')
 | 
			
		||||
    async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
 | 
			
		||||
    async def list_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=key, value=key) for key in auto_roles]
 | 
			
		||||
        return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user