FIX: Fixed an issue with the system update

This commit is contained in:
Dan 2024-06-30 17:38:48 -04:00
parent 8c58585af1
commit fce87e7924

View File

@ -1,15 +1,14 @@
# modules/admin/update.py
import discord import discord
from discord import app_commands from discord import app_commands
import os import os
import subprocess
import logging import logging
import subprocess
import sys
class Update: class Update:
def __init__(self, bot, branch='dev-rework'): def __init__(self, bot):
self.bot = bot self.bot = bot
self.branch = branch
self.logger = logging.getLogger('Update') self.logger = logging.getLogger('Update')
self.logger.setLevel(logging.DEBUG) self.logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w') handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w')
@ -17,31 +16,41 @@ class Update:
self.logger.addHandler(handler) self.logger.addHandler(handler)
async def update_bot(self, interaction: discord.Interaction): async def update_bot(self, interaction: discord.Interaction):
await interaction.response.send_message(embed=discord.Embed(description="Updating Selena...", color=discord.Color.green())) try:
await interaction.response.send_message(embed=discord.Embed(description="Updating Selena...", color=discord.Color.green()))
except discord.errors.InteractionResponded:
await interaction.followup.send(embed=discord.Embed(description="Updating Selena...", color=discord.Color.green()))
self.logger.info(f"Pulling latest code from Git repository on branch {self.branch}...") # Fetch updates from the specified branch
subprocess.run(["git", "fetch"], check=True) branch = 'main' # change this to your branch if necessary
subprocess.run(["git", "checkout", self.branch], check=True) result = subprocess.run(['git', 'pull', 'origin', branch], capture_output=True, text=True)
subprocess.run(["git", "pull", "origin", self.branch], check=True)
self.logger.info("Installing dependencies...") if result.returncode == 0:
subprocess.run(["pip", "install", "-r", "requirements.txt"], check=True) self.logger.info("Successfully pulled updates from the repository")
self.logger.info(result.stdout)
try:
await interaction.followup.send(embed=discord.Embed(description="Successfully updated Selena. Restarting...", color=discord.Color.green()))
except discord.errors.InteractionResponded:
await interaction.followup.send(embed=discord.Embed(description="Successfully updated Selena. Restarting...", color=discord.Color.green()))
await interaction.followup.send(embed=discord.Embed(description="Update completed. Restarting Selena...", color=discord.Color.green())) # Restart the bot (this is just a placeholder, modify according to your setup)
self.logger.info("Update completed. Restarting Selena...") os.execv(sys.executable, ['python'] + sys.argv)
else:
os._exit(0) self.logger.error(f"Failed to pull updates: {result.stderr}")
try:
await interaction.followup.send(embed=discord.Embed(description="Failed to update Selena. Check logs for details.", color=discord.Color.red()))
except discord.errors.InteractionResponded:
await interaction.followup.send(embed=discord.Embed(description="Failed to update Selena. Check logs for details.", color=discord.Color.red()))
def setup(self, tree: app_commands.CommandTree): def setup(self, tree: app_commands.CommandTree):
@tree.command(name="update", description="Update Selena to the latest version") @tree.command(name="update", description="Update the bot to the latest version")
async def update_command(interaction: discord.Interaction): async def update_command(interaction: discord.Interaction):
await interaction.response.defer() # Defer the interaction response
await self.update_bot(interaction) await self.update_bot(interaction)
if not tree.get_command("update"): if not tree.get_command("update"):
tree.add_command(update_command) tree.add_command(update_command)
def setup(bot, branch='dev-rework'): def setup(bot):
updater = Update(bot, branch=branch) update = Update(bot)
updater.setup(bot.tree) update.setup(bot.tree)