273 lines
10 KiB
Python
273 lines
10 KiB
Python
import asyncio
|
|
import datetime
|
|
import io
|
|
import json
|
|
import os
|
|
import pprint
|
|
import random
|
|
from typing import List, Literal, Optional
|
|
|
|
import aiohttp
|
|
import discord
|
|
import pnwkit
|
|
from discord.ext import commands, tasks
|
|
from discord.utils import format_dt
|
|
|
|
from botclass import Brahmos, CustomCtx
|
|
from utils.base import BasicView
|
|
from utils.emojis import Emojis
|
|
from utils.paginator import Paginator
|
|
|
|
ALLIANCE_ID = 10177
|
|
GUILD = 943540698492334120
|
|
CATEGORY = 1017442427537215619
|
|
OFFENSIVE_LOG_CHANNEL = 1359571584574029925
|
|
DEFENSIVE_LOG_CHANNEL = 1362303245598457856
|
|
MONITORED_ALLIANCES = [10177,10198]
|
|
|
|
class WarMonitor(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.war_create_task = None
|
|
self.war_update_task = None
|
|
|
|
async def cog_load(self):
|
|
self.war_create_task = self.bot.loop.create_task(self.war_create_subscription())
|
|
self.war_update_task = self.bot.loop.create_task(self.war_update_subscription())
|
|
|
|
async def cog_unload(self):
|
|
self.war_create_task.cancel()
|
|
self.war_update_task.cancel()
|
|
|
|
def timeline_button(self, war: pnwkit.War):
|
|
class UrlButton(discord.ui.Button):
|
|
def __init__(self, war: pnwkit.War):
|
|
super().__init__(
|
|
label="War Timeline",
|
|
url=f"https://politicsandwar.com/nation/war/timeline/war={war.id}",
|
|
style=discord.ButtonStyle.url,
|
|
)
|
|
|
|
view = BasicView()
|
|
view.add_item(UrlButton(war))
|
|
return view
|
|
|
|
async def get_nation(self, id: int) -> pnwkit.Nation:
|
|
nation = await self.kit.query(
|
|
"nations",
|
|
{"id": id, "first": 1},
|
|
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}"
|
|
)
|
|
return nation.nations[0]
|
|
|
|
async def war_create_subscription(self):
|
|
subscription = await self.kit.subscribe("war", "create")
|
|
async for war in subscription:
|
|
await self.war_logger(war)
|
|
|
|
async def war_update_subscription(self):
|
|
subscription = await self.kit.subscribe("war", "update")
|
|
async for war in subscription:
|
|
try:
|
|
await self.defense_handler(war)
|
|
except Exception as e:
|
|
ch = await self.bot.fetch_channel(1005142844475981834)
|
|
await ch.send(e)
|
|
self.war_update_task.cancel()
|
|
|
|
async def war_logger(self, war: pnwkit.War):
|
|
|
|
def format_count(num):
|
|
if num >= 1000:
|
|
return f"{num/1000:,.1f}K"
|
|
else:
|
|
return num
|
|
async def build_embed(nation_id: int, color: discord.Color):
|
|
nation = await self.get_nation(nation_id)
|
|
|
|
em = discord.Embed(
|
|
title=f"{nation.nation_name} c{nation.num_cities}",
|
|
color=color,
|
|
url = f"https://politicsandwar.com/nation/id={nation.id}"
|
|
).set_thumbnail(url=nation.flag)
|
|
|
|
em.description = (
|
|
f"{Emojis.soldiers} {format_count(nation.soldiers)}\t\t\t"
|
|
f"{Emojis.tanks} {format_count(nation.tanks)}\n"
|
|
f"{Emojis.planes} {format_count(nation.aircraft)}\t\t\t"
|
|
f"{Emojis.ships} {format_count(nation.ships)}\n"
|
|
f"{Emojis.missiles} {format_count(nation.missiles)}\t\t\t"
|
|
f"{Emojis.nukes} {format_count(nation.nukes)}\n"
|
|
)
|
|
return em
|
|
|
|
off_channel = await self.bot.fetch_channel(OFFENSIVE_LOG_CHANNEL)
|
|
def_channel = await self.bot.fetch_channel(DEFENSIVE_LOG_CHANNEL)
|
|
|
|
if war.att_alliance_id in MONITORED_ALLIANCES:
|
|
|
|
embed1 = await build_embed(war.att_id, discord.Color.green())
|
|
embed2 = await build_embed(war.def_id, discord.Color.green())
|
|
embed2.timestamp = datetime.datetime.now()
|
|
|
|
view = self.timeline_button(war)
|
|
|
|
await off_channel.send(embeds=[embed1, embed2], view=view, content="> WAR (OFFENSIVE)")
|
|
|
|
if war.def_alliance_id in MONITORED_ALLIANCES:
|
|
|
|
embed1 = await build_embed(war.att_id, discord.Color.red())
|
|
embed2 = await build_embed(war.def_id, discord.Color.red())
|
|
embed2.timestamp = datetime.datetime.now()
|
|
|
|
view = self.timeline_button(war)
|
|
|
|
await def_channel.send(embeds=[embed1, embed2], view=view, content="> WAR (DEFENSIVE)")
|
|
|
|
# create war channel
|
|
|
|
|
|
async def get_war_channel(self, war: pnwkit.War):
|
|
category = await self.bot.fetch_channel(CATEGORY)
|
|
attacker = await self.get_nation(war.att_id)
|
|
channel = discord.utils.get(category.text_channels, name=f"{attacker.nation_name.replace(' ', '-')}-{war.id}")
|
|
return channel
|
|
|
|
async def build_nation_card(self, nation: pnwkit.Nation) -> discord.Embed:
|
|
def format_count(num):
|
|
if num >= 1000:
|
|
return f"{num/1000:,.1f}K"
|
|
else:
|
|
return num
|
|
em = discord.Embed(
|
|
title=f"{nation.nation_name}",
|
|
url=f"https://politicsandwar.com/nation/id={nation.id}",
|
|
color=discord.Color.blurple(),
|
|
timestamp=datetime.datetime.now()
|
|
)
|
|
em.set_thumbnail(url=nation.flag)
|
|
em.set_author(name=nation.alliance.name, icon_url=nation.alliance.flag, url=f"https://politicsandwar.com/alliance/id={nation.alliance.id}")
|
|
em.add_field(name="Score", value=f"{nation.score}")
|
|
em.add_field(name="Cities", value=f"{nation.num_cities}")
|
|
em.add_field(
|
|
name="Military",
|
|
value=(f"{Emojis.soldiers} {format_count(nation.soldiers)} "
|
|
f"{Emojis.tanks} {format_count(nation.tanks)} "
|
|
f"{Emojis.planes} {format_count(nation.aircraft)} "
|
|
f"{Emojis.ships} {format_count(nation.ships)} "
|
|
f"{Emojis.missiles} {format_count(nation.missiles)} "
|
|
f"{Emojis.nukes} {format_count(nation.nukes)}"
|
|
),
|
|
inline=False
|
|
)
|
|
return em
|
|
|
|
async def build_war_card(self, war: pnwkit.War) -> discord.Embed:
|
|
em = discord.Embed(
|
|
title = "War Info",
|
|
color=discord.Color.blurple(),
|
|
timestamp=datetime.datetime.now(),
|
|
)
|
|
attacker = await self.get_nation(war.att_id)
|
|
defender = await self.get_nation(war.def_id)
|
|
em.add_field(name="Turns Left", value=war.turns_left)
|
|
em.add_field(name="Attacker MAPs", value=war.att_points)
|
|
em.add_field(name="Defender MAPs", value=war.def_points)
|
|
em.add_field(name="Attacker Resistance", value=war.att_resistance)
|
|
em.add_field(name="Defender Resistance", value=war.def_resistance)
|
|
em.add_field(name="War Type", value=war.war_type)
|
|
if not war.ground_control:
|
|
value = attacker.nation_name if war.ground_control == war.att_id else defender.nation_name
|
|
em.add_field(name="Ground Control", value=value, inline=False)
|
|
if not war.air_superiority:
|
|
value = attacker.nation_name if war.air_superiority == war.att_id else defender.nation_name
|
|
em.add_field(name="Air Superiority", value=value, inline=False)
|
|
if not war.naval_blockade:
|
|
value = attacker.nation_name if war.naval_blockade == war.att_id else defender.nation_name
|
|
em.add_field(name="Naval Blockade", value=value, inline=False)
|
|
|
|
return em
|
|
|
|
async def defense_handler(self, war: pnwkit.War):
|
|
category = await self.bot.fetch_channel(CATEGORY)
|
|
attacker = await self.get_nation(war.att_id)
|
|
defender = await self.get_nation(war.def_id)
|
|
channel = await category.create_text_channel(name=f"{attacker.nation_name}-{war.id}")
|
|
atk_card = await self.build_nation_card(attacker)
|
|
def_card = await self.build_nation_card(defender)
|
|
war_card = await self.build_war_card(war)
|
|
await channel.send(embeds=[atk_card, def_card])
|
|
button = self.timeline_button(war)
|
|
await channel.send(embeds=[war_card], view=button)
|
|
|
|
|
|
# @tasks.loop(minutes=2)
|
|
# async def war_check(self):
|
|
# wars = await self.kit.query(
|
|
# "wars",
|
|
# {"alliance_id": ALLIANCE_ID},
|
|
# (
|
|
# "id,"
|
|
# "date,"
|
|
# "att_points,"
|
|
# "def_points,"
|
|
# "att_resistance,"
|
|
# "def_resistance,"
|
|
# "reason,"
|
|
# "ground_control,"
|
|
# "war_type,"
|
|
# "air_superiority,"
|
|
# "naval_blockade,"
|
|
# "turns_left,"
|
|
# "att_alliance_id,"
|
|
# "def_alliance_id,"
|
|
# "attacker {"
|
|
# "nation_name,"
|
|
# "soldiers,"
|
|
# "tanks,"
|
|
# "aircraft,"
|
|
# "ships,"
|
|
# "missiles,"
|
|
# "score,"
|
|
# "flag,"
|
|
# "nukes },"
|
|
# "defender {"
|
|
# "nation_name,"
|
|
# "soldiers,"
|
|
# "tanks,"
|
|
# "aircraft,"
|
|
# "ships,"
|
|
# "missiles,"
|
|
# "flag,"
|
|
# "score,"
|
|
# "nukes }"
|
|
# )
|
|
# )
|
|
|
|
# wars = wars.wars
|
|
# war1 = wars[0]
|
|
# guild = await self.bot.fetch_guild(GUILD)
|
|
# # category = discord.utils.get(guild.categories, id=CATEGORY)
|
|
# # if war1.attacker.nation_name in [c.name for c in category.channels]:
|
|
# # return
|
|
# # ch = await guild.create_text_channel(name=war1.attacker.nation_name, category=category)
|
|
|
|
|
|
|
|
async def setup(bot: Brahmos):
|
|
await bot.add_cog(WarMonitor(bot))
|
|
|
|
|