import config import asyncio import logging from discord.ext import commands import subprocess TOKEN = config.BOT_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())