From 0eb1a48fca5668c7c5c35ef81d247b4511d8e78a Mon Sep 17 00:00:00 2001 From: Samlon Date: Mon, 11 Aug 2025 17:16:33 +0200 Subject: changed printing to proper logging --- cogs/math.py | 7 +++++-- cogs/music/play.py | 7 +++++-- cogs/music/play_compressed.py | 9 ++++++--- cogs/vclogs.py | 13 ++++++++----- 4 files changed, 24 insertions(+), 12 deletions(-) (limited to 'cogs') diff --git a/cogs/math.py b/cogs/math.py index 53340f2..a837ed9 100644 --- a/cogs/math.py +++ b/cogs/math.py @@ -1,8 +1,11 @@ +import logging import subprocess -from discord import (ApplicationContext) +from discord import ApplicationContext from discord.ext import commands +logger = logging.getLogger(__name__) + class Math(commands.Cog): def __init__(self, bot): @@ -18,7 +21,7 @@ class Math(commands.Cog): await ctx.respond(f"{equation} = {math.decode('utf-8').strip()}") except Exception as e: await ctx.respond(e, ephemeral=True) - print(e) + logger.error(f"Error in math command: {e}", exc_info=True) def setup(bot): diff --git a/cogs/music/play.py b/cogs/music/play.py index 5d82127..d902cdc 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py @@ -1,10 +1,13 @@ import asyncio +import logging import discord import yt_dlp from discord.ext import commands from youtubesearchpython import VideosSearch +logger = logging.getLogger(__name__) + # Suppress noise about console usage from yt-dlp yt_dlp.utils.bug_reports_message = lambda: '' @@ -45,13 +48,13 @@ class Play(commands.Cog): def after_playing(error): if error: - print(f'Player error: {error}') + logger.error(f'Player error: {error}') coro = vc.disconnect() fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) try: fut.result() except Exception as e: - print(f'Error disconnecting: {e}') + logger.error(f'Error disconnecting: {e}', exc_info=True) source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) vc.play(source, after=after_playing) diff --git a/cogs/music/play_compressed.py b/cogs/music/play_compressed.py index 0d4901c..038a15e 100644 --- a/cogs/music/play_compressed.py +++ b/cogs/music/play_compressed.py @@ -1,4 +1,5 @@ import asyncio +import logging import os import subprocess @@ -6,6 +7,8 @@ import discord from discord.ext import commands from youtubesearchpython import VideosSearch +logger = logging.getLogger(__name__) + class GrajcujZkompresowane(commands.Cog): def __init__(self, bot): @@ -20,7 +23,7 @@ class GrajcujZkompresowane(commands.Cog): modified.write(f"{title}\n".encode('utf-8')) modified.write(data) except Exception as e: - print(f"Failed to prepend title: {e}") + logger.error(f"Failed to prepend title: {e}", exc_info=True) @staticmethod def compress_audio(input_path): @@ -84,13 +87,13 @@ class GrajcujZkompresowane(commands.Cog): title = found_title def after_playing(error): if error: - print(f'Player error: {error}') + logger.error(f'Player error: {error}') coro = vc.disconnect() fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop) try: fut.result() except Exception as e: - print(f'Error disconnecting: {e}') + logger.error(f'Error disconnecting: {e}', exc_info=True) if vc.is_playing() or vc.is_paused(): vc.stop() diff --git a/cogs/vclogs.py b/cogs/vclogs.py index 4bc7104..ed0737b 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py @@ -1,11 +1,14 @@ import datetime import json +import logging import os import discord from discord.ext import commands from gtts import gTTS +logger = logging.getLogger(__name__) + class VcLogs(commands.Cog): def __init__(self, bot): @@ -39,7 +42,7 @@ class VcLogs(commands.Cog): message, tts_text = state_messages[state_type][is_enabled] await log_channel.send(message) - print(message) + logger.info(f"Voice state change: {message}") return tts_text, channel async def play_tts(self, channel, text): @@ -80,10 +83,10 @@ class VcLogs(commands.Cog): json.dump({'volume': '5'}, f, indent=4) with open('settings.json', 'w') as f: json.dump({"volume": volume}, f, indent=4) - await ctx.send(f"Volume has been set to: {volume}") + await ctx.followup.send(f"Volume has been set to: {volume}") except Exception as e: await ctx.respond(e, ephemeral=True) - print(e) + logger.error(f"Error in volume command: {e}", exc_info=True) @commands.Cog.listener() async def on_voice_state_update(self, member, before, after): @@ -101,13 +104,13 @@ class VcLogs(commands.Cog): tts_text = f"{member.display_name} sie zespawnił" tts_channel = after.channel await log_channel.send(message) - print(message) + logger.info(f"User joined voice channel: {message}") elif before.channel is not None and after.channel is None: message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name}" tts_text = f"{member.display_name} jest chujem" tts_channel = before.channel await log_channel.send(message) - print(message) + logger.info(f"User left voice channel: {message}") if before.self_deaf != after.self_deaf: tts_text, tts_channel = await self.handle_state_change( -- cgit v1.2.3