import asyncio import datetime import io import os import pprint import random from typing import List, Literal, Optional import aiohttp import discord import pnwkit from discord.ext import commands from botclass import Brahmos, CustomCtx from utils.base import BasicView from utils.emojis import Emojis from utils.paginator import Paginator class BaseMul: money = 1000000 food = 5000 uranium = 75 gasoline = 1000 munitions = 1200 steel40 = 1575 steel100 = 1100 aluminum = 600 @classmethod def values(cls): return { 'money': cls.money, 'food': cls.food, 'uranium': cls.uranium, 'gasoline': cls.gasoline, 'munitions': cls.munitions, 'steel40': cls.steel40, 'steel100': cls.steel100, 'aluminum': cls.aluminum } @classmethod def calc(cls, cities: int, nation: pnwkit.Nation): return { 'money': cls.money * cities - nation.money, 'food': cls.food * cities - nation.food, 'uranium': cls.uranium * cities - nation.uranium, 'gasoline': cls.gasoline * cities - nation.gasoline, 'munitions': cls.munitions * cities - nation.munitions, 'steel40': cls.steel40 * cities - nation.steel, 'steel100': cls.steel100 * cities - nation.steel, 'aluminum': cls.aluminum * cities - nation.aluminum } class Banking(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), ) @commands.has_role(987770837627187250) @commands.command() async def tmilf(self, ctx: CustomCtx): """ transfer resources from MILF to DILF """ # fetch resources resources = await self.kit.query( "alliances", {"id": 10177, "first": 1}, "money, food, uranium, coal, iron, oil, bauxite, iron, lead, munitions, gasoline, aluminum, steel", ) resources = resources.alliances[0] e = discord.Embed( color=discord.Color.yellow(), timestamp=datetime.datetime.utcnow(), title="Approve Transaction", ) e.description = ( "Transferring resources to **Damn I Love Frogs**\n" f"{Emojis.money} {resources.money:,}\n" f"{Emojis.food} {resources.food:,}\n" f"{Emojis.uranium} {resources.uranium:,}\n" f"{Emojis.coal} {resources.coal:,}\n" f"{Emojis.iron} {resources.iron:,}\n" f"{Emojis.oil} {resources.oil:,}\n" f"{Emojis.bauxite} {resources.bauxite:,}\n" f"{Emojis.lead} {resources.lead:,}\n" f"{Emojis.munitions} {resources.munitions:,}\n" f"{Emojis.gasoline} {resources.gasoline:,}\n" f"{Emojis.aluminum} {resources.aluminum:,}\n" f"{Emojis.steel} {resources.steel:,}" ) class View(BasicView): def __init__( self, timeout: float | None = 60, clear: bool | None = False, kit: pnwkit.QueryKit = None, ): super().__init__(timeout, clear) self.kit = kit @discord.ui.button(label="cancel", style=discord.ButtonStyle.red) async def cancel(self, i: discord.Interaction, _): await i.message.edit( content="> transaction cancelled", embed=None, view=None ) self.stop() @discord.ui.button(label="confirm", style=discord.ButtonStyle.green) async def confirm(self, i: discord.Interaction, _): e.title = "Transaction Approved" e.description = e.description.replace("Transferring", "Transferred") e.color = discord.Color.green() await self.kit.mutation( "bankWithdraw", { "receiver": 10198, "receiver_type": 2, "money": resources.money, "food": resources.food, "uranium": resources.uranium, "coal": resources.coal, "iron": resources.iron, "oil": resources.oil, "bauxite": resources.bauxite, "lead": resources.lead, "munitions": resources.munitions, "gasoline": resources.gasoline, "aluminum": resources.aluminum, "steel": resources.steel, }, "id", ) await i.message.edit(embed=e, view=None) self.stop() await ctx.send(embed=e, view=View(kit=self.kit)) @commands.has_role(987770837627187250) @commands.command() async def warchest(self, ctx: CustomCtx): """ refill warchest of all members """ data = await self.kit.query( "alliances", {"id": 10177, "first": 1}, "nations {id, nation_name, discord, money, food, steel, gasoline, munitions, aluminum, uranium, cities {name}}" ) alliance = data.alliances[0] nations = alliance.nations resource_delta = [] for nation in nations: cities = len(nation.cities) delta = BaseMul.calc(cities, nation) delta.update({ 'id': nation.id, 'discord': nation.discord or "N/A", 'cities': cities, 'name': nation.nation_name }) for key in delta: if isinstance(delta[key], float): delta[key] = int(delta[key]) resource_delta.append(delta) embeds = [] e = discord.Embed(title="Warchest Resources") count = 0 for rd in resource_delta: e.add_field( name=rd.get('name'), value= ( f"[{rd.get('discord')}](https://politicsandwar.com/nation/id={rd.get('id')}) (c{rd.get('cities')})\n\n" f"{Emojis.money} {rd.get('money'):,}\n" f"{Emojis.food} {rd.get('food'):,}\n" f"{Emojis.uranium} {rd.get('uranium'):,}\n" f"{Emojis.munitions} {rd.get('munitions'):,}\n" f"{Emojis.gasoline} {rd.get('gasoline'):,}\n" f"{Emojis.aluminum} {rd.get('aluminum'):,}\n" f"{Emojis.steel} {rd.get('steel100'):,}" ) ) count += 1 if count == 9: embeds.append(e) e = discord.Embed(title="Warchest Resources") count = 0 if count > 0: embeds.append(e) class ConfirmButton(discord.ui.Button): def __init__(self, *, style = discord.ButtonStyle.green, label= "confirm", bot: Brahmos, p: Paginator): super.__init__(style=style, label=label) self.bot = bot self.v = p async def interaction_check(self, interaction: discord.Interaction) -> bool: return interaction.user == ctx.author async def callback(self, interaction: discord.Interaction) -> io.Any: kit = 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) ) logmsg = "" logstream = io.StringIO await interaction.response.defer() self.label = "processing" self.emoji = "" self.style= discord.ButtonStyle.blurple self.v.clear_items() self.v.add_item(self) e = discord.Embed(color=discord.Color.blurple, timestamp=datetime.datetime.utcnow(), title="Transferring resources") await interaction.message.edit(embed=e, view=v) for rd in resource_delta: for k in rd: # remove negatives if (isinstance(rd[k]), int) and (rd[k] < 0): rd[k] = 0 try: await kit.mutation( "bankWithdraw", { "receiver" : rd.get("id"), "receiver_type": 1, "money": rd.get("money"), "food": rd.get("food"), "uranium": rd.get("uranium"), "gasoline": rd.get("gasoline"), "steel": rd.get("steel00"), "munitions": rd.get("munitions"), "aluminum": rd.get("aluminum"), "note": "warchest" }, "id" ) pprint.pprint(rd, stream=logstream, indent=3) logmsg += f"{rd.get("name")} <:tick:1275824967522717707>\n" e.description = logmsg await interaction.message.edit(embed=e) await asyncio.sleep(1) except Exception as e: pprint.pprint(f"{rd.get('name')} -> {e}", stream=logstream, indent=3) logmsg += f"{rd.get("name")} <:cross:1275824989131505779>\n" e.description = logmsg await interaction.message.edit(embed=e) await asyncio.sleep(1) await interaction.message.edit(view=None) await ctx.send(file=discord.File(logstream, filename="log.txt")) paginator = ctx.Paginator(pages=embeds) paginator.start(ctx=ctx, custom_items=[ConfirmButton(bot=self.bot, p=paginator)]) async def setup(bot: Brahmos): await bot.add_cog(Banking(bot))