import asyncio import datetime import os import random from typing import Optional import aiohttp import discord import pnwkit import pytz from discord.ext import commands, tasks from discord.utils import format_dt from pnwkit import Order, OrderBy from botclass import Brahmos, CustomCtx class Misc(commands.Cog): def __init__(self, bot: Brahmos): self.bot = bot self.kit = pnwkit.QueryKit( api_key=os.getenv("pnwAPIKEY"), bot_key=os.getenv("pnwBOTKEY"), bot_key_api_key=os.getenv("pnwACCKEY"), aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop), ) self.dkit = pnwkit.QueryKit( api_key=os.getenv("pnwDILFKEY"), bot_key=os.getenv("pnwBOTKEY"), 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="snowflake") async def snowflake(self, ctx: CustomCtx, snowflake: int): """ get snowflake info """ snowflake = discord.utils.snowflake_time(snowflake) embed = discord.Embed( title="Snowflake Info", description=f"Snowflake: {snowflake}\n" f"Date: {snowflake.strftime('%Y-%m-%d %H:%M:%S')}\n" f"Timestamp: {snowflake.timestamp()}", color=0x2F3136, ) await ctx.send(embed=embed) @commands.command(name="alliance") async def alliance(self, ctx: CustomCtx, id: Optional[int] = 10177): try: result = await self.kit.query( "nations", { "alliance_id": id, "orderBy": OrderBy("cities", Order.DESC), "first": 300, }, "id, nation_name, num_cities, alliance {name}", ) string = f"Nations in alliance {result.nations[0].alliance.name}\n" i = 0 for nation in result.nations: i += 1 string += ( f"{i:>3}. {nation.id} c{nation.num_cities} {nation.nation_name}\n" ) await ctx.cbsend(string) except IndexError: return await ctx.send("> alliance not found.") except Exception as e: return await ctx.cbsend(e) @commands.command(name="color") async def color(self, ctx): """ return members not olive colored in the alliance """ res = await self.kit.query( "nations", {"alliance_id": [10177, 10198]}, "id, leader_name, nation_name, color, vacation_mode_turns", ) nations = [ n for n in res.nations if n.color != "olive" and n.vacation_mode_turns == 0 ] 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.now() 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], "alliance_position": [2, 3, 4, 5]}, "id, leader_name, nation_name, color, vacation_mode_turns, last_active", ) nations = [ n for n in res.nations if n.color not in ["olive", "beige"] and n.vacation_mode_turns == 0 ] await channel.send( "> nations not olive:\n" + "\n".join( [ f"[{n.nation_name}]() [{n.leader_name}]" for n in nations ] ) ) inactives = [] for nation in res.nations: if nation.vacation_mode_turns != 0: continue utcnow = datetime.datetime.now().replace(tzinfo=pytz.utc) delta = utcnow - nation.last_active if delta > datetime.timedelta(days=3, hours=12): inactives.append(nation) if inactives: await channel.send( "> Inactive nations:\n" + "\n".join( [ f"[{n.nation_name}]() [{n.leader_name}] {format_dt(n.last_active)}" for n in inactives ] ) ) @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): await bot.add_cog(Misc(bot))