From 183594efb5230678bc315796b490aab5359c1685 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 28 Jan 2025 09:22:02 -0500 Subject: [PATCH] Fixed the Update command so it can ping anyone be an indiviual or a team owner when there is an update. --- commands.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 29 ++++++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/commands.py b/commands.py index ce1b824..5e8d8a1 100644 --- a/commands.py +++ b/commands.py @@ -1,5 +1,8 @@ import discord from discord import app_commands +import subprocess +import os +import sys from audio import ( play_audio, stop_audio, @@ -81,4 +84,69 @@ async def setup_commands(client, guild_id=None): ) await interaction.response.send_message(embed=embed) + @client.tree.command(name="check_update", description="Check if Quartessa has updates.") + async def check_update(interaction: discord.Interaction): + """Check for updates and notify the token owner via DM.""" + owner = (await client.application_info()).owner + + try: + # Defer the interaction + await interaction.response.defer() + + # Run git fetch to check for updates without pulling + subprocess.run(["git", "fetch"], check=True) + local_commit = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() + remote_commit = subprocess.check_output(["git", "rev-parse", "@{u}"]).decode("utf-8").strip() + + if local_commit == remote_commit: + # No updates available + await interaction.followup.send("✅ **Quartessa is up to date.**") + else: + # Notify the token owner via DM + update_message = ( + "🔄 **Quartessa has updates available!**\n\n" + f"Local Commit: `{local_commit}`\n" + f"Remote Commit: `{remote_commit}`\n\n" + "To update Quartessa, you can:\n" + "1. Run the `/update` command.\n" + "2. Reinstall or restart the bot.\n" + "3. Pull the latest changes manually using Git." + ) + try: + await owner.send(update_message) + await interaction.followup.send("📩 **Update notification sent to the bot owner via DM.**") + except discord.Forbidden: + await interaction.followup.send( + "⚠️ **Quartessa has updates available, but I couldn't DM the owner.**\n" + f"Local Commit: `{local_commit}`\nRemote Commit: `{remote_commit}`" + ) + except Exception as e: + await interaction.followup.send(f"⚠️ **Error checking for updates:** {e}") + + @client.tree.command(name="update", description="Pull updates and restart Quartessa.") + async def update(interaction: discord.Interaction): + """Manually check for updates, apply them, and restart the bot.""" + owner = (await client.application_info()).owner + + try: + # Defer response + await interaction.response.defer() + + # Pull updates from the repository + result = subprocess.run(["git", "pull"], capture_output=True, text=True) + if "Already up to date." in result.stdout: + await interaction.followup.send("✅ **Quartessa is already up to date.**") + else: + # Notify the owner and restart the bot + commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() + update_message = ( + f"🔄 **Quartessa updated to the latest version:** `{commit_hash}`\n\n" + "Restarting to apply changes..." + ) + await owner.send(update_message) + await interaction.followup.send("🔄 **Updates applied. Restarting Quartessa...**") + os.execv(sys.executable, ['python'] + sys.argv) + except Exception as e: + await interaction.followup.send(f"⚠️ **Error while updating Quartessa:** {e}") + await client.tree.sync() diff --git a/main.py b/main.py index 90741bb..a968e25 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ import os import logging import discord +import subprocess from dotenv import load_dotenv from commands import setup_commands @@ -9,6 +10,7 @@ load_dotenv() TOKEN = os.getenv("DISCORD_TOKEN") GUILD_ID = os.getenv("GUILD_ID") +DEFAULT_OWNER_ID = os.getenv("DEFAULT_OWNER_ID") # Validate Guild ID if GUILD_ID: @@ -31,7 +33,30 @@ class AmberClient(discord.Client): self.tree = discord.app_commands.CommandTree(self) async def on_ready(self): - logging.info(f"Amber is online as {self.user}") + try: + app_info = await client.application_info() + if hasattr(app_info, "team") and app_info.team: # Check if the bot is part of a team + team_owner_id = app_info.team.owner_id + owner = await client.fetch_user(team_owner_id) + print(f"✅ Team owner fetched successfully: {owner} ({team_owner_id})") + else: + owner = app_info.owner or await client.fetch_user(DEFAULT_OWNER_ID) + print(f"✅ Individual owner fetched successfully: {owner} ({owner.id})") + + # Send startup notification + try: + commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip() + startup_message = ( + f"🚀 **Quartessa is online!**\n" + f"Version: `{commit_hash}`\n\n" + "Use `/check_update` to check for updates or `/update` to apply updates." + ) + await owner.send(startup_message) + except discord.Forbidden: + print("⚠️ Unable to send startup notification to the owner. Check privacy settings.") + + except Exception as e: + print(f"⚠️ Error fetching owner information: {e}") # Sync commands after the bot is fully ready if GUILD_ID: @@ -44,6 +69,8 @@ class AmberClient(discord.Client): except Exception as e: logging.error(f"Failed to setup commands: {e}") + print(f"✅ Quartessa is online as {client.user}. Done.") + logging.basicConfig(level=logging.INFO) client = AmberClient()