Add terminal output cog: Implement command to display bot log output for admin users

This commit is contained in:
Slipstream 2025-05-19 12:08:26 -06:00
parent 99acd0156e
commit 6ea24b4b09
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD
2 changed files with 47 additions and 0 deletions

6
bot.py
View File

@ -7,6 +7,12 @@ import traceback
import sys
import functools
from discord import app_commands
import sys
# Redirect stdout and stderr to a log file
log_file_path = 'bot.log'
sys.stdout = open(log_file_path, 'a')
sys.stderr = open(log_file_path, 'a')
# Load environment variables
load_dotenv("/home/server/keys.env")

41
cogs/terminal_output.py Normal file
View File

@ -0,0 +1,41 @@
import discord
from discord.ext import commands
from discord import app_commands
import os
class TerminalOutput(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.log_file_path = 'bot.log'
@app_commands.command(name='logs', description='Shows the bot log output (Admin only)')
@app_commands.is_owner() # Restrict to bot owner, can change to check for admin role if needed
async def show_logs(self, interaction: discord.Interaction):
if not os.path.exists(self.log_file_path):
await interaction.response.send_message("Log file not found.", ephemeral=True)
return
try:
with open(self.log_file_path, 'r') as f:
log_content = f.read()
except Exception as e:
await interaction.response.send_message(f"Error reading log file: {e}", ephemeral=True)
return
if not log_content:
await interaction.response.send_message("Log file is empty.", ephemeral=True)
return
# Split output into chunks of less than 2000 characters
chunks = [log_content[i:i + 1990] for i in range(0, len(log_content), 1990)] # Leave some buffer for formatting
# Send the first chunk as the initial response
await interaction.response.send_message(f"```\n{chunks[0]}\n```", ephemeral=True)
# Send subsequent chunks as followups
for chunk in chunks[1:]:
await interaction.followup.send(f"```\n{chunk}\n```", ephemeral=True)
async def setup(bot):
await bot.add_cog(TerminalOutput(bot))