move update command to a seperate cog cause im fucking stupid

This commit is contained in:
Slipstream 2025-05-08 00:09:00 -06:00
parent 170b87dc51
commit 659bdf5f94
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD
2 changed files with 46 additions and 30 deletions

View File

@ -5,9 +5,7 @@ import psutil
import discord
from discord.ext import commands
from discord import app_commands
import shutil
import subprocess
import sys
import asyncio
import logging
import time
@ -326,33 +324,6 @@ class Core(commands.Cog):
embed.set_footer(text="Thank you for using the bot!")
await interaction.response.send_message(embed=embed)
@app_commands.command(name="update", description="Updates the bot code from GitLab and restarts the bot. (Admin Only)")
async def update(self, interaction: discord.Interaction):
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You do not have permission to run this command.", ephemeral=True)
return
await interaction.response.send_message("Initiating update. The bot will restart shortly...")
target_dir = "/home/server/wdiscordbotserver/"
repo_url = "https://gitlab.com/pancakes1234/wdiscordbotserver.git"
restart_script = "/home/server/wdiscordbotserver/bot.py"
try:
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
await interaction.followup.send(f"Removed directory: {target_dir}")
else:
await interaction.followup.send(f"Directory {target_dir} does not exist; proceeding with clone...")
subprocess.run(["git", "clone", repo_url, target_dir], check=True)
await interaction.followup.send("Repository cloned successfully.")
except Exception as e:
error_msg = f"Update failed: {e}"
print(error_msg)
await interaction.followup.send(error_msg)
return
os.execv(sys.executable, [sys.executable, restart_script])
await interaction.response.send_message("Bot has updated to the latest commit and is restarting...")
@app_commands.command(name="temps", description="Runs the 'sensors' command and sends its output to chat.")
async def temps(self, interaction: discord.Interaction):
"""Executes the sensors command and returns the output."""
@ -398,4 +369,4 @@ class Core(commands.Cog):
async def setup(bot: commands.Bot):
await bot.add_cog(Core(bot))
await bot.add_cog(Core(bot))

45
cogs/update.py Normal file
View File

@ -0,0 +1,45 @@
import os
import shutil
import subprocess
import sys
import discord
from discord.ext import commands
from discord import app_commands
class UpdateCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@app_commands.command(name="update", description="Updates the bot code from GitLab and restarts the bot. (Admin Only)")
async def update(self, interaction: discord.Interaction):
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You do not have permission to run this command.", ephemeral=True)
return
await interaction.response.send_message("Initiating update. The bot will restart shortly...")
target_dir = "/home/server/wdiscordbotserver/"
repo_url = "https://gitlab.com/pancakes1234/wdiscordbotserver.git"
restart_script = "/home/server/wdiscordbotserver/bot.py"
try:
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
await interaction.followup.send(f"Removed directory: {target_dir}")
else:
await interaction.followup.send(f"Directory {target_dir} does not exist; proceeding with clone...")
subprocess.run(["git", "clone", repo_url, target_dir], check=True)
await interaction.followup.send("Repository cloned successfully.")
except Exception as e:
error_msg = f"Update failed: {e}"
print(error_msg)
await interaction.followup.send(error_msg)
return
try:
await interaction.followup.send("Bot has updated to the latest commit and is restarting...")
os.execv(sys.executable, [sys.executable, restart_script])
# If os.execv returns, it means it failed
except Exception as e:
await interaction.followup.send(f"Failed to restart bot: {e}")
async def setup(bot: commands.Bot):
await bot.add_cog(UpdateCog(bot))