From cb30148f7738c7e555675f05c2c6ad87734930b9 Mon Sep 17 00:00:00 2001 From: crate Date: Thu, 10 Oct 2024 14:19:03 +0000 Subject: [PATCH] first commit --- .gitignore | 3 +++ terminal.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 terminal.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0bd16c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# ignore +__pycache__/ +config.py diff --git a/terminal.py b/terminal.py new file mode 100644 index 0000000..0cdf8c9 --- /dev/null +++ b/terminal.py @@ -0,0 +1,64 @@ +import config +import asyncio +import logging +from discord.ext import commands +import subprocess + +TOKEN = config.TOKEN +USER_ID = config.USER_ID +PREFIX = '$' + +bot = commands.Bot(command_prefix=PREFIX, intents=discord.Intents.all()) + +def setup_logging(): + logger = logging.getLogger('termlog') + logger.setLevel(logging.INFO) + handler = TimedRotatingFileHandler( + 'terminal.log', + when='midnight', + interval=1, + backupCount=7, + delay=True + ) + formatter = logging.Formatter( + '%(asctime)s [%(levelname)s] %(message)s', + datefmt='%Y-%m-%d %H:%M:%S' + ) + handler.setFormatter(formatter) + logger.addHandler(handler) + return logger + +logger = setup_logging() + +@bot.event +async def on_ready(): + logger.info(f'Logged in as {bot.user}') + +@bot.command(name='cmd') +async def run_cmd(ctx, *, command: str): + if str(ctx.author.id) == USER_ID: + try: + logger.info(f"Command: {command}") + result = subprocess.run(command, shell=True, capture_output=True, text=True) + output = result.stdout or result.stderr + logger.info(f"Output: {output}") + await ctx.send(f"```{output}```") + except Exception as e: + e = f"Error: {str(e)}" + logger.error(e) + await ctx.send(e) + else: + await ctx.send("You are not authorized to use this command.") + logger.warning(f"Command attempt by unauthorized user {ctx.author.id}.") + +async def bot_connect(): + while True: + try: + await bot.start(TOKEN) + except Exception as e: + e = str(e) + logger.error(f"Terminal couldn't connect! {e}") + await asyncio.sleep(60) + +if __name__ == "__main__": + asyncio.run(bot_connect())