Added GameServer handling & commands #238

This commit is contained in:
2023-03-04 20:24:49 +01:00
parent a8dad6b223
commit 91b2cf7546
12 changed files with 339 additions and 36 deletions

View File

@@ -8,6 +8,7 @@ from bot_core.abc.module_abc import ModuleABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from modules.base.abc.base_helper_abc import BaseHelperABC
from modules.base.command.afk_command import AFKCommand
from modules.base.command.game_server_group import GameServerGroup
from modules.base.command.help_command import HelpCommand
from modules.base.command.info_command import InfoCommand
from modules.base.command.mass_move_command import MassMoveCommand
@@ -66,6 +67,7 @@ class BaseModule(ModuleABC):
self._dc.add_command(UserGroup)
self._dc.add_command(RegisterGroup)
self._dc.add_command(UnregisterGroup)
self._dc.add_command(GameServerGroup)
# events
self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent)
self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent)

View File

@@ -0,0 +1,151 @@
from typing import List as TList
import discord
from cpl_core.database.context import DatabaseContextABC
from cpl_discord.command import DiscordCommandABC
from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.helper.command_checks import CommandChecks
from bot_core.logging.command_logger import CommandLogger
from bot_data.abc.api_key_repository_abc import ApiKeyRepositoryABC
from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.game_server import GameServer
from modules.permission.abc.permission_service_abc import PermissionServiceABC
class GameServerGroup(DiscordCommandABC):
def __init__(
self,
logger: CommandLogger,
message_service: MessageServiceABC,
bot: DiscordBotServiceABC,
client_utils: ClientUtilsABC,
translate: TranslatePipe,
servers: ServerRepositoryABC,
game_servers: GameServerRepositoryABC,
api_keys: ApiKeyRepositoryABC,
db: DatabaseContextABC,
permission_service: PermissionServiceABC,
):
DiscordCommandABC.__init__(self)
self._logger = logger
self._message_service = message_service
self._bot = bot
self._client_utils = client_utils
self._t = translate
self._servers = servers
self._game_servers = game_servers
self._api_keys = api_keys
self._db = db
self._permissions = permission_service
self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}")
@commands.hybrid_group(name="game-server")
@commands.guild_only()
async def game_server(self, ctx: Context):
pass
@game_server.command(alias="game-servers")
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def list(self, ctx: Context, wait: int = None):
self._logger.debug(__name__, f"Received command game_server list {ctx}")
if ctx.guild is None:
return
server = self._servers.get_server_by_discord_id(ctx.guild.id)
game_servers = self._game_servers.get_game_servers_by_server_id(server.id)
if game_servers.count() < 1:
await self._message_service.send_ctx_msg(
ctx, self._t.transform("modules.base.game_server.error.nothing_found")
)
self._logger.trace(__name__, f"Finished command game_server list")
return
game_server_name = ""
api_key = ""
for game_server in game_servers:
game_server: GameServer = game_server
game_server_name += f"\n{game_server.name}"
api_key += f"\n{game_server.api_key.identifier}"
embed = discord.Embed(
title=self._t.transform("modules.base.game_server.list.title"),
description=self._t.transform("modules.base.game_server.list.description"),
color=int("ef9d0d", 16),
)
embed.add_field(
name=self._t.transform("modules.base.game_server.list.name"),
value=game_server_name,
inline=True,
)
embed.add_field(name=self._t.transform("modules.base.game_server.list.api_key"), value=api_key, inline=True)
await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait)
self._logger.trace(__name__, f"Finished command game_server list")
@game_server.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_admin()
async def add(self, ctx: Context, name: str, api_key_id: int):
self._logger.debug(__name__, f"Received command game-server add {ctx}: {name} {api_key_id}")
server = self._servers.get_server_by_discord_id(ctx.guild.id)
api_key = self._api_keys.get_api_key_by_id(api_key_id)
game_server = GameServer(name, server, api_key)
self._game_servers.add_game_server(game_server)
self._db.save_changes()
await self._message_service.send_ctx_msg(
ctx,
self._t.transform("modules.base.game_server.add.success").format(name),
)
self._logger.trace(__name__, f"Finished command game-server add")
@add.autocomplete("api_key_id")
async def api_key_id_autocomplete(
self, interaction: discord.Interaction, current: str
) -> TList[app_commands.Choice[str]]:
keys = self._api_keys.get_api_keys()
return [
app_commands.Choice(name=f"{key.identifier}: {key.key}", value=key.id)
for key in self._client_utils.get_auto_complete_list(keys, current, lambda x: x.key)
]
@game_server.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_admin()
async def remove(self, ctx: Context, id: int):
self._logger.debug(__name__, f"Received command game-server remove {ctx}: {id}")
game_server = self._game_servers.get_game_server_by_id(id)
self._game_servers.delete_game_server(game_server)
self._db.save_changes()
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.game_server.remove.success"))
self._logger.trace(__name__, f"Finished command game-server remove")
@remove.autocomplete("id")
async def id_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
server = self._servers.get_server_by_discord_id(interaction.guild.id)
game_servers = self._game_servers.get_game_servers_by_server_id(server.id)
return [
app_commands.Choice(name=gs.name, value=gs.id)
for gs in self._client_utils.get_auto_complete_list(game_servers, current, lambda x: x.name)
]