86 lines
2.5 KiB
Python
86 lines
2.5 KiB
Python
import asyncio
|
|
import base64
|
|
import os
|
|
from typing import List, Union, Optional, Union
|
|
|
|
|
|
import aiohttp
|
|
import discord
|
|
import pnwkit
|
|
from utils.base import BasicView
|
|
|
|
|
|
async def get_webhook(
|
|
channel: Union[discord.TextChannel, discord.ForumChannel, discord.VoiceChannel],
|
|
) -> discord.Webhook:
|
|
"""Get a webhook created by bot in the channel"""
|
|
wname = "brahmos"
|
|
hooks: List[discord.Webhook] = await channel.webhooks()
|
|
for hook in hooks:
|
|
if hook.name == wname:
|
|
return hook
|
|
# if it doesn't exist create one
|
|
hook = await channel.create_webhook(name=wname)
|
|
return hook
|
|
|
|
|
|
def discord_bg_embed(desc: str = "\u200b") -> discord.Embed:
|
|
return discord.Embed(color=0x36393E, description=desc)
|
|
|
|
|
|
def format_count(num):
|
|
if num >= 1000:
|
|
return f"{num / 1000:,.1f}K"
|
|
else:
|
|
return num
|
|
|
|
|
|
def timeline_button(id: int):
|
|
class UrlButton(discord.ui.Button):
|
|
def __init__(self, id: int):
|
|
super().__init__(
|
|
label="War Timeline",
|
|
url=f"https://politicsandwar.com/nation/war/timeline/war={id}",
|
|
style=discord.ButtonStyle.url,
|
|
)
|
|
|
|
view = BasicView()
|
|
view.add_item(UrlButton(id))
|
|
return view
|
|
|
|
|
|
async def get_nation(
|
|
kit: pnwkit.QueryKit, id: Union[List[int], int]
|
|
) -> Union[pnwkit.data.Nation, List[pnwkit.data.Nation]]:
|
|
query_result = await kit.query(
|
|
"nations",
|
|
{"id": id},
|
|
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}, discord, vacation_mode_turns, color, alliance_position, leader_name",
|
|
)
|
|
if isinstance(id, list):
|
|
result = []
|
|
result.extend(id)
|
|
for n in query_result.nations:
|
|
result[result.index(n.id)] = n
|
|
return result
|
|
return query_result.nations[0]
|
|
|
|
|
|
async def get_war(
|
|
kit: pnwkit.QueryKit, id: int | List[int]
|
|
) -> pnwkit.data.War | List[pnwkit.War] | None:
|
|
war = await kit.query(
|
|
"wars",
|
|
{"id": id, "active": False},
|
|
"att_alliance_id, def_alliance_id, id, att_id, def_id, turns_left, winner_id, war_type, att_infra_destroyed, att_infra_destroyed_value, def_infra_destroyed, def_infra_destroyed_value, air_superiority, ground_control, naval_blockade, att_points, def_points, att_resistance, def_resistance",
|
|
)
|
|
if isinstance(id, list):
|
|
result = []
|
|
result.extend(id)
|
|
for war in war.wars:
|
|
result[result.index(war.id)] = war
|
|
return result
|
|
if war.wars:
|
|
return war.wars[0]
|
|
return None
|