diff --git a/src/extensions/tasks.py b/src/extensions/tasks.py index c718623..ec49ee6 100644 --- a/src/extensions/tasks.py +++ b/src/extensions/tasks.py @@ -65,13 +65,24 @@ class TaskCog(commands.Cog): """ self.subscription_task.cancel() - @app_commands.command(name="debug-trigger-task") - async def debug_trigger_task(self, inter): + group = app_commands.Group( + name="task", + description="Commands for tasks", + guild_only=True + ) + + @group.command(name="trigger") + async def cmd_trigger_task(self, inter): await inter.response.defer() start_time = perf_counter() - await self.subscription_task() - end_time = perf_counter() - await inter.followup.send(f"completed in {end_time - start_time:.4f} seconds") + + try: + await self.subscription_task() + except Exception as error: + await inter.followup.send(str(error)) + finally: + end_time = perf_counter() + await inter.followup.send(f"completed in {end_time - start_time:.4f} seconds") @tasks.loop(time=subscription_task_times) async def subscription_task(self): @@ -199,7 +210,7 @@ class TaskCog(commands.Cog): continue blocked = any(self.filter_item(_filter, item) for _filter in filters) - mutated_item = item.create_mutated_copy(sub.mutators) + mutated_item = item.create_mutated_copy(sub.mutators) if sub.mutators else None for channel in channels: await self.track_and_send(sub, feed, item, mutated_item, channel, blocked) @@ -225,7 +236,7 @@ class TaskCog(commands.Cog): log.debug("filter match found? '%s'", match_found) return match_found - async def track_and_send(self, sub: Subscription, feed: RSSFeed, item: RSSItem, mutated_item: RSSItem, channel: TextChannel, blocked: bool): + async def track_and_send(self, sub: Subscription, feed: RSSFeed, item: RSSItem, mutated_item: RSSItem | None, channel: TextChannel, blocked: bool): message_id = -1 log.debug("track and send func %s, %s", item.guid, item.title) @@ -243,7 +254,8 @@ class TaskCog(commands.Cog): if not blocked: try: log.debug("sending '%s', exists '%s'", item.guid, result[1]) - message = await channel.send(embed=await mutated_item.to_embed(sub, feed, self.api.session)) + sendable_item = mutated_item or item + message = await channel.send(embed=await sendable_item.to_embed(sub, feed, self.api.session)) message_id = message.id except Forbidden: log.error(f"Forbidden to send to channel {channel.id}")