This commit is contained in:
un1xx committed 2025-04-18 17:11:15 +05:30
1 parent 0080dfd912
commit 7db6bbe839
2 files changed
+124 -33

No files matched your search

+118 -27
View File
@@ -21,7 +21,8 @@ from utils.paginator import Paginator
ALLIANCE_ID = 10177
GUILD = 943540698492334120
CATEGORY = 1017442427537215619
WAR_LOG_CHANNEL = 1359571584574029925
OFFENSIVE_LOG_CHANNEL = 1359571584574029925
DEFENSIVE_LOG_CHANNEL = 1362303245598457856
MONITORED_ALLIANCES = [10177,10198]
class WarMonitor(commands.Cog):
@@ -39,20 +40,18 @@ class WarMonitor(commands.Cog):
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
@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 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 war_logger(self, war: pnwkit.War):
# offensive wars
def url_button_view(war: pnwkit.War):
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__(
@@ -64,22 +63,42 @@ class WarMonitor(commands.Cog):
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}"
return f"{num/1000:,.1f}K"
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]
nation = await self.get_nation(nation_id)
em = discord.Embed(
title=f"{nation.nation_name} c({nation.num_cities})",
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)
@@ -94,7 +113,8 @@ class WarMonitor(commands.Cog):
)
return em
channel = await self.bot.fetch_channel(WAR_LOG_CHANNEL)
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:
@@ -102,9 +122,9 @@ class WarMonitor(commands.Cog):
embed2 = await build_embed(war.def_id, discord.Color.green())
embed2.timestamp = datetime.datetime.now()
view = url_button_view(war)
view = self.timeline_button(war)
await channel.send(embeds=[embed1, embed2], view=view, content="> WAR (OFFENSIVE)")
await off_channel.send(embeds=[embed1, embed2], view=view, content="> WAR (OFFENSIVE)")
if war.def_alliance_id in MONITORED_ALLIANCES:
@@ -112,9 +132,80 @@ class WarMonitor(commands.Cog):
embed2 = await build_embed(war.def_id, discord.Color.red())
embed2.timestamp = datetime.datetime.now()
view = url_button_view(war)
view = self.timeline_button(war)
await channel.send(embeds=[embed1, embed2], view=view, content="> WAR (DEFENSIVE)")
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:
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)
@@ -173,4 +264,4 @@ class WarMonitor(commands.Cog):
async def setup(bot: Brahmos):
await bot.add_cog(WarMonitor(bot))
+6 -6
View File
@@ -15,11 +15,11 @@ class Emojis:
coal = "<:coal:1220371877013229619>"
money = "<:dollarsym:1220378339970252841>"
credits = "<:credits:1280578712286269581>"
tanks = "<:tank:1362095745846481038>"
ships = "<:ship:1362095796610400277>"
soldiers = "<:soldiers:1362095846497452052>"
planes = "<:plane:1362095773172633630>"
nukes = "<:nuke:1362095819846844606>"
missiles = "<:missile:1362095960611881190>"
tanks = "<:tanks:1362294860077338714>"
ships = "<:ship:1362294894881800243>"
soldiers = "<:soldiers:1362294922052632707>"
planes = "<:plane:1362294883741864058>"
nukes = "<:nuke:1362294908659957840>"
missiles = "<:missile:1362294935352512612>"