# Buggy code
async def check_streams(self):
    for stream in self.streams:
        try:
            try:
                if stream.__class__.__name__ == "TwitchStream":
                    await self.maybe_renew_twitch_bearer_token()
                    embed, is_rerun = await stream.is_online()

                elif stream.__class__.__name__ == "YoutubeStream":
                    embed, is_schedule = await stream.is_online()

                else:
                    embed = await stream.is_online()
                    is_rerun = False
                    is_schedule = False
            except OfflineStream:
                if not stream._messages_cache:
                    continue
                for message in stream._messages_cache:
                    if await self.bot.cog_disabled_in_guild(self, message.guild):
                        continue
                    autodelete = await self.config.guild(message.guild).autodelete()
                    if autodelete:
                        with contextlib.suppress(discord.NotFound):
                            await message.delete()
                stream._messages_cache.clear()
                await self.save_streams()
            else:
                if stream._messages_cache:
                    continue
                for channel_id in stream.channels:
                    channel = self.bot.get_channel(channel_id)
                    if not channel:
                        continue
                    if await self.bot.cog_disabled_in_guild(self, channel.guild):
                        continue
                    ignore_reruns = await self.config.guild(
                        channel.guild
                    ).ignore_reruns()
                    if ignore_reruns and is_rerun:
                        continue
                    ignore_schedules = await self.config.guild(
                        channel.guild
                    ).ignore_schedule()
                    if ignore_schedules and is_schedule:
                        continue
                    if is_schedule:
                        # skip messages and mentions
                        await self._send_stream_alert(stream, channel, embed)
                        await self.save_streams()
                        continue
                    await set_contextual_locales_from_guild(self.bot, channel.guild)

                    mention_str, edited_roles = await self._get_mention_str(
                        channel.guild, channel
                    )

                    if mention_str:
                        alert_msg = await self.config.guild(
                            channel.guild
                        ).live_message_mention()
                        if alert_msg:
                            content = (
                                alert_msg  # Stop bad things from happening here...
                            )
                            content = content.replace(
                                "{stream.name}", str(stream.name)
                            )  # Backwards compatibility
                            content = content.replace("{stream}", str(stream.name))
                            content = content.replace("{mention}", mention_str)
                        else:
                            content = _("{mention}, {stream} is live!").format(
                                mention=mention_str,
                                stream=escape(
                                    str(stream.name),
                                    mass_mentions=True,
                                    formatting=True,
                                ),
                            )
                    else:
                        alert_msg = await self.config.guild(
                            channel.guild
                        ).live_message_nomention()
                        if alert_msg:
                            content = (
                                alert_msg  # Stop bad things from happening here...
                            )
                            content = content.replace(
                                "{stream.name}", str(stream.name)
                            )  # Backwards compatibility
                            content = content.replace("{stream}", str(stream.name))
                        else:
                            content = _("{stream} is live!").format(
                                stream=escape(
                                    str(stream.name),
                                    mass_mentions=True,
                                    formatting=True,
                                )
                            )
                    await self._send_stream_alert(stream, channel, embed, content)
                    if edited_roles:
                        for role in edited_roles:
                            await role.edit(mentionable=False)
                    await self.save_streams()
        except Exception as e:
            log.error(
                "An error has occured with Streams. Please report it.", exc_info=e
            )


