2024-06-27 11:35:22 -04:00
|
|
|
import discord
|
|
|
|
from discord import app_commands
|
|
|
|
import os
|
2024-06-30 17:38:48 -04:00
|
|
|
import subprocess
|
2024-06-30 17:55:41 -04:00
|
|
|
import logging
|
2024-06-30 17:38:48 -04:00
|
|
|
import sys
|
2024-06-27 11:35:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Update:
|
2024-06-30 17:38:48 -04:00
|
|
|
def __init__(self, bot):
|
2024-06-27 11:35:22 -04:00
|
|
|
self.bot = bot
|
|
|
|
self.logger = logging.getLogger('Update')
|
|
|
|
self.logger.setLevel(logging.DEBUG)
|
|
|
|
handler = logging.FileHandler(filename='log/selena.log', encoding='utf-8', mode='w')
|
|
|
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s'))
|
|
|
|
self.logger.addHandler(handler)
|
|
|
|
|
|
|
|
async def update_bot(self, interaction: discord.Interaction):
|
2024-06-30 17:55:41 -04:00
|
|
|
await interaction.response.defer(ephemeral=True)
|
|
|
|
await interaction.followup.send(embed=discord.Embed(description="Updating Selena...", color=discord.Color.green()))
|
|
|
|
self.logger.info('Starting update process...')
|
2024-06-30 17:38:48 -04:00
|
|
|
try:
|
2024-06-30 17:55:41 -04:00
|
|
|
subprocess.run(["git", "pull"], check=True)
|
|
|
|
self.logger.info('Successfully pulled updates from git.')
|
|
|
|
await interaction.followup.send(embed=discord.Embed(description="Update complete. Restarting...", color=discord.Color.green()))
|
2024-06-30 17:38:48 -04:00
|
|
|
os.execv(sys.executable, ['python'] + sys.argv)
|
2024-06-30 17:55:41 -04:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
self.logger.error(f'Error during update: {e}')
|
|
|
|
await interaction.followup.send(embed=discord.Embed(description=f"Update failed: {e}", color=discord.Color.red()))
|
2024-06-27 11:35:22 -04:00
|
|
|
|
|
|
|
def setup(self, tree: app_commands.CommandTree):
|
2024-06-30 17:55:41 -04:00
|
|
|
@tree.command(name="update", description="Update the bot from the repository")
|
2024-06-27 11:35:22 -04:00
|
|
|
async def update_command(interaction: discord.Interaction):
|
|
|
|
await self.update_bot(interaction)
|
|
|
|
|
|
|
|
if not tree.get_command("update"):
|
|
|
|
tree.add_command(update_command)
|
|
|
|
|
|
|
|
|
2024-06-30 17:38:48 -04:00
|
|
|
def setup(bot):
|
|
|
|
update = Update(bot)
|
|
|
|
update.setup(bot.tree)
|