Added gmod support #277

This commit is contained in:
2023-12-11 21:34:38 +01:00
parent ba173a6743
commit 4e80e3ccb7
2 changed files with 57 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ from cpl_translation import TranslatePipe
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
from valve.steam.id import SteamID
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
@@ -50,6 +51,17 @@ class RegisterGroup(DiscordCommandABC):
self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}")
async def _game_server_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_server.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)
]
@commands.hybrid_group()
@commands.guild_only()
async def register(self, ctx: Context):
@@ -101,10 +113,44 @@ class RegisterGroup(DiscordCommandABC):
async def game_server_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_server.get_game_servers_by_server_id(server.id)
return await self._game_server_autocomplete(interaction, current)
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)
]
@register.command()
@commands.guild_only()
@CommandChecks.check_is_ready()
@CommandChecks.check_is_member_moderator()
async def gmod(self, ctx: Context, member: discord.Member, game_server: int, steam_url: str):
self._logger.debug(__name__, f"Received command register gmod {ctx}")
steam_id = None
try:
self._logger.debug(__name__, f"Try to get steam id for {id}")
steam_id = SteamID.from_community_url(steam_url)
except Exception as e:
self._logger.error(__name__, f"Get steam id for {steam_id} failed", e)
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.register.not_found")
)
if steam_id is None:
return
server = self._servers.get_server_by_discord_id(ctx.guild.id)
user = self._users.get_user_by_discord_id_and_server_id(member.id, server.id)
gs = self._game_server.get_game_server_by_id(game_server)
game_ident = UserGameIdent(user, gs, steam_id)
self._user_game_ident.add_user_game_ident(game_ident)
self._db.save_changes()
await self._message_service.send_interaction_msg(
ctx.interaction, self._t.transform("modules.base.register.success")
)
self._logger.trace(__name__, f"Finished register gmod command")
@gmod.autocomplete("game_server")
async def game_server_autocomplete(
self, interaction: discord.Interaction, current: str
) -> TList[app_commands.Choice[str]]:
return await self._game_server_autocomplete(interaction, current)