# Traceback (UnboundLocalError)
[23:51:59] ERROR    An error has occured with Streams. Please report it.                       [red.core.cogs.Streams]
╭────────────────── Traceback (most recent call last) ───────────────────╮
│ /home/ubuntu/github/renchan/lib/python3.8/site-packages/Red_DiscordBot │
│ -3.4.4-py3.8.egg/redbot/cogs/streams/streams.py:768 in check_streams   │
│                                                                        │
│   765 │   │   │   │   │   │   ignore_schedules = await                 │
│       self.config.guild(channel.guild).ignore_schedule()               │
│   766 │   │   │   │   │   │   if ignore_schedules and is_schedule:     │
│   767 │   │   │   │   │   │   │   continue                             │
│ ❱ 768 │   │   │   │   │   │   if is_schedule:                          │
│   769 │   │   │   │   │   │   │   # skip messages and mentions         │
│   770 │   │   │   │   │   │   │   await self._send_stream_alert(stream │
│   771 │   │   │   │   │   │   │   await self.save_streams()            │
╰────────────────────────────────────────────────────────────────────────╯
UnboundLocalError: local variable 'is_schedule' referenced before
assignment
[23:57:06] ERROR    An error has occured with Streams. Please report it.                       [red.core.cogs.Streams]
╭────────────────── Traceback (most recent call last) ───────────────────╮
│ /home/ubuntu/github/renchan/lib/python3.8/site-packages/Red_DiscordBot │
│ -3.4.4-py3.8.egg/redbot/cogs/streams/streams.py:766 in check_streams   │
│                                                                        │
│   763 │   │   │   │   │   │   if ignore_reruns and is_rerun:           │
│   764 │   │   │   │   │   │   │   continue                             │
│   765 │   │   │   │   │   │   ignore_schedules = await                 │
│       self.config.guild(channel.guild).ignore_schedule()               │
│ ❱ 766 │   │   │   │   │   │   if ignore_schedules and is_schedule:     │
│   767 │   │   │   │   │   │   │   continue                             │
│   768 │   │   │   │   │   │   if is_schedule:                          │
│   769 │   │   │   │   │   │   │   # skip messages and mentions         │
╰────────────────────────────────────────────────────────────────────────╯
UnboundLocalError: local variable 'is_schedule' referenced before
assignment
[00:02:15] ERROR    An error has occured with Streams. Please report it.                       [red.core.cogs.Streams]
╭────────────────── Traceback (most recent call last) ───────────────────╮
│ /home/ubuntu/github/renchan/lib/python3.8/site-packages/Red_DiscordBot │
│ -3.4.4-py3.8.egg/redbot/cogs/streams/streams.py:768 in check_streams   │
│                                                                        │
│   765 │   │   │   │   │   │   ignore_schedules = await                 │
│       self.config.guild(channel.guild).ignore_schedule()               │
│   766 │   │   │   │   │   │   if ignore_schedules and is_schedule:     │
│   767 │   │   │   │   │   │   │   continue                             │
│ ❱ 768 │   │   │   │   │   │   if is_schedule:                          │
│   769 │   │   │   │   │   │   │   # skip messages and mentions         │
│   770 │   │   │   │   │   │   │   await self._send_stream_alert(stream │
│   771 │   │   │   │   │   │   │   await self.save_streams()            │
╰────────────────────────────────────────────────────────────────────────╯
UnboundLocalError: local variable 'is_schedule' referenced before
assignment

# Fixed code
async def check_streams(self):
    for stream in self.streams:
        try:
            try:
                is_rerun = False
                is_schedule = False
                if stream.__class__.__name__ == "TwitchStream":
                    await self.maybe_renew_twitch_bearer_token()
                    embed, is_rerun = await stream.is_online()

                elif stream.__class__.__name__ == "YoutubeStream":
                    embed, is_schedule = await stream.is_online()

                else:
                    embed = await stream.is_online()
            except OfflineStream:
                if not stream._messages_cache:
                    continue
                for message in stream._messages_cache:
                    if await self.bot.cog_disabled_in_guild(self, message.guild):
                        continue
                    autodelete = await self.config.guild(message.guild).autodelete()
                    if autodelete:
                        with contextlib.suppress(discord.NotFound):
                            await message.delete()
                stream._messages_cache.clear()
                await self.save_streams()
            else:
                if stream._messages_cache:
                    continue
                for channel_id in stream.channels:
                    channel = self.bot.get_channel(channel_id)
                    if not channel:
                        continue
                    if await self.bot.cog_disabled_in_guild(self, channel.guild):
                        continue
                    ignore_reruns = await self.config.guild(
                        channel.guild
                    ).ignore_reruns()
                    if ignore_reruns and is_rerun:
                        continue
                    ignore_schedules = await self.config.guild(
                        channel.guild
                    ).ignore_schedule()
                    if ignore_schedules and is_schedule:
                        continue
                    if is_schedule:
                        # skip messages and mentions
                        await self._send_stream_alert(stream, channel, embed)
                        await self.save_streams()
                        continue
                    await set_contextual_locales_from_guild(self.bot, channel.guild)

                    mention_str, edited_roles = await self._get_mention_str(
                        channel.guild, channel
                    )

                    if mention_str:
                        alert_msg = await self.config.guild(
                            channel.guild
                        ).live_message_mention()
                        if alert_msg:
                            content = (
                                alert_msg  # Stop bad things from happening here...
                            )
                            content = content.replace(
                                "{stream.name}", str(stream.name)
                            )  # Backwards compatibility
                            content = content.replace("{stream}", str(stream.name))
                            content = content.replace("{mention}", mention_str)
                        else:
                            content = _("{mention}, {stream} is live!").format(
                                mention=mention_str,
                                stream=escape(
                                    str(stream.name),
                                    mass_mentions=True,
                                    formatting=True,
                                ),
                            )
                    else:
                        alert_msg = await self.config.guild(
                            channel.guild
                        ).live_message_nomention()
                        if alert_msg:
                            content = (
                                alert_msg  # Stop bad things from happening here...
                            )
                            content = content.replace(
                                "{stream.name}", str(stream.name)
                            )  # Backwards compatibility
                            content = content.replace("{stream}", str(stream.name))
                        else:
                            content = _("{stream} is live!").format(
                                stream=escape(
                                    str(stream.name),
                                    mass_mentions=True,
                                    formatting=True,
                                )
                            )
                    await self._send_stream_alert(stream, channel, embed, content)
                    if edited_roles:
                        for role in edited_roles:
                            await role.edit(mentionable=False)
                    await self.save_streams()
        except Exception as e:
            log.error(
                "An error has occured with Streams. Please report it.", exc_info=e
            )