Skip to content

Commit b12d079

Browse files
electron271anemoijereja-eden
authored andcommitted
fix(logging): remove redundant logging
1 parent 004a5cd commit b12d079

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

tux/cogs/services/starboard.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from tux.database.controllers import DatabaseController
1010
from tux.ui.embeds import EmbedCreator, EmbedType
1111
from tux.utils import checks
12+
from tux.utils.converters import get_channel_safe
1213
from tux.utils.functions import generate_usage
1314

1415

@@ -296,8 +297,8 @@ async def handle_starboard_reaction(self, payload: discord.RawReactionActionEven
296297
if not starboard or str(payload.emoji) != starboard.starboard_emoji:
297298
return
298299

299-
channel = self.bot.get_channel(payload.channel_id)
300-
if not isinstance(channel, discord.TextChannel):
300+
channel = await get_channel_safe(self.bot, payload.channel_id)
301+
if channel is None:
301302
return
302303

303304
try:

tux/cogs/utility/poll.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import cast
2-
31
import discord
42
from discord import app_commands
53
from discord.ext import commands
@@ -9,6 +7,7 @@
97
from tux.bot import Tux
108
from tux.database.controllers import DatabaseController
119
from tux.ui.embeds import EmbedCreator
10+
from tux.utils.converters import get_channel_safe
1211

1312
# TODO: Create option inputs for the poll command instead of using a comma separated string
1413

@@ -74,17 +73,9 @@ async def on_message(self, message: discord.Message) -> None:
7473
@commands.Cog.listener()
7574
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None:
7675
# get reaction from payload.message_id, payload.channel_id, payload.guild_id, payload.emoji
77-
channel = self.bot.get_channel(payload.channel_id)
76+
channel = await get_channel_safe(self.bot, payload.channel_id)
7877
if channel is None:
79-
try:
80-
channel = await self.bot.fetch_channel(payload.channel_id)
81-
except discord.NotFound:
82-
logger.error(f"Channel not found for ID: {payload.channel_id}")
83-
return
84-
except (discord.Forbidden, discord.HTTPException) as fetch_error:
85-
logger.error(f"Failed to fetch channel: {fetch_error}")
86-
return
87-
channel = cast(discord.TextChannel | discord.Thread, channel)
78+
return
8879

8980
message = await channel.fetch_message(payload.message_id)
9081
# Lookup the reaction object for this event

tux/utils/converters.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import re
2-
from typing import Any
2+
from typing import Any, cast
33

4+
import discord
45
from discord.ext import commands
6+
from loguru import logger
57

68
from prisma.enums import CaseType
9+
from tux.bot import Tux
710

811
time_regex = re.compile(r"(\d{1,5}(?:[.,]?\d{1,5})?)([smhd])")
912
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}
@@ -76,6 +79,21 @@ async def convert(self, ctx: commands.Context[Any], argument: str) -> CaseType:
7679
raise commands.BadArgument(msg) from e
7780

7881

82+
async def get_channel_safe(bot: Tux, channel_id: int) -> discord.TextChannel | discord.Thread | None:
83+
"""Get a channel by ID, returning None if not found."""
84+
channel = bot.get_channel(channel_id)
85+
if channel is None:
86+
try:
87+
channel = await bot.fetch_channel(channel_id)
88+
except discord.NotFound:
89+
logger.error(f"Channel not found for ID: {channel_id}")
90+
return None
91+
except (discord.Forbidden, discord.HTTPException) as fetch_error:
92+
logger.error(f"Failed to fetch channel: {fetch_error}")
93+
return None
94+
return cast(discord.TextChannel | discord.Thread, channel)
95+
96+
7997
def convert_bool(x: str | None) -> bool | None:
8098
"""Convert a string to a boolean value.
8199

0 commit comments

Comments
 (0)