diff --git a/core/enums.py b/core/enums.py index c5d1d88cd..5b2bd2b03 100644 --- a/core/enums.py +++ b/core/enums.py @@ -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 diff --git a/modules/serverlogs/serverlogs.py b/modules/serverlogs/serverlogs.py index 5f9ce30cd..e46d815f3 100644 --- a/modules/serverlogs/serverlogs.py +++ b/modules/serverlogs/serverlogs.py @@ -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") diff --git a/modules/twitch/twitch.py b/modules/twitch/twitch.py index 5c70cf338..862fabb2a 100644 --- a/modules/twitch/twitch.py +++ b/modules/twitch/twitch.py @@ -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, @@ -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): @@ -320,7 +322,12 @@ 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): @@ -328,6 +335,8 @@ async def on_stream_starts(self, stream: StreamObject, guild: discord.Guild): 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):