Skip to content

Commit

Permalink
fix(twitch): send error logs when 403 on stream start
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Jan 27, 2025
1 parent efed7b1 commit 8fc266b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions core/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ class ServerWarningType(IntEnum):
TEMP_ROLE_REMOVE_FORBIDDEN = 11
# channel, feed_id
RSS_INVALID_FORMAT = 12
# channel_id, username
STREAM_NOTIFICATION_MISSING_PERMISSIONS = 13
# role_id, member, username
STREAM_ROLE_MISSING_PERMISSIONS = 14
13 changes: 13 additions & 0 deletions modules/serverlogs/serverlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,20 @@ async def on_server_warning(self, warning_type: ServerWarningType, guild: discor
user = kwargs.get("user")
emb.description = f"**Could not remove temporary role** {role.mention} from user {user.mention}"
emb.add_field(name="Reason", value="Missing permission")
elif warning_type == ServerWarningType.STREAM_NOTIFICATION_MISSING_PERMISSIONS:
channel_mention = f"<#{kwargs.get('channel_id')}>"
username = kwargs.get("username")
emb.description = f"**Could not send stream notification** in channel {channel_mention}"
emb.add_field(name="Streamer username", value=username)
elif warning_type == ServerWarningType.STREAM_ROLE_MISSING_PERMISSIONS:
role_mention = f"<@&{kwargs.get('role_id')}>"
member_mention = kwargs.get("member").mention
username = kwargs.get("username")
emb.description = f"**Could not give stream role** to user {member_mention}"
emb.add_field(name="Streamer username", value=username)
emb.add_field(name="Role to give", value=role_mention)
else:
self.bot.dispatch("error", f"Unknown warning type: {warning_type}")
return
await self.validate_logs(guild, channel_ids, emb, "bot_warnings")

Expand Down
13 changes: 11 additions & 2 deletions modules/twitch/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mysql.connector.errors import IntegrityError

from core.bot_classes import Axobot
from core.enums import ServerWarningType

from .api.api_agent import TwitchApiAgent
from .api.types import (GroupedStreamerDBObject, PlatformId, StreamersDBObject,
Expand Down Expand Up @@ -89,7 +90,8 @@ async def db_get_guilds_per_streamers(self, platform: PlatformId | None=None) ->
async def db_remove_streamer(self, guild_id: int, platform: PlatformId, user_id: str):
"Remove a streamer from the database"
query = "DELETE FROM `streamers` WHERE `guild_id` = %s AND `platform` = %s AND `user_id` = %s AND `beta` = %s"
async with self.bot.db_main.write(query, (guild_id, platform, user_id, self.bot.beta), returnrowcount=True) as query_result:
async with self.bot.db_main.write(
query, (guild_id, platform, user_id, self.bot.beta), returnrowcount=True) as query_result:
return query_result > 0

async def db_set_streamer_status(self, platform: PlatformId, user_id: str, is_streaming: bool):
Expand Down Expand Up @@ -320,14 +322,21 @@ async def on_stream_starts(self, stream: StreamObject, guild: discord.Guild):
"When a stream starts, send a notification to the subscribed guild"
# Send notification
if channel := await self.bot.get_config(guild.id, "streaming_channel"):
await self.send_stream_alert(stream, channel)
try:
await self.send_stream_alert(stream, channel)
except discord.Forbidden:
self.log.info("Cannot send notif to channel %s in guild %s: Forbidden", channel.id, guild.id)
self.bot.dispatch("server_warning", ServerWarningType.STREAM_NOTIFICATION_MISSING_PERMISSIONS,
guild, channel_id=channel.id, username=stream["user_name"])
# Grant role
if role := await self.bot.get_config(guild.id, "streaming_role"):
if member := await self.find_streamer_in_guild(stream["user_name"], guild):
try:
await member.add_roles(role, reason="Twitch streamer is live")
except discord.Forbidden:
self.log.info("Cannot add role %s to member %s in guild %s: Forbidden", role.id, member.id, guild.id)
self.bot.dispatch("server_warning", ServerWarningType.STREAM_ROLE_MISSING_PERMISSIONS,
guild, role_id=role.id, member=member, username=stream["user_name"])

@commands.Cog.listener()
async def on_stream_ends(self, _streamer_name: str, guild: discord.Guild):
Expand Down

0 comments on commit 8fc266b

Please sign in to comment.