mirror of
https://gitlab.com/pancakes1234/wdiscordbotserver.git
synced 2025-06-16 07:14:21 -06:00
Add terminal output cog: Implement command to display bot log output for admin users
This commit is contained in:
parent
99acd0156e
commit
6ea24b4b09
6
bot.py
6
bot.py
@ -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
41
cogs/terminal_output.py
Normal 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))
|
Loading…
x
Reference in New Issue
Block a user