add allied attack log

This commit is contained in:
un1xx committed 2025-04-21 20:34:22 +05:30
1 parent c1b6544962
commit 0361571b27
2 files changed
+138 -121

No files matched your search

+99 -118
View File
@@ -7,7 +7,7 @@ import logging
import os
import pprint
import random
from typing import List, Literal, Optional
from typing import List, Literal, Optional, Union
import aiohttp
import discord
@@ -19,7 +19,7 @@ from botclass import Brahmos, CustomCtx
from utils.base import BasicView
from utils.emojis import Emojis
from utils.paginator import Paginator
from utils.funcs import format_count
from utils.funcs import format_count, timeline_button, get_nation, get_war
ALLIANCE_ID = 10177
GUILD = 943540698492334120
@@ -28,6 +28,9 @@ ARCHIVE_CATEGORY = 1363161502969892874
OFFENSIVE_LOG_CHANNEL = 1359571584574029925
DEFENSIVE_LOG_CHANNEL = 1362303245598457856
MONITORED_ALLIANCES = [10177, 10198]
ALLIANCE_ATK_CH = 1363882443605541127
REFILL_LIST = json.load(open("refill.json"))
logger = logging.getLogger("discord")
# decorator to log errors for a function manually
@@ -93,36 +96,6 @@ class WarMonitor(commands.Cog):
self.war_update_task.cancel()
self.war_attacks_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}, discord",
)
return nation.nations[0]
async def get_war(self, id: int) -> pnwkit.War:
war = await self.kit.query(
"wars",
{"id": id},
"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"
)
war = war.wars[0]
return war
async def war_create_subscription(self):
subscription = await self.kit.subscribe("war", "create")
@@ -145,7 +118,7 @@ class WarMonitor(commands.Cog):
@log_errors
async def war_logger(self, war: pnwkit.War):
async def build_embed(nation_id: int, color: discord.Color):
nation = await self.get_nation(nation_id)
nation = await get_nation(nation_id)
em = discord.Embed(
title=f"{nation.nation_name} c{nation.num_cities}",
@@ -177,7 +150,7 @@ class WarMonitor(commands.Cog):
embed2 = await build_embed(war.def_id, discord.Color.green())
embed2.timestamp = datetime.datetime.now()
view = self.timeline_button(war)
view = timeline_button(war.id)
await off_channel.send(
embeds=[embed1, embed2], view=view, content=f"> WAR ({war.war_type.name})"
@@ -189,7 +162,7 @@ class WarMonitor(commands.Cog):
embed2 = await build_embed(war.def_id, discord.Color.red())
embed2.timestamp = datetime.datetime.now()
view = self.timeline_button(war)
view = timeline_button(war.id)
await def_channel.send(
embeds=[embed1, embed2], view=view, content=f"> WAR ({war.war_type.name})"
@@ -198,10 +171,10 @@ class WarMonitor(commands.Cog):
# create war channel for defensive wars
await self.defense_handler(war)
async def get_war_channel(self, war: pnwkit.War) -> discord.TextChannel:
async def get_war_channel(self, id: int) -> discord.TextChannel:
id = str(id)
category = await self.bot.fetch_channel(CATEGORY)
attacker = await self.get_nation(war.att_id)
channel = [c for c in category.text_channels if str(war.id) in c.name]
channel = [c for c in category.text_channels if id in c.name]
if channel:
channel = channel[0]
return channel
@@ -243,8 +216,7 @@ class WarMonitor(commands.Cog):
color=discord.Color.blurple(),
timestamp=datetime.datetime.now(),
)
attacker = await self.get_nation(war.att_id)
defender = await self.get_nation(war.def_id)
attacker, defender = await get_nation([warattack.att_id, warattack.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)
@@ -282,7 +254,7 @@ class WarMonitor(commands.Cog):
"id, name, flag, nations {id, nation_name, score, soldiers, tanks, aircraft, ships, missiles, nukes, num_cities}",
)
nations = alliances.alliances[0].nations + alliances.alliances[1].nations
attacker = await self.get_nation(war.att_id)
attacker = await get_nation(war.att_id)
score = attacker.score
in_range = []
for nation in nations:
@@ -305,15 +277,14 @@ class WarMonitor(commands.Cog):
@log_errors
async def defense_handler(self, war: pnwkit.War):
channel = await self.get_war_channel(war)
channel = await get_war_channel(war.id)
if channel:
await self.war_update_handler(war)
return
if war.id not in self.monitored_wars:
self.monitored_wars.append(war.id)
category = await self.bot.fetch_channel(CATEGORY)
attacker = await self.get_nation(war.att_id)
defender = await self.get_nation(war.def_id)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id])
channel = await category.create_text_channel(
name=f"{attacker.nation_name.lower()}-{war.id}"
)
@@ -321,7 +292,7 @@ class WarMonitor(commands.Cog):
def_card = await self.build_nation_card(defender, discord.Color.green())
war_card = await self.build_war_card(war)
await channel.send(embeds=[atk_card, def_card])
button = self.timeline_button(war)
button = timeline_button(war.id)
await channel.send(embeds=[war_card], view=button)
await self.calc_inrange(war, channel)
# try to add defender discord user to the channel
@@ -333,11 +304,11 @@ class WarMonitor(commands.Cog):
@log_errors
async def war_update_handler(self, war: pnwkit.War):
if war.id in self.monitored_wars:
channel = await self.get_war_channel(war)
channel = await get_war_channel(war.id)
if not channel:
return
war_card = await self.build_war_card(war)
button = self.timeline_button(war)
button = timeline_button(war.id)
# get last message in channel
async for message in channel.history(limit=1):
if message.author == self.bot.user and message.embeds:
@@ -346,7 +317,7 @@ class WarMonitor(commands.Cog):
await channel.send(embeds=[war_card], view=button)
if war.turns_left <= 0 or war.winner_id:
if war.winner_id:
nation = await self.get_nation(war.winner_id)
nation = await get_nation(war.winner_id)
await channel.send(
f"> War has Ended."
f" {nation.nation_name} has won the war.\n"
@@ -360,83 +331,93 @@ class WarMonitor(commands.Cog):
await channel.edit(name=name)
self.monitored_wars.remove(war.id)
@log_errors
async def warattack_logger(self, warattack: pnwkit.WarAttack):
if not warattack.war_id in self.monitored_wars:
return
war = await self.get_war(warattack.war_id)
channel = await self.get_war_channel(war)
attacker = await self.get_nation(warattack.att_id)
defender = await self.get_nation(warattack.def_id)
if not channel:
return
def construct_message(self, warattack: pnwkit.WarAttack, attacker: pnwkit.Nation, defender: pnwkit.Nation):
status = {
0: "Utter Failure",
1: "Pyrrhic Victory",
2: "Moderate Success",
3: "Immense Triumph"
}
if warattack.type.value <= 5:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) has ordered airstrike upon the nation of [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" The attack was {status[warattack.success]}."
f" {attacker.nation_name}'s forces lost {warattack.att_aircraft_lost:,} aircraft, while {defender.nation_name}'s defenders lost {warattack.def_aircraft_lost:,} aircraft."
f" The attack destroyed {warattack.infra_destroyed:,} infrastructure"
)
if warattack.type.value == 0:
msg += ". This was an airstrike done on infra."
if warattack.type.value == 1:
msg += f" and killed {warattack.def_soldiers_lost:,} soldiers."
if warattack.type.value == 2:
msg += f" and destroyed {warattack.def_tanks_lost:,} tanks."
if warattack.type.value == 3:
msg += f". This airstrike was done on money, and destroyed {warattack.money_destroyed:,} money."
if warattack.type.value == 4:
msg += f" and destroyed {warattack.def_ships_lost:,} ships."
if warattack.type.value == 5:
msg += "."
if warattack.type.value == 6:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) has ordered ground battle on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" The attack was {status[warattack.success]}."
f" {attacker.nation_name}'s forces lost {warattack.att_soldiers_lost:,} soldiers and {warattack.att_tanks_lost:,} tanks,"
f" {defender.nation_name}'s defenders lost {warattack.def_soldiers_lost:,} soldiers and {warattack.def_tanks_lost:,} tanks."
)
if warattack.def_aircraft_lost:
msg += f" The attack also destroyed {warattack.def_aircraft_lost:,} aircraft."
msg += f" {attacker.nation_name} stole ${warattack.money_stolen:,} money from the attack and also destroyed {warattack.infra_destroyed:,} infrastructure."
if warattack.type.value in [7, 9]:
projectiles = {7: "missile", 9: "nuke"}
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) launched a {projectiles[warattack.type.value]} on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
msg += f"{attacker.nation_name} destroyed {warattack.infra_destroyed:,} infrastructure"
if warattack.improvements_lost:
msg += f" and {warattack.improvements_lost} improvements: {','.join(warattack.improvements_destroyed)}."
else:
msg += "."
if warattack.type.value in [8, 10]:
projectiles = {8: "missile", 10: "nuke"}
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) launched a {projectiles[warattack.type.value]} on {defender.nation_name}, the attack failed."
if warattack.type.value == 11:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) ordered a naval battle on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" {attacker.nation_name}'s forces lost {warattack.att_ships_lost} ships, while {defender.nation_name}'s defenders lost {warattack.def_ships_lost} ships."
f" The attack destroyed {warattack.infra_destroyed:,} infrastructure."
)
if warattack.type.value == 12:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) fortified their nation."
if warattack.type.value == 13:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) offered/cancelled peace."
if warattack.type.value in [14, 15]:
msg = warattack.loot_info
def construct_message():
msg = None
if warattack.type.value <= 5:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) has ordered airstrike upon the nation of [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" The attack was {status[warattack.success]}."
f" {attacker.nation_name}'s forces lost {warattack.att_aircraft_lost:,} aircraft, while {defender.nation_name}'s defenders lost {warattack.def_aircraft_lost:,} aircraft."
f" The attack destroyed {warattack.infra_destroyed:,} infrastructure"
)
if warattack.type.value == 0:
msg += ". This was an airstrike done on infra."
if warattack.type.value == 1:
msg += f" and killed {warattack.def_soldiers_lost:,} soldiers."
if warattack.type.value == 2:
msg += f" and destroyed {warattack.def_tanks_lost:,} tanks."
if warattack.type.value == 3:
msg += f". This airstrike was done on money, and destroyed {warattack.money_destroyed:,} money."
if warattack.type.value == 4:
msg += f" and destroyed {warattack.def_ships_lost:,} ships."
if warattack.type.value == 5:
msg += "."
if warattack.type.value == 6:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) has ordered ground battle on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" The attack was {status[warattack.success]}."
f" {attacker.nation_name}'s forces lost {warattack.att_soldiers_lost:,} soldiers and {warattack.att_tanks_lost:,} tanks,"
f" {defender.nation_name}'s defenders lost {warattack.def_soldiers_lost:,} soldiers and {warattack.def_tanks_lost:,} tanks."
)
if warattack.def_aircraft_lost:
msg += f" The attack also destroyed {warattack.def_aircraft_lost:,} aircraft."
msg += f" {attacker.nation_name} stole ${warattack.money_stolen:,} money from the attack and also destroyed {warattack.infra_destroyed:,} infrastructure."
if warattack.type.value in [7, 9]:
projectiles = {7: "missile", 9: "nuke"}
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) launched a {projectiles[warattack.type.value]} on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
msg += f"{attacker.nation_name} destroyed {warattack.infra_destroyed:,} infrastructure"
if warattack.improvements_lost:
msg += f" and {warattack.improvements_lost} improvements: {','.join(warattack.improvements_destroyed)}."
else:
msg += "."
if warattack.type.value in [8, 10]:
projectiles = {8: "missile", 10: "nuke"}
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) launched a {projectiles[warattack.type.value]} on {defender.nation_name}, the attack failed."
if warattack.type.value == 11:
msg = (
f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) ordered a naval battle on [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>) and eliminated {warattack.resistance_lost} resistance."
f" {attacker.nation_name}'s forces lost {warattack.att_ships_lost} ships, while {defender.nation_name}'s defenders lost {warattack.def_ships_lost} ships."
f" The attack destroyed {warattack.infra_destroyed:,} infrastructure."
)
if warattack.type.value == 12:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) fortified their nation."
if warattack.type.value == 13:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) offered/cancelled peace."
if warattack.type.value in [14, 15]:
msg = warattack.loot_info
return msg
return msg
await channel.send(construct_message())
@log_errors
async def warattack_logger(self, warattack: pnwkit.WarAttack):
await self.log_ally_attacks(warattack)
if not warattack.war_id in self.monitored_wars:
return
war = await get_war(warattack.war_id)
channel = await get_war_channel(war.id)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id])
if not channel:
return
msg = self.construct_message(warattack, attacker, defender)
await channel.send(msg)
async def log_ally_attacks(self, warattack: pnwkit.WarAttack):
if not warattack.att_id in REFILL_LIST:
return
channel = await self.bot.fetch_channel(ALLIANCE_ATK_CH)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id])
msg = self.construct_message(warattack, attacker, defender)
button = timeline_button(war.id)
await channel.send(msg, view=button)
@commands.command()
@commands.has_permissions(administrator=True)
@@ -506,7 +487,7 @@ class WarMonitor(commands.Cog):
async def monitor(self, ctx: CustomCtx, id: int):
self.monitored_wars.append(id)
await ctx.send(f"added war {id} to monitored wars")
await self.defense_handler(await self.get_war(id))
await self.defense_handler(await get_war(id))
async def setup(bot: Brahmos):
+39 -3
View File
@@ -1,11 +1,12 @@
import asyncio
import base64
import os
from typing import List, Union
from typing import List, Union, Optional, Union
import aiohttp
import discord
import pnwkit
async def get_webhook(
channel: Union[discord.TextChannel, discord.ForumChannel, discord.VoiceChannel]
@@ -28,4 +29,39 @@ def format_count(num):
if num >= 1000:
return f"{num/1000:,.1f}K"
else:
return num
return num
def timeline_button(self, 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(id: Union[List[int], int]) -> Union[pnwkit.Nation, List[pnwkit.Nation]]:
nation = await self.kit.query(
"nations",
{"id": id},
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}, discord",
)
if isinstance(id, list):
return nation.nations
return nation.nations[0]
async def get_war(id: Union[int, List[int]]) -> Union[pnwkit.War, List[pnwkit.War]]:
war = await self.kit.query(
"wars",
{"id": id},
"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):
return war.wars
return war.wars[0]