mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 11:40:12 +01:00
chore: ruff formatting
This commit is contained in:
@@ -10,18 +10,24 @@ from utils.checks import is_owner_or_friend
|
||||
class Logs(commands.Cog, name="logs"):
|
||||
def __init__(self, bot) -> None:
|
||||
self.bot = bot
|
||||
|
||||
async def send_embed(self, context: Context, embed: discord.Embed, *, ephemeral: bool = False) -> None:
|
||||
|
||||
async def send_embed(
|
||||
self, context: Context, embed: discord.Embed, *, ephemeral: bool = False
|
||||
) -> None:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if interaction.response.is_done():
|
||||
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
|
||||
else:
|
||||
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
|
||||
await interaction.response.send_message(
|
||||
embed=embed, ephemeral=ephemeral
|
||||
)
|
||||
else:
|
||||
await context.send(embed=embed)
|
||||
|
||||
async def send_file(self, context: Context, *, file: discord.File, ephemeral: bool = False) -> None:
|
||||
async def send_file(
|
||||
self, context: Context, *, file: discord.File, ephemeral: bool = False
|
||||
) -> None:
|
||||
interaction = getattr(context, "interaction", None)
|
||||
if interaction is not None:
|
||||
if interaction.response.is_done():
|
||||
@@ -35,7 +41,9 @@ class Logs(commands.Cog, name="logs"):
|
||||
name="logs",
|
||||
description="View the bot's log file",
|
||||
)
|
||||
@app_commands.describe(lines="Number of lines to read from the end of the log file (default: 50, max: 200)")
|
||||
@app_commands.describe(
|
||||
lines="Number of lines to read from the end of the log file (default: 50, max: 200)"
|
||||
)
|
||||
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
|
||||
@app_commands.allowed_installs(guilds=True, users=True)
|
||||
@is_owner_or_friend()
|
||||
@@ -44,12 +52,15 @@ class Logs(commands.Cog, name="logs"):
|
||||
lines = 200
|
||||
elif lines < 1:
|
||||
lines = 1
|
||||
|
||||
|
||||
log_file_path = os.getenv("LOG_FILE", "logs/discord.log")
|
||||
|
||||
|
||||
if not os.path.isabs(log_file_path):
|
||||
log_file_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), log_file_path)
|
||||
|
||||
log_file_path = os.path.join(
|
||||
os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
|
||||
log_file_path,
|
||||
)
|
||||
|
||||
try:
|
||||
if not os.path.exists(log_file_path):
|
||||
embed = discord.Embed(
|
||||
@@ -57,47 +68,57 @@ class Logs(commands.Cog, name="logs"):
|
||||
description=f"Log file not found at: `{log_file_path}`",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp")
|
||||
embed.set_author(
|
||||
name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp"
|
||||
)
|
||||
await self.send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
with open(log_file_path, 'r', encoding='utf-8', errors='replace') as f:
|
||||
|
||||
with open(log_file_path, "r", encoding="utf-8", errors="replace") as f:
|
||||
all_lines = f.readlines()
|
||||
|
||||
|
||||
if not all_lines:
|
||||
embed = discord.Embed(
|
||||
title="Logs",
|
||||
description="Log file is empty.",
|
||||
color=0x7289DA,
|
||||
)
|
||||
embed.set_author(name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp")
|
||||
embed.set_author(
|
||||
name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp"
|
||||
)
|
||||
await self.send_embed(context, embed, ephemeral=True)
|
||||
return
|
||||
|
||||
|
||||
selected_lines = all_lines[-lines:] if len(all_lines) > lines else all_lines
|
||||
log_content = ''.join(selected_lines)
|
||||
|
||||
log_content = "".join(selected_lines)
|
||||
|
||||
log_file = f"logs.txt"
|
||||
with open(log_file, "w", encoding="utf-8") as f:
|
||||
f.write(f"Bot logs extracted at {discord.utils.utcnow().strftime('%Y-%m-%d %H:%M:%S')} UTC\n")
|
||||
f.write(f"Last {len(selected_lines)} lines from {os.path.basename(log_file_path)}\n")
|
||||
f.write(
|
||||
f"Bot logs extracted at {discord.utils.utcnow().strftime('%Y-%m-%d %H:%M:%S')} UTC\n"
|
||||
)
|
||||
f.write(
|
||||
f"Last {len(selected_lines)} lines from {os.path.basename(log_file_path)}\n"
|
||||
)
|
||||
f.write(f"Total lines in log file: {len(all_lines)}\n")
|
||||
f.write("-" * 50 + "\n\n")
|
||||
f.write(ascii_plain + "\n\n")
|
||||
f.write(log_content)
|
||||
|
||||
|
||||
file_obj = discord.File(log_file)
|
||||
await self.send_file(context, file=file_obj, ephemeral=True)
|
||||
|
||||
|
||||
os.remove(log_file)
|
||||
|
||||
|
||||
except PermissionError:
|
||||
embed = discord.Embed(
|
||||
title="Error",
|
||||
description=f"Permission denied when trying to read log file: `{log_file_path}`",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp")
|
||||
embed.set_author(
|
||||
name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp"
|
||||
)
|
||||
await self.send_embed(context, embed, ephemeral=True)
|
||||
except Exception as e:
|
||||
embed = discord.Embed(
|
||||
@@ -105,7 +126,9 @@ class Logs(commands.Cog, name="logs"):
|
||||
description=f"An error occurred while reading the log file:\n```\n{str(e)}\n```",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp")
|
||||
embed.set_author(
|
||||
name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp"
|
||||
)
|
||||
await self.send_embed(context, embed, ephemeral=True)
|
||||
|
||||
async def cog_command_error(self, context: Context, error) -> None:
|
||||
@@ -115,7 +138,9 @@ class Logs(commands.Cog, name="logs"):
|
||||
description="You are not the owner of this bot!",
|
||||
color=0xE02B2B,
|
||||
)
|
||||
embed.set_author(name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp")
|
||||
embed.set_author(
|
||||
name="Owner", icon_url="https://yes.nighty.works/raw/zReOib.webp"
|
||||
)
|
||||
await self.send_embed(context, embed, ephemeral=True)
|
||||
else:
|
||||
raise error
|
||||
@@ -123,4 +148,3 @@ class Logs(commands.Cog, name="logs"):
|
||||
|
||||
async def setup(bot) -> None:
|
||||
await bot.add_cog(Logs(bot))
|
||||
|
||||
Reference in New Issue
Block a user