diff --git a/exts/misc.py b/exts/misc.py index 150f5d6..3ee4bfa 100644 --- a/exts/misc.py +++ b/exts/misc.py @@ -1,11 +1,12 @@ import aiohttp +import asyncio import datetime import os import random from typing import List, Literal, Optional import discord -from discord.ext import commands +from discord.ext import commands, tasks import pnwkit from pnwkit import Order, OrderBy @@ -26,7 +27,11 @@ class Misc(commands.Cog): bot_key_api_key=os.getenv("pnwACCKEY"), aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop), ) + self.send_daily_message.start() + def cog_unload(self): + self.send_daily_message.cancel() + @commands.command(name="sex") async def sex(self, ctx: CustomCtx): """ @@ -104,6 +109,33 @@ class Misc(commands.Cog): await ctx.send( "> nations not olive:\n" + "\n".join([f"[{n.nation_name}]() [{n.leader_name}]" for n in nations]) ) + + def seconds_until_target(self, hour: int, minute: int = 0): + now = datetime.datetime.utcnow() + target = now.replace(hour=hour, minute=minute, second=0, microsecond=0) + if target < now: + target += datetime.timedelta(days=1) + return (target - now).total_seconds() + + @tasks.loop(hours=24) + async def send_daily_message(self): + channel = self.bot.get_channel(1356234853217075200) + res = await self.kit.query( + "nations", + {"alliance_id": [10177, 10198]}, + "id, leader_name, nation_name, color" + ) + nations = [n for n in res.nations if n.color != "olive"] + await channel.send( + "> nations not olive:\n" + "\n".join([f"[{n.nation_name}]() [{n.leader_name}]" for n in nations]) + ) + + + @send_daily_message.before_loop + async def before_send_daily_message(self): + await self.bot.wait_until_ready() + seconds = self.seconds_until_target(2, 5) + await asyncio.sleep(seconds) async def setup(bot: commands.Bot):