From 42fab012bcd34664979162c48d3a908086e73844 Mon Sep 17 00:00:00 2001 From: corbz Date: Tue, 13 Feb 2024 00:08:06 +0000 Subject: [PATCH] subscription links --- src/extensions/rss.py | 84 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/src/extensions/rss.py b/src/extensions/rss.py index 7bec1ed..a68e45f 100644 --- a/src/extensions/rss.py +++ b/src/extensions/rss.py @@ -115,7 +115,7 @@ class FeedCog(commands.Cog): Command cog. """ - def __init__(self, bot): + def __init__(self, bot: commands.Bot): super().__init__() self.bot = bot @@ -167,6 +167,32 @@ class FeedCog(commands.Cog): for sub in subscriptions ] + async def autocomplete_subscription_channels(self, inter: Interaction, uuid: str): + """""" + + log.debug("autocompleting subscription channels") + + try: + async with aiohttp.ClientSession() as session: + api = API(self.bot.api_token, session) + results, _ = await api.get_subscription_channels() + + except Exception as exc: + log.error(exc) + return [] + + subscription_channels = SubscriptionChannel.from_list(results) + + async def name(link): + result = self.bot.get_channel(link.id) or await self.bot.fetch_channel(link.id) + return f"{link.subscription.name} -> #{result.name}" + + return [ + Choice(name=await name(link), value=link.uuid) + for link in subscription_channels + ] + + # channel_group = Group( # name="channels", # description="channel commands", @@ -270,7 +296,7 @@ class FeedCog(commands.Cog): @subscription_group.command(name="link") @autocomplete(sub_uuid=autocomplete_subscriptions) @rename(sub_uuid="subscription") - async def link_subscription_to_channel(self, inter: Interaction, sub_uuid: str, channel: TextChannel): + async def link_subscription_channel(self, inter: Interaction, sub_uuid: str, channel: TextChannel): """ Link Subscription to discord.TextChannel. """ @@ -296,6 +322,60 @@ class FeedCog(commands.Cog): .send(inter) ) + @subscription_group.command(name="unlink") + @autocomplete(uuid=autocomplete_subscription_channels) + @rename(uuid="link") + async def unlink_subscription_channel(self, inter: Interaction, uuid: str): + """ + Unlink subscription from discord.TextChannel. + """ + + await inter.response.defer() + + try: + async with aiohttp.ClientSession() as session: + api = API(self.bot.api_token, session) + + except Exception as exc: + return await ( + Followup(exc.__class__.__name__, str(exc)) + .error() + .send(inter) + ) + + await ( + Followup("Unlinked!") + .added() + .send(inter) + ) + + @subscription_group.command(name="list-links") + async def list_subscription(self, inter: Interaction): + """List Subscriptions Channels in this server.""" + + await inter.response.defer() + + page = 1 + pagesize = 10 + + async def formatdata(index: int, item: dict) -> tuple[str, str]: + item = SubscriptionChannel.from_dict(item) + channel = self.bot.get_channel(id) or await self.bot.fetch_channel(item.id) + next_emoji = self.bot.get_emoji(1204542366602502265) + key = f"{index}. {item.subscription.name} {next_emoji} {channel.mention}" + value = f"[RSS]({item.subscription.rss_url}) ยท [API]({API.CHANNEL_ENDPOINT}{item.uuid}/)" + return key, value + + async def getdata(page: int) -> dict: + async with aiohttp.ClientSession() as session: + api = API(self.bot.api_token, session) + return await api.get_subscription_channels( + subscription__server=inter.guild.id, page=page, page_size=pagesize + ) + + embed = Followup(f"Links in {inter.guild.name}").info()._embed + pagination = PaginationView(self.bot, inter, embed, getdata, formatdata, pagesize, page) + await pagination.send() @subscription_group.command(name="add") async def new_subscription(self, inter: Interaction, name: str, rss_url: str):