mirror of
https://github.com/majmongoose/PageBot.git
synced 2025-07-06 03:35:16 -04:00
Revert "Update pagebot.py"
This reverts commit d15fd53c867a5fd04de75101448b305c3045b367.
This commit is contained in:
parent
d15fd53c86
commit
da41fa6f73
62
pagebot.py
62
pagebot.py
@ -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,9 +9,6 @@ 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):
|
||||||
@ -32,7 +29,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")
|
||||||
bot.loop.create_task(upload_to_discord(mp4_file,text))
|
client.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):
|
||||||
@ -65,6 +62,8 @@ 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.
|
||||||
@ -72,7 +71,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 = bot.get_channel(secrets_file.channel_id)
|
channel = client.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
|
||||||
@ -93,52 +92,39 @@ 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}")
|
||||||
|
|
||||||
## Command to restart TTD software manually
|
def launch_and_watch(program_path):
|
||||||
@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)
|
||||||
|
while True:
|
||||||
process = subprocess.Popen(program_path,cwd=program_directory)
|
process = subprocess.Popen(program_path,cwd=program_directory)
|
||||||
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.")
|
||||||
|
|
||||||
## Launch and watch TTD
|
|
||||||
def launch_and_watch_ttd():
|
|
||||||
while True:
|
|
||||||
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
|
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()
|
||||||
|
|
||||||
## Start watching TTD
|
## Launch TTD
|
||||||
if secrets_file.ttd_path:
|
if (secrets_file.ttd_path != ""):
|
||||||
ttd_thread = threading.Thread(target=launch_and_watch_ttd)
|
watchdog_thread = threading.Thread(target=launch_and_watch, args=(secrets_file.ttd_path,))
|
||||||
ttd_thread.start()
|
watchdog_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:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@ -147,7 +133,9 @@ if __name__ == "__main__":
|
|||||||
observer.stop()
|
observer.stop()
|
||||||
observer.join()
|
observer.join()
|
||||||
|
|
||||||
## Discord event
|
##discord stuffs
|
||||||
@bot.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'We have logged in as {bot.user}')
|
print(f'We have logged in as {client.user}')
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user