From d3308af0e238c7d279d3cbe66b8b031290a431e0 Mon Sep 17 00:00:00 2001 From: Karlo Prikratki Date: Thu, 29 Dec 2022 20:18:26 +0100 Subject: [PATCH] [Streams] Add toggleable button to stream alerts (#5856) Co-authored-by: Jakub Kuczys --- docs/cog_guides/streams.rst | 16 ++++++++++++++ redbot/cogs/streams/streams.py | 38 +++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/docs/cog_guides/streams.rst b/docs/cog_guides/streams.rst index cdc87375b..23aefd078 100644 --- a/docs/cog_guides/streams.rst +++ b/docs/cog_guides/streams.rst @@ -338,6 +338,22 @@ To set the Twitch API tokens, follow these steps: .. attention:: These tokens are sensitive and should only be used in a private channel or in DM with the bot. +.. _streams-command-streamset-usebuttons: + +^^^^^^^^^^^^^^^^^^^^ +streamset usebuttons +^^^^^^^^^^^^^^^^^^^^ + +**Syntax** + +.. code-block:: none + + [p]streamset usebuttons + +**Description** + +Toggle whether to use buttons for stream alerts. + .. _streams-command-picarto: ^^^^^^^ diff --git a/redbot/cogs/streams/streams.py b/redbot/cogs/streams/streams.py index 6de0774ed..759515953 100644 --- a/redbot/cogs/streams/streams.py +++ b/redbot/cogs/streams/streams.py @@ -61,6 +61,7 @@ class Streams(commands.Cog): "live_message_nomention": False, "ignore_reruns": False, "ignore_schedule": False, + "use_buttons": False, } role_defaults = {"mention": False} @@ -289,7 +290,18 @@ class Streams(commands.Cog): return else: embed = info - await ctx.send(embed=embed) + + use_buttons: bool = await self.config.guild(ctx.channel.guild).use_buttons() + view = None + if use_buttons: + stream_url = embed.url + view = discord.ui.View() + view.add_item( + discord.ui.Button( + label=_("Watch the stream"), style=discord.ButtonStyle.link, url=stream_url + ) + ) + await ctx.send(embed=embed, view=view) @commands.group() @commands.guild_only() @@ -705,6 +717,19 @@ class Streams(commands.Cog): await self.config.guild(guild).ignore_schedule.set(True) await ctx.send(_("Streams schedules will no longer send an alert.")) + @streamset.command(name="usebuttons") + @commands.guild_only() + async def use_buttons(self, ctx: commands.Context): + """Toggle whether to use buttons for stream alerts.""" + guild = ctx.guild + current_setting: bool = await self.config.guild(guild).use_buttons() + if current_setting: + await self.config.guild(guild).use_buttons.set(False) + await ctx.send(_("I will no longer use buttons in stream alerts.")) + else: + await self.config.guild(guild).use_buttons.set(True) + await ctx.send(_("I will use buttons in stream alerts.")) + async def add_or_remove(self, ctx: commands.Context, stream, discord_channel): if discord_channel.id not in stream.channels: stream.channels.append(discord_channel.id) @@ -773,10 +798,21 @@ class Streams(commands.Cog): *, is_schedule: bool = False, ): + use_buttons: bool = await self.config.guild(channel.guild).use_buttons() + view = None + if use_buttons: + stream_url = embed.url + view = discord.ui.View() + view.add_item( + discord.ui.Button( + label=_("Watch the stream"), style=discord.ButtonStyle.link, url=stream_url + ) + ) m = await channel.send( content, embed=embed, allowed_mentions=discord.AllowedMentions(roles=True, everyone=True), + view=view, ) message_data = {"guild": m.guild.id, "channel": m.channel.id, "message": m.id} if is_schedule: