176 lines
5.8 KiB
Python
176 lines
5.8 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
|
|
WAR_LOG_CHANNEL = 1359571584574029925
|
|
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),
|
|
)
|
|
|
|
@commands.Cog.listener()
|
|
async def on_ready(self):
|
|
self.bot.loop.create_task(self.war_subscription_task())
|
|
|
|
async def war_subscription_task(self):
|
|
subscription = await self.kit.subscribe("war", "create")
|
|
async for war in subscription:
|
|
await self.war_logger(war)
|
|
|
|
async def war_logger(self, war: pnwkit.War):
|
|
# offensive wars
|
|
|
|
def url_button_view(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
|
|
|
|
def format_count(num):
|
|
if num >= 1000:
|
|
return f"{num/1000:,.1f}"
|
|
else:
|
|
return num
|
|
async def build_embed(nation_id: int, color: discord.Color):
|
|
nation = await self.kit.query(
|
|
"nations",
|
|
{"id": nation_id, "first": 1},
|
|
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id"
|
|
)
|
|
nation = nation.nations[0]
|
|
|
|
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
|
|
|
|
channel = await self.bot.fetch_channel(WAR_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 = url_button_view(war)
|
|
|
|
await 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 = url_button_view(war)
|
|
|
|
await channel.send(embeds=[embed1, embed2], view=view, content="> WAR (DEFENSIVE)")
|
|
|
|
|
|
# @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))
|
|
|
|
|