From 00c6b3634432ff0ae9acf47b6d7ca487852f54f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sk=C3=BDpala?= Date: Sun, 5 Nov 2023 23:48:35 +0100 Subject: [PATCH] News: Search news by id And not by index. There would be bazzilion mistakes that way. --- hrochobot/cogs/news.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/hrochobot/cogs/news.py b/hrochobot/cogs/news.py index 8778fbb..fdebab9 100644 --- a/hrochobot/cogs/news.py +++ b/hrochobot/cogs/news.py @@ -50,14 +50,19 @@ def format_entry(entry, author=None): return embed -async def post_news(bot, guild, index): +async def post_news(bot, guild, entry_id): news_json = data.load_guild_data(guild.id, NEWS_JSON) if "news_channel" not in news_json: - return + return "News channel not set." channel = get(guild.channels, id=news_json["news_channel"]) feed = await ksp_feed() - return await channel.send(embed=format_entry(feed.entries[index], author=feed.feed.author)) + entries_with_id = list(filter(lambda e: e.id == f"https://ksp.mff.cuni.cz/news_{entry_id}", feed.entries)) + if len(entries_with_id) == 0: + return f"Entry with id ``{entry_id}`` not found." + + await channel.send(embed=format_entry(entries_with_id[0], author=feed.feed.author)) + return None class News(commands.Cog): @@ -79,9 +84,11 @@ class News(commands.Cog): return await ctx.respond(f"News channel set to {channel.mention}.", ephemeral=True) @news.command(description="Synchronize news feed.") - @discord.option("index", int, description="Send index-th most recent entry. (indexed from 0)") - async def post_news(self, ctx, index: int): - await post_news(self.bot, ctx.guild, index) + @discord.option("id", str, description="Id of entry to send.") + async def post_news(self, ctx, id: int): + err = await post_news(self.bot, ctx.guild, id) + if err: + return await ctx.respond(err, ephemeral=True) return await ctx.respond(f"News posted.", ephemeral=True)