Update pagebot.py

This commit is contained in:
ceb515 2024-03-25 14:23:03 -04:00
parent addc49269d
commit d15fd53c86

View File

@ -1,5 +1,5 @@
#!/usr/bin/python3
import discord import discord
from discord.ext import commands
import os import os
import subprocess import subprocess
import time import time
@ -9,6 +9,9 @@ from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler from watchdog.events import FileSystemEventHandler
import speech_recognition as sr import speech_recognition as sr
## Initialize the bot with command prefix
bot = commands.Bot(command_prefix='!')
## New File Handler ## New File Handler
class MyHandler(FileSystemEventHandler): class MyHandler(FileSystemEventHandler):
def on_created(self, event): def on_created(self, event):
@ -29,7 +32,7 @@ class MyHandler(FileSystemEventHandler):
print("Converting to MP4") print("Converting to MP4")
mp4_file = convert_to_mp4(filepath) mp4_file = convert_to_mp4(filepath)
print("Sending to Discord") 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 ## Convert MP3 to MP4
def convert_to_mp4(mp3_file): def convert_to_mp4(mp3_file):
@ -62,8 +65,6 @@ def convert_to_text(mp3_path,mp3_name):
print(f"Error during conversion: {e}") print(f"Error during conversion: {e}")
return "" return ""
## upload to discord ## upload to discord
async def upload_to_discord(mp4_file,text): async def upload_to_discord(mp4_file,text):
## Check to make sure conversion worked. ## Check to make sure conversion worked.
@ -71,7 +72,7 @@ async def upload_to_discord(mp4_file,text):
print("Conversion failed. Skipping upload.") print("Conversion failed. Skipping upload.")
return return
## Get channel to send in ## Get channel to send in
channel = client.get_channel(secrets_file.channel_id) channel = bot.get_channel(secrets_file.channel_id)
if channel: if channel:
filename = os.path.basename(mp4_file) filename = os.path.basename(mp4_file)
## Send Video with name ## Send Video with name
@ -92,39 +93,52 @@ async def upload_to_discord(mp4_file,text):
else: else:
print(f"Could not find channel with ID {channel}") 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) 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: while True:
process = subprocess.Popen(program_path,cwd=program_directory) process = subprocess.Popen(secrets_file.ttd_path)
process.wait() process.wait()
if process.returncode != 0: if process.returncode != 0:
print("TTD has crashed. Relaunching...") print("TTD has crashed. Relaunching...")
else: else:
print("TTD has exited normally.") print("TTD has exited normally.")
break break
# Wait for a few seconds before relaunching # Wait for a few seconds before relaunching
time.sleep(2) time.sleep(2)
if __name__ == "__main__": if __name__ == "__main__":
## Initialize watchdogs
## initialize watchdogs
event_handler = MyHandler() event_handler = MyHandler()
observer = Observer() observer = Observer()
observer.schedule(event_handler, secrets_file.watch_folder, recursive=False) observer.schedule(event_handler, secrets_file.watch_folder, recursive=False)
observer.start() observer.start()
## Launch TTD ## Start watching TTD
if (secrets_file.ttd_path != ""): if secrets_file.ttd_path:
watchdog_thread = threading.Thread(target=launch_and_watch, args=(secrets_file.ttd_path,)) ttd_thread = threading.Thread(target=launch_and_watch_ttd)
watchdog_thread.start() ttd_thread.start()
## initialize discord ## Initialize Discord bot
intents = discord.Intents.default() bot.run(secrets_file.key)
intents.message_content = True
client = discord.Client(intents=intents)
client.run(secrets_file.key)
try: try:
while True: while True:
@ -133,9 +147,7 @@ if __name__ == "__main__":
observer.stop() observer.stop()
observer.join() observer.join()
##discord stuffs ## Discord event
@client.event @bot.event
async def on_ready(): async def on_ready():
print(f'We have logged in as {client.user}') print(f'We have logged in as {bot.user}')