74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
import config
|
|
import asyncio
|
|
import logging
|
|
import discord
|
|
import subprocess
|
|
from discord.ext import commands
|
|
from logging.handlers import TimedRotatingFileHandler
|
|
|
|
TOKEN = config.BOT_TOKEN
|
|
USER_ID = config.USER_ID
|
|
PREFIX = '$'
|
|
|
|
intents = discord.Intents.default()
|
|
intents.guilds = True
|
|
intents.messages = True
|
|
intents.dm_messages = True
|
|
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
|
|
|
|
def setup_logging():
|
|
logger = logging.getLogger('termlog')
|
|
logger.setLevel(logging.INFO)
|
|
formatter=logging.Formatter(
|
|
'%(asctime)s [%(levelname)s] %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
)
|
|
file_handler = TimedRotatingFileHandler(
|
|
'terminal.log',
|
|
when='midnight',
|
|
interval=1,
|
|
backupCount=7,
|
|
delay=True
|
|
)
|
|
file_handler.setFormatter(formatter)
|
|
console_handler = logging.StreamHandler()
|
|
console_handler.setFormatter(formatter)
|
|
logger.addHandler(file_handler)
|
|
logger.addHandler(console_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())
|