import aiohttp import asyncio import datetime import os import random from typing import List, Literal, Optional import discord from discord.ext import commands, tasks import pnwkit from pnwkit import Order, OrderBy from discord.utils import format_dt 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="sex") async def sex(self, ctx: CustomCtx): """ sex """ replies_rare = [ "https://media.discordapp.net/attachments/943540698492334123/1215965590037069944/QQmVi9s.png", "https://tenor.com/view/houshou-marine-sexy-treasure-box-im-your-treasure-box-ride-gif-26351095", ] replies_common = [ "# SEXOOOO", "nope", "kys", "yes", "not with u lmao", "in your dreams", "no", ] num = random.randint(1, 10) reply = random.choice(replies_common) if num <= 2: reply = replies_rare[random.randint(0, 1)] await ctx.send(reply) @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.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], "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 != "olive" 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 delta = datetime.datetime.utcnow() - nation.last_active if delta > datetime.timedelta(days=2, hours=16): 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))