Fixed autocompletes #150

This commit is contained in:
Sven Heidemann 2022-12-30 12:18:56 +01:00
parent 476db0ed33
commit 7646335d03
4 changed files with 46 additions and 24 deletions

View File

@ -1,5 +1,7 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable
from cpl_query.extension import List
from discord.ext.commands import Context from discord.ext.commands import Context
@ -25,3 +27,6 @@ class ClientUtilsServiceABC(ABC):
@abstractmethod @abstractmethod
async def presence_game(self, t_key: str): pass async def presence_game(self, t_key: str): pass
@abstractmethod
def get_auto_complete_list(self, _l: List, current: str, select: Callable = None) -> List: pass

View File

@ -1,10 +1,14 @@
from typing import Callable
import discord import discord
from cpl_core.configuration import ConfigurationABC 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_core.logging import LoggerABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from cpl_translation import TranslatePipe from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext.commands import Context 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
@ -81,3 +85,12 @@ class ClientUtilsService(ClientUtilsServiceABC):
name = self._t.transform(t_key).format(bot.__version__) name = self._t.transform(t_key).format(bot.__version__)
await self._bot.change_presence(activity=discord.Game(name=name)) await self._bot.change_presence(activity=discord.Game(name=name))
self._logger.info(__name__, f'Set presence {name}') self._logger.info(__name__, f'Set presence {name}')
def get_auto_complete_list(self, _l: List, current: str, select: Callable = None) -> List:
if current != '':
if select is None:
select = lambda x: x
_l = _l.skip(_l.select(select).index_of(current))
return _l.take(25)

View File

@ -1,4 +1,4 @@
from typing import List as TList, Optional from typing import List as TList, Optional, Any
import discord import discord
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
@ -50,6 +50,11 @@ class AutoRoleGroup(DiscordCommandABC):
self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}')
async def _auto_role_auto_complete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id)
auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in self._client_utils.get_auto_complete_list(auto_roles, current)]
@commands.hybrid_group(name="auto-role") @commands.hybrid_group(name="auto-role")
@commands.guild_only() @commands.guild_only()
async def auto_role(self, ctx: Context): async def auto_role(self, ctx: Context):
@ -155,9 +160,7 @@ class AutoRoleGroup(DiscordCommandABC):
@remove.autocomplete('auto_role') @remove.autocomplete('auto_role')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._auto_role_auto_complete(interaction, current)
auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).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()
@ -198,9 +201,7 @@ class AutoRoleGroup(DiscordCommandABC):
@list.autocomplete('auto_role') @list.autocomplete('auto_role')
async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def list_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._auto_role_auto_complete(interaction, current)
auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
@rule.command() @rule.command()
@commands.guild_only() @commands.guild_only()
@ -255,19 +256,17 @@ class AutoRoleGroup(DiscordCommandABC):
@add.autocomplete('auto_role') @add.autocomplete('auto_role')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._auto_role_auto_complete(interaction, current)
auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id)
return [app_commands.Choice(name=auto_role, value=auto_role) for auto_role in auto_roles]
@add.autocomplete('emoji_name') @add.autocomplete('emoji_name')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
emojis = interaction.guild.emojis emojis = List(discord.Emoji, interaction.guild.emojis)
return [app_commands.Choice(name=emoji.name, value=emoji.name) for emoji in emojis] return [app_commands.Choice(name=emoji.name, value=emoji.name) for emoji in self._client_utils.get_auto_complete_list(emojis, current, lambda e: e.name)]
@add.autocomplete('role_id') @add.autocomplete('role_id')
async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def rule_add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
roles = interaction.guild.roles roles = List(discord.Role, interaction.guild.roles)
return [app_commands.Choice(name=role.name, value=str(role.id)) for role in roles] return [app_commands.Choice(name=role.name, value=str(role.id)) for role in self._client_utils.get_auto_complete_list(roles, current, lambda r: r.name)]
@rule.command() @rule.command()
@commands.guild_only() @commands.guild_only()
@ -294,4 +293,10 @@ class AutoRoleGroup(DiscordCommandABC):
server = self._servers.get_server_by_discord_id(interaction.guild.id) server = self._servers.get_server_by_discord_id(interaction.guild.id)
auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id) auto_roles = self._auto_roles.get_auto_roles_by_server_id(server.server_id).select(lambda x: x.auto_role_id)
rules = auto_roles.select_many(lambda ar: self._auto_roles.get_auto_role_rules_by_auto_role_id(ar)) rules = auto_roles.select_many(lambda ar: self._auto_roles.get_auto_role_rules_by_auto_role_id(ar))
return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in rules] return [
app_commands.Choice(
name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}',
value=rule.auto_role_rule_id
)
for rule in self._client_utils.get_auto_complete_list(rules, current, lambda r: r.auto_role_rule_id)
]

View File

@ -93,6 +93,11 @@ class LevelGroup(DiscordCommandABC):
self._logger.error(__name__, f'Level seeding failed', e) self._logger.error(__name__, f'Level seeding failed', e)
await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_failed')) await self._message_service.send_channel_message(channel, self._t.transform('modules.level.seeding_failed'))
async def _level_auto_complete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id)
levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name)
return [app_commands.Choice(name=level, value=level) for level in self._client_utils.get_auto_complete_list(levels, current)]
@commands.hybrid_group() @commands.hybrid_group()
@commands.guild_only() @commands.guild_only()
async def level(self, ctx: Context): async def level(self, ctx: Context):
@ -240,9 +245,7 @@ class LevelGroup(DiscordCommandABC):
@edit.autocomplete('level') @edit.autocomplete('level')
async def edit_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def edit_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._level_auto_complete(interaction, current)
levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name)
return [app_commands.Choice(name=level, value=level) for level in levels]
@edit.autocomplete('color') @edit.autocomplete('color')
async def edit_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def edit_color_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
@ -283,9 +286,7 @@ class LevelGroup(DiscordCommandABC):
@remove.autocomplete('level') @remove.autocomplete('level')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._level_auto_complete(interaction, current)
levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name)
return [app_commands.Choice(name=level, value=level) for level in levels]
@level.command() @level.command()
@commands.guild_only() @commands.guild_only()
@ -392,6 +393,4 @@ class LevelGroup(DiscordCommandABC):
@set.autocomplete('level') @set.autocomplete('level')
async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id) return await self._level_auto_complete(interaction, current)
levels = self._levels.get_levels_by_server_id(server.server_id).select(lambda l: l.name)
return [app_commands.Choice(name=level, value=level) for level in levels]