From 9c376c3db9e30c0f0d8cf465847d1ffa745fdc1e Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Thu, 21 Dec 2023 18:17:10 +0530 Subject: question_of_the_day: add command qotd post --- pindelegate/pindelegate.py | 4 +++- question_of_the_day/question_of_the_day.py | 29 ++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pindelegate/pindelegate.py b/pindelegate/pindelegate.py index 3d3b7ea..94d5425 100644 --- a/pindelegate/pindelegate.py +++ b/pindelegate/pindelegate.py @@ -61,7 +61,9 @@ class PinDelegate(commands.Cog): Pin the replied-to message. """ if await self.is_pin_capable(ctx.channel, ctx.author.id): - await ctx.message.reference.resolved.pin(reason=f"On behalf of {ctx.author.name}") + await ctx.message.reference.resolved.pin( + reason=f"On behalf of {ctx.author.name}" + ) @commands.command() async def unpin(self, ctx): diff --git a/question_of_the_day/question_of_the_day.py b/question_of_the_day/question_of_the_day.py index 46cf1e6..029cddb 100644 --- a/question_of_the_day/question_of_the_day.py +++ b/question_of_the_day/question_of_the_day.py @@ -30,13 +30,13 @@ class QuestionOfTheDay(commands.Cog): latest_qotd_message_info={"channel_id": None, "message_id": None}, ) self.config.register_global(last_posted_qotds_at=None, guild_to_post_at={}) - self.post_qotds.start() + self.post_qotds_loop.start() async def cog_unload(self): - self.post_qotds.cancel() + self.post_qotds_loop.cancel() @tasks.loop(seconds=30) - async def post_qotds(self): + async def post_qotds_loop(self): async def post_qotds_for_time(hour, minute): try: guilds_due = (await self.config.guild_to_post_at())[ @@ -53,7 +53,7 @@ class QuestionOfTheDay(commands.Cog): f"QOTD was due for guild {guild.name} ({guild_id}) but no channel was set, so it was not posted." ) channel = await guild.fetch_channel(channel_id) - await self.post_question(channel) + await self.send_question_to_channel(channel) current_time = time.time() @@ -74,7 +74,9 @@ class QuestionOfTheDay(commands.Cog): ): await post_qotds_for_time(hour, minute) - gap_secs = current_time - (last_posted_time or current_time) + gap_secs = current_time - ( + last_posted_time if last_posted_time is not None else current_time + ) if gap_secs >= 60: # Posts may have been missed; recover them up to an hour self.logger.info(f"Detected gap of {gap_secs} seconds.") @@ -138,6 +140,19 @@ class QuestionOfTheDay(commands.Cog): except IndexError: await ctx.reply(f"Error: no question with id {question_id}.") + @qotd.command() + @checks.admin_or_permissions(manage_server=True) + async def post(self, ctx): + channel_id = await self.config.guild(ctx.guild).post_in_channel() + if channel_id: + await self.send_question_to_channel( + await ctx.guild.fetch_channel(channel_id) + ) + else: + await ctx.reply( + "Error: no channel set! Use `qotd post_here` in the intended channel first." + ) + @qotd.command() @checks.admin_or_permissions(manage_server=True) async def post_at(self, ctx, hour_after_midnight_utc: int, minute_after_hour: int): @@ -191,7 +206,7 @@ class QuestionOfTheDay(commands.Cog): @checks.admin_or_permissions(manage_server=True) async def toggle(self, ctx): """ - Turn questions of the day on or off for this server. + Turn on or off automatic posting of questions of the day in this server. """ should_be_enabled = not await self.config.guild(ctx.guild).enabled() await self.config.guild(ctx.guild).enabled.set(should_be_enabled) @@ -296,7 +311,7 @@ class QuestionOfTheDay(commands.Cog): except IndexError: await ctx.reply(f"Error: no suggestion with id {suggestion_id}.") - async def post_question(self, channel): + async def send_question_to_channel(self, channel): guild = channel.guild async with self.config.guild(guild).questions() as questions: questions_len = len(questions) -- cgit v1.2.3-57-g22cb