import discord from discord import app_commands from discord.ext import commands from dom5game import Dom5game class SlashCommands(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot def game_autocomplete(self, current): options = [] for game in self.bot.tracked_games: options.append(game.name) return [ app_commands.Choice(name=option, value=option) for option in options if option.lower().startswith(current.lower()) ][:25] @app_commands.command(name="ping", description="Check bot latency") async def ping(self, interaction: discord.Interaction): await interaction.response.send_message( f"Pong! {round(self.bot.latency * 1000)}ms" ) @app_commands.command( name="dom5-addgame", description="Adds a game that is already running but not tracked by the bot yet.", ) async def addgame(self, interaction: discord.Interaction, name: str): try: if any(game.name == name for game in self.bot.tracked_games): await interaction.response.send_message("Game already tracked.") return game = Dom5game(name, interaction.channel_id, [], 0, {}) game.update_turn() game.update_players() game.to_json() self.bot.tracked_games.append(game) await interaction.response.send_message("Added the game.") except: await interaction.response.send_message( "Something went wrong. Are you sure the name is correct and the game exists?" ) @app_commands.command(name="dom5-creategame", description="Creates a new game.") async def creategame(self, interaction: discord.Interaction, name: str, port: int): print("a") @app_commands.command( name="dom5-pingme", description="Signs you up to be pinged for a game. Run the command again to not get pinged anymore.", ) async def pingme(self, interaction: discord.Interaction, name: str): game = Dom5game.get_game_by_name(name, self.bot.tracked_games) if game == None: await interaction.response.send_message("Game does not exist.") return try: if interaction.user.id in game.members: game.members.remove(interaction.user.id) await interaction.response.send_message( 'You will no longer receive turn notifications for game: **"' + name + '"**.' ) else: game.members.append(interaction.user.id) await interaction.response.send_message( 'You will now receive turn notifications for game: **"' + name + '"**.' ) except: await interaction.response.send_message("Something went wrong.") @pingme.autocomplete("name") async def pingme_autocomplete(self, interaction: discord.Interaction, current: str): return self.game_autocomplete(current) @app_commands.command( name="dom5-details", description="Shows the details of an ongoing game." ) async def details(self, interaction: discord.Interaction, name: str): game = Dom5game.get_game_by_name(name, self.bot.tracked_games) if game == None: await interaction.response.send_message("Game does not exist.") return embed = discord.Embed(title=name) for player in game.players.keys(): if game.players[player] not in {"AI", "Eliminated"}: embed.add_field(name=player, value=game.players[player]) await interaction.response.send_message(embed=embed) @details.autocomplete("name") async def details_autocomplete( self, interaction: discord.Interaction, current: str ): return self.game_autocomplete(current) async def setup(bot: commands.Bot): await bot.add_cog(SlashCommands(bot))