Skip to content

Commit

Permalink
Merge pull request #45 from notsniped/add-colors-to-terminal-output
Browse files Browse the repository at this point in the history
Add some colors to the client terminal output
  • Loading branch information
notsniped authored May 1, 2024
2 parents 7323844 + 6f4a45e commit ca2881b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import framework.auth
import framework.logger
from framework.colors import Colors as colors
from datetime import datetime
from discord import ApplicationContext, option
from discord.ext import commands
Expand All @@ -30,14 +31,15 @@ def create_files():
logs = ["snipe.log", "editsnipe.log", "errors.log"]
for log_file in logs:
if not os.path.isfile(f"logs/{log_file}"):
print(f"[!] {colors.yellow}\"{log_file}\" appears to be missing from logs directory.{colors.end} Creating file...")
with open(f"logs/{log_file}", 'x', encoding="utf-8") as f:
f.close()

# Create database files
databases = ["snipe.json", "editsnipe.json", "database.json"]
for db in databases:
if not os.path.isfile(db):
print(f"[!] \"{db}\" appears to be missing from directory. Creating file...")
print(f"[!] {colors.yellow}\"{db}\" appears to be missing from directory.{colors.end} Creating file...")
with open(db, 'x', encoding="utf-8") as f:
if db == "database.json":
json.dump({"audit_channel": {}}, f)
Expand Down Expand Up @@ -97,19 +99,19 @@ async def on_application_command_error(ctx: ApplicationContext, error: discord.D
elif isinstance(error, commands.BotMissingPermissions): await ctx.respond(":x: I don\'t have the required permissions to use this.\nIf you think this is a mistake, please go to server settings and fix the bot's role permissions.")
elif isinstance(error, commands.NoPrivateMessage): await ctx.respond(":x: You can only use this command in a server!", ephemeral=True)
else: # If the exception isnt pre-handled, land at this logic-gate and return the raw error from the command.
print(f"[main/Client] Command failure: An uncaught error occured while running the command.\n >>> {error}")
print(f"{colors.red}[main/Client] Command failure: An uncaught error occured while running the command.\n >>> {error}{colors.end}")
logger.error(f"[main/Client] Command failure: An uncaught error occured while running the command.\n >>> {error}")
await ctx.respond(f"An uncaught error occured while running the command.\n```\n{error}\n```")

# API Events
@client.event
async def on_ready():
print(f'Logged on to Discord as {client.user.name}')
print(f'[main/Client] {colors.green}Logged on to Discord as {client.user.name}{colors.end}')
print('====================')
print('Some client info:')
print('------------------')
print(f' Latency: {round(client.latency * 1000)}ms')
print(f' Startup time: {round(startTime)}')
print(f' Startup timestamp: {round(startTime)}')
print(f' Owner: {str(owner)}')
print('------------------')
print('====================')
Expand Down Expand Up @@ -148,7 +150,7 @@ async def on_message_delete(message):
# Log the edited message content to the client deleted message log.
if bool(snipe_log):
timestamp = datetime.now().strftime('%H:%M:%S')
print(f"[{timestamp}] Message deleted in #{message.channel} ({message.guild}):\n Message content: {message.content}")
print(f"[{timestamp}] {colors.cyan}Message deleted in #{message.channel}{colors.end} ({message.guild}):\n Message content: {message.content}")
logger.snipe(f"[{timestamp}] Message deleted in #{message.channel} ({message.guild}): {message.content}")

# Send the deleted message content to audit logging channel
Expand Down Expand Up @@ -186,7 +188,7 @@ async def on_message_edit(message_before, message_after):
# Log the edited message content to the client deleted message log.
if bool(editsnipe_log):
timestamp = datetime.now().strftime('%H:%M:%S')
print(f"[{timestamp}] Message edited in #{message_before.channel} ({message_before.guild}):\n Old message: {message_before.content}\n New message: {message_after.content}")
print(f"[{timestamp}] {colors.cyan}Message edited in #{message_before.channel}{colors.end} ({message_before.guild}):\n Old message: {message_before.content}\n New message: {message_after.content}")
logger.editsnipe(f"[{timestamp}] Message edited in #{message_before.channel} ({message_before.guild}):\n Old message: {message_before.content}\n New message: {message_after.content}")

# Send the edited message content to audit logging channel
Expand Down Expand Up @@ -294,5 +296,5 @@ async def _editsnipe(ctx: ApplicationContext, user: discord.User):
token = auth.get_token()
try: client.run(token)
except Exception as exc:
print(f"[main/CLIENT] Error: Unable to start client: {type(exc).__name__}: {exc}")
print(f"[main/Client] {colors.red}Error: Unable to start client: {type(exc).__name__}: {exc}{colors.end}")
raise SystemExit
10 changes: 10 additions & 0 deletions framework/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Library used for fetching stdout colors. (color codes)"""

class Colors:
"""Contains general stdout colors."""
cyan = '\033[96m'
red = '\033[91m'
green = '\033[92m'
orange = '\033[33m'
yellow = '\033[93m'
end = '\033[0m'

0 comments on commit ca2881b

Please sign in to comment.