From d15fd53c867a5fd04de75101448b305c3045b367 Mon Sep 17 00:00:00 2001 From: ceb515 Date: Mon, 25 Mar 2024 14:23:03 -0400 Subject: [PATCH] Update pagebot.py --- pagebot.py | 62 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/pagebot.py b/pagebot.py index 5fcc557..d0473a6 100644 --- a/pagebot.py +++ b/pagebot.py @@ -1,5 +1,5 @@ -#!/usr/bin/python3 import discord +from discord.ext import commands import os import subprocess import time @@ -9,6 +9,9 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import speech_recognition as sr +## Initialize the bot with command prefix +bot = commands.Bot(command_prefix='!') + ## New File Handler class MyHandler(FileSystemEventHandler): def on_created(self, event): @@ -29,7 +32,7 @@ class MyHandler(FileSystemEventHandler): print("Converting to MP4") mp4_file = convert_to_mp4(filepath) print("Sending to Discord") - client.loop.create_task(upload_to_discord(mp4_file,text)) + bot.loop.create_task(upload_to_discord(mp4_file,text)) ## Convert MP3 to MP4 def convert_to_mp4(mp3_file): @@ -62,8 +65,6 @@ def convert_to_text(mp3_path,mp3_name): print(f"Error during conversion: {e}") return "" - - ## upload to discord async def upload_to_discord(mp4_file,text): ## Check to make sure conversion worked. @@ -71,7 +72,7 @@ async def upload_to_discord(mp4_file,text): print("Conversion failed. Skipping upload.") return ## Get channel to send in - channel = client.get_channel(secrets_file.channel_id) + channel = bot.get_channel(secrets_file.channel_id) if channel: filename = os.path.basename(mp4_file) ## Send Video with name @@ -92,39 +93,52 @@ async def upload_to_discord(mp4_file,text): else: print(f"Could not find channel with ID {channel}") -def launch_and_watch(program_path): +## Command to restart TTD software manually +@bot.command() +async def restart_ttd(ctx): + if secrets_file.ttd_path: + await ctx.send("Restarting TTD software...") + restart_ttd_process(secrets_file.ttd_path) + else: + await ctx.send("TTD path not configured.") + +## Function to restart TTD software +def restart_ttd_process(program_path): program_directory = os.path.dirname(program_path) + process = subprocess.Popen(program_path, cwd=program_directory) + process.wait() + if process.returncode != 0: + print("TTD has crashed. Relaunching...") + else: + print("TTD has exited normally.") + +## Launch and watch TTD +def launch_and_watch_ttd(): while True: - process = subprocess.Popen(program_path,cwd=program_directory) + process = subprocess.Popen(secrets_file.ttd_path) process.wait() if process.returncode != 0: print("TTD has crashed. Relaunching...") else: print("TTD has exited normally.") break - # Wait for a few seconds before relaunching time.sleep(2) if __name__ == "__main__": - - ## initialize watchdogs + ## Initialize watchdogs event_handler = MyHandler() observer = Observer() observer.schedule(event_handler, secrets_file.watch_folder, recursive=False) observer.start() - ## Launch TTD - if (secrets_file.ttd_path != ""): - watchdog_thread = threading.Thread(target=launch_and_watch, args=(secrets_file.ttd_path,)) - watchdog_thread.start() + ## Start watching TTD + if secrets_file.ttd_path: + ttd_thread = threading.Thread(target=launch_and_watch_ttd) + ttd_thread.start() - ## initialize discord - intents = discord.Intents.default() - intents.message_content = True - client = discord.Client(intents=intents) - client.run(secrets_file.key) - + ## Initialize Discord bot + bot.run(secrets_file.key) try: while True: @@ -133,9 +147,7 @@ if __name__ == "__main__": observer.stop() observer.join() -##discord stuffs -@client.event +## Discord event +@bot.event async def on_ready(): - print(f'We have logged in as {client.user}') - - + print(f'We have logged in as {bot.user}')