From 32b9a54a2787773a0688afc7ded3f32eeb1f56e1 Mon Sep 17 00:00:00 2001 From: Alex Orid Date: Tue, 8 Dec 2020 06:06:58 -0500 Subject: [PATCH] Added Restart/Shutdown Commands Added the restart and shutdown command to the bot's discord module. --- .../implemented/command_restartBot_discord.py | 23 +++++++++++++ .../command_shutdownBot_discord.py | 23 +++++++++++++ discord_script.py | 7 ++-- requirements.txt | 3 +- twitch_script.py | 3 +- utilities_script.py | 34 ++++++++++++++++++- 6 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 commands/implemented/command_restartBot_discord.py create mode 100644 commands/implemented/command_shutdownBot_discord.py diff --git a/commands/implemented/command_restartBot_discord.py b/commands/implemented/command_restartBot_discord.py new file mode 100644 index 0000000..49e38bd --- /dev/null +++ b/commands/implemented/command_restartBot_discord.py @@ -0,0 +1,23 @@ +from abc import ABCMeta + +from commands.command_base import AbstractCommand + +import discord +import discord.message + +import utilities_script as utilities + +class CommandTTS(AbstractCommand, metaclass=ABCMeta): + command = "//restart" + + def __init__(self): + super().__init__(CommandTTS.command, n_args=0, command_type=AbstractCommand.CommandType.DISCORD) + + async def do_command(self, bot, discord_message: discord.message): + + if str(discord_message.author.top_role) == "Admin": + print("Admin Check") + #response = str("Bot restarting... on %s" % discord_message.guild.name) + response = str("Bot restarting...") + await bot.send_message(discord_message, response) + utilities.restart_self() diff --git a/commands/implemented/command_shutdownBot_discord.py b/commands/implemented/command_shutdownBot_discord.py new file mode 100644 index 0000000..588850f --- /dev/null +++ b/commands/implemented/command_shutdownBot_discord.py @@ -0,0 +1,23 @@ +from abc import ABCMeta + +from commands.command_base import AbstractCommand + +import discord +import discord.message + +import utilities_script as utilities + +class CommandTTS(AbstractCommand, metaclass=ABCMeta): + command = "//shutdown" + + def __init__(self): + super().__init__(CommandTTS.command, n_args=0, command_type=AbstractCommand.CommandType.DISCORD) + + async def do_command(self, bot, discord_message: discord.message): + + if str(discord_message.author.top_role) == "Admin": + print("Admin Check") + #response = str("Bot restarting... on %s" % discord_message.guild.name) + response = str("Bot shutting down...") + await bot.send_message(discord_message, response) + utilities.hard_shutdown() diff --git a/discord_script.py b/discord_script.py index 16fa26f..36d1a40 100644 --- a/discord_script.py +++ b/discord_script.py @@ -1,5 +1,6 @@ import random import re +import utilities_script as utilities from discord import message from discord.client import Client @@ -49,13 +50,15 @@ class Discord_Module(discord.Client): async def on_message(self, message: discord.Message): print("{" + message.guild.name + "}[ " + str(message.channel) + " ](" + message.author.display_name + ")> ") + #print(message.author.mention) print(message.content) #print(message.channel.id) #Message ID #print(str(message.id)) #print(str(message.channel.id)) - if message.content == "//testing": - await message.channel.send('test response') + if message.content == "//test": + pass + #await message.channel.send('test response') #test = self.get_channel(431129571308339210) #await test.send("testerino") diff --git a/requirements.txt b/requirements.txt index b90342e..25d7c4d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ pandas numpy gTTS playsound -discord.py \ No newline at end of file +discord.py +psutil \ No newline at end of file diff --git a/twitch_script.py b/twitch_script.py index faa3406..ee39c98 100644 --- a/twitch_script.py +++ b/twitch_script.py @@ -1,6 +1,7 @@ from typing import Sequence import random import re +import utilities_script as utilities import twitch import twitch.chat @@ -107,7 +108,7 @@ class Twitch_Module(): #print(e) print("failed command") pass # we don't care - + def tts_message(self, message: twitch.chat.Message): if not self.contains_slur(message): diff --git a/utilities_script.py b/utilities_script.py index 38a1292..1d88c72 100644 --- a/utilities_script.py +++ b/utilities_script.py @@ -1,6 +1,38 @@ import os +import sys +import psutil +import subprocess clearScreen = lambda: os.system('cls' if os.name == 'nt' else 'clear') def get_args(text: str) -> list: - return text.split(" ") \ No newline at end of file + return text.split(" ") + +def hard_shutdown(): + current_system_pid = os.getpid() + + ThisSystem = psutil.Process(current_system_pid) + ThisSystem.terminate() + +def restart_self(): + + #current_system_pid = os.getpid() + #os.startfile("python C:/praxis/main.py") + #subprocess.run("python C:/praxis/main.py") + #subprocess.call("python main.py", shell=True) + os.system('start cmd /k python main.py') + hard_shutdown() + #os.system('python twitch_script.py') + #os.system('python discord_script.py') + +def restart_target(): + pass + +def launch_target(inputScript: str): + cmd = "start cmd /k python " + inputScript + os.system(cmd) + + +if __name__ == "__main__": + #pass + restart_self() \ No newline at end of file