This commit is contained in:
un1xx committed 2025-04-21 21:58:07 +05:30
1 parent 743f1f650a
commit 73f97cfb20
3 files changed
+43 -24

No files matched your search

+1 -1
View File
@@ -305,7 +305,7 @@ class Banking(commands.Cog):
logstream = io.StringIO() logstream = io.StringIO()
await interaction.response.defer() await interaction.response.defer()
self.label = "processing" self.label = "processing"
self.emoji = "<a:load:1275827052909432887>" self.emoji = "<a:load:1275839759192686654>"
self.style = discord.ButtonStyle.blurple self.style = discord.ButtonStyle.blurple
self.v.clear_items() self.v.clear_items()
self.v.add_item(self) self.v.add_item(self)
+29 -18
View File
@@ -118,7 +118,7 @@ class WarMonitor(commands.Cog):
@log_errors @log_errors
async def war_logger(self, war: pnwkit.War): async def war_logger(self, war: pnwkit.War):
async def build_embed(nation_id: int, color: discord.Color): async def build_embed(nation_id: int, color: discord.Color):
nation = await get_nation(nation_id) nation = await get_nation(self.kit, nation_id)
em = discord.Embed( em = discord.Embed(
title=f"{nation.nation_name} c{nation.num_cities}", title=f"{nation.nation_name} c{nation.num_cities}",
@@ -216,7 +216,7 @@ class WarMonitor(commands.Cog):
color=discord.Color.blurple(), color=discord.Color.blurple(),
timestamp=datetime.datetime.now(), timestamp=datetime.datetime.now(),
) )
attacker, defender = await get_nation([warattack.att_id, warattack.def_id]) attacker, defender = await get_nation(self.kit, [war.att_id, war.def_id])
em.add_field(name="Turns Left", value=war.turns_left) em.add_field(name="Turns Left", value=war.turns_left)
em.add_field(name="Attacker MAPs", value=war.att_points) em.add_field(name="Attacker MAPs", value=war.att_points)
em.add_field(name="Defender MAPs", value=war.def_points) em.add_field(name="Defender MAPs", value=war.def_points)
@@ -247,17 +247,19 @@ class WarMonitor(commands.Cog):
return em return em
async def calc_inrange(self, war: pnwkit.War, channel: discord.TextChannel): async def calc_inrange(self, attacker_id: int, channel: discord.TextChannel):
alliances = await self.kit.query( alliances = await self.kit.query(
"alliances", "alliances",
{"id": MONITORED_ALLIANCES}, {"id": MONITORED_ALLIANCES},
"id, name, flag, nations {id, nation_name, score, soldiers, tanks, aircraft, ships, missiles, nukes, num_cities}", "id, name, flag, nations {id, nation_name, score, soldiers, tanks, aircraft, ships, missiles, nukes, num_cities, vacation_mode_turns}",
) )
nations = alliances.alliances[0].nations + alliances.alliances[1].nations nations = alliances.alliances[0].nations + alliances.alliances[1].nations
attacker = await get_nation(war.att_id) attacker = await get_nation(self.kit, attacker_id)
score = attacker.score score = attacker.score
in_range = [] in_range = []
for nation in nations: for nation in nations:
if nation.vacation_mode_turns != 0:
continue
min_score = nation.score * 0.75 min_score = nation.score * 0.75
max_score = nation.score * 2.50 max_score = nation.score * 2.50
if min_score <= score <= max_score: if min_score <= score <= max_score:
@@ -277,14 +279,14 @@ class WarMonitor(commands.Cog):
@log_errors @log_errors
async def defense_handler(self, war: pnwkit.War): async def defense_handler(self, war: pnwkit.War):
channel = await get_war_channel(war.id) channel = await self.get_war_channel(war.id)
if channel: if channel:
await self.war_update_handler(war) await self.war_update_handler(war)
return return
if war.id not in self.monitored_wars: if war.id not in self.monitored_wars:
self.monitored_wars.append(war.id) self.monitored_wars.append(war.id)
category = await self.bot.fetch_channel(CATEGORY) category = await self.bot.fetch_channel(CATEGORY)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id]) attacker, defender = await get_nation(self.kit, [war.att_id, war.def_id])
channel = await category.create_text_channel( channel = await category.create_text_channel(
name=f"{attacker.nation_name.lower()}-{war.id}" name=f"{attacker.nation_name.lower()}-{war.id}"
) )
@@ -294,7 +296,7 @@ class WarMonitor(commands.Cog):
await channel.send(embeds=[atk_card, def_card]) await channel.send(embeds=[atk_card, def_card])
button = timeline_button(war.id) button = timeline_button(war.id)
await channel.send(embeds=[war_card], view=button) await channel.send(embeds=[war_card], view=button)
await self.calc_inrange(war, channel) await self.calc_inrange(war.id, channel)
# try to add defender discord user to the channel # try to add defender discord user to the channel
guild = await self.bot.fetch_guild(GUILD) guild = await self.bot.fetch_guild(GUILD)
member = discord.utils.get(guild.members, name=defender.discord) member = discord.utils.get(guild.members, name=defender.discord)
@@ -304,7 +306,7 @@ class WarMonitor(commands.Cog):
@log_errors @log_errors
async def war_update_handler(self, war: pnwkit.War): async def war_update_handler(self, war: pnwkit.War):
if war.id in self.monitored_wars: if war.id in self.monitored_wars:
channel = await get_war_channel(war.id) channel = await self.get_war_channel(war.id)
if not channel: if not channel:
return return
war_card = await self.build_war_card(war) war_card = await self.build_war_card(war)
@@ -317,7 +319,7 @@ class WarMonitor(commands.Cog):
await channel.send(embeds=[war_card], view=button) await channel.send(embeds=[war_card], view=button)
if war.turns_left <= 0 or war.winner_id: if war.turns_left <= 0 or war.winner_id:
if war.winner_id: if war.winner_id:
nation = await get_nation(war.winner_id) nation = await get_nation(self.kit, war.winner_id)
await channel.send( await channel.send(
f"> War has Ended." f"> War has Ended."
f" {nation.nation_name} has won the war.\n" f" {nation.nation_name} has won the war.\n"
@@ -387,7 +389,7 @@ class WarMonitor(commands.Cog):
if warattack.type.value == 12: if warattack.type.value == 12:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) fortified their nation." msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) fortified their nation."
if warattack.type.value == 13: if warattack.type.value == 13:
msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) offered/cancelled peace." msg = f"[{attacker.nation_name}](<https://politicsandwar.com/nation/id={attacker.id}>) offered/cancelled peace with [{defender.nation_name}](<https://politicsandwar.com/nation/id={defender.id}>)."
if warattack.type.value in [14, 15]: if warattack.type.value in [14, 15]:
msg = warattack.loot_info msg = warattack.loot_info
@@ -399,9 +401,9 @@ class WarMonitor(commands.Cog):
await self.log_ally_attacks(warattack) await self.log_ally_attacks(warattack)
if not warattack.war_id in self.monitored_wars: if not warattack.war_id in self.monitored_wars:
return return
war = await get_war(warattack.war_id) war = await get_war(self.kit, warattack.war_id)
channel = await get_war_channel(war.id) channel = await self.get_war_channel(war.id)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id]) attacker, defender = await get_nation(self.kit, [warattack.att_id, warattack.def_id])
if not channel: if not channel:
return return
@@ -412,9 +414,9 @@ class WarMonitor(commands.Cog):
if not warattack.att_id in REFILL_LIST: if not warattack.att_id in REFILL_LIST:
return return
channel = await self.bot.fetch_channel(ALLIANCE_ATK_CH) channel = await self.bot.fetch_channel(ALLIANCE_ATK_CH)
attacker, defender = await get_nation([warattack.att_id, warattack.def_id]) attacker, defender = await get_nation(self.kit, [warattack.att_id, warattack.def_id])
msg = self.construct_message(warattack, attacker, defender) msg = self.construct_message(warattack, attacker, defender)
button = timeline_button(war.id) button = timeline_button(warattack.war_id)
await channel.send(msg, view=button) await channel.send(msg, view=button)
@@ -423,7 +425,7 @@ class WarMonitor(commands.Cog):
@commands.has_permissions(administrator=True) @commands.has_permissions(administrator=True)
async def sync_wars(self, ctx: CustomCtx): async def sync_wars(self, ctx: CustomCtx):
# get all alliance active wars # get all alliance active wars
await ctx.send("> syncing wars...") m = await ctx.send("> <a:load:1275839759192686654> syncing wars")
wars = await self.kit.query( wars = await self.kit.query(
"wars", "wars",
{"alliance_id": MONITORED_ALLIANCES, "status": pnwkit.WarActivity.ACTIVE}, {"alliance_id": MONITORED_ALLIANCES, "status": pnwkit.WarActivity.ACTIVE},
@@ -443,6 +445,7 @@ class WarMonitor(commands.Cog):
finished_wars = finished_wars.wars finished_wars = finished_wars.wars
for war in finished_wars: for war in finished_wars:
await self.defense_handler(war) await self.defense_handler(war)
await m.edit(content="> \u2705 synced wars")
@commands.command() @commands.command()
@@ -487,8 +490,16 @@ class WarMonitor(commands.Cog):
async def monitor(self, ctx: CustomCtx, id: int): async def monitor(self, ctx: CustomCtx, id: int):
self.monitored_wars.append(id) self.monitored_wars.append(id)
await ctx.send(f"added war {id} to monitored wars") await ctx.send(f"added war {id} to monitored wars")
await self.defense_handler(await get_war(id)) await self.defense_handler(await get_war(self.kit, id))
@commands.command()
async def inrange(self, ctx: CustomCtx):
category = await self.bot.fetch_channel(CATEGORY)
if ctx.channel not in category.text_channels:
await ctx.send(f"this command only works inside {category.name}")
return
id = int(ctx.channel.name.split("-")[-1])
await self.calc_inrange(id, ctx.channel)
async def setup(bot: Brahmos): async def setup(bot: Brahmos):
await bot.add_cog(WarMonitor(bot)) await bot.add_cog(WarMonitor(bot))
+13 -5
View File
@@ -7,7 +7,7 @@ from typing import List, Union, Optional, Union
import aiohttp import aiohttp
import discord import discord
import pnwkit import pnwkit
from base import BasicView from utils.base import BasicView
async def get_webhook( async def get_webhook(
channel: Union[discord.TextChannel, discord.ForumChannel, discord.VoiceChannel] channel: Union[discord.TextChannel, discord.ForumChannel, discord.VoiceChannel]
@@ -32,7 +32,7 @@ def format_count(num):
else: else:
return num return num
def timeline_button(self, id: int): def timeline_button(id: int):
class UrlButton(discord.ui.Button): class UrlButton(discord.ui.Button):
def __init__(self, id: int): def __init__(self, id: int):
super().__init__( super().__init__(
@@ -50,10 +50,14 @@ async def get_nation(kit: pnwkit.QueryKit, id: Union[List[int], int]) -> Union[p
nation = await kit.query( nation = await kit.query(
"nations", "nations",
{"id": id}, {"id": id},
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}, discord", "nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}, discord, vacation_mode_turns",
) )
if isinstance(id, list): if isinstance(id, list):
return nation.nations result = []
result.extend(id)
for nation in nation.nations:
result[result.index(nation.id)] = nation
return result
return nation.nations[0] return nation.nations[0]
async def get_war(kit: pnwkit.QueryKit, id: Union[int, List[int]]) -> Union[pnwkit.War, List[pnwkit.War]]: async def get_war(kit: pnwkit.QueryKit, id: Union[int, List[int]]) -> Union[pnwkit.War, List[pnwkit.War]]:
@@ -63,6 +67,10 @@ async def get_war(kit: pnwkit.QueryKit, id: Union[int, List[int]]) -> Union[pnwk
"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" "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): if isinstance(id, list):
return war.wars result = []
result.extend(id)
for war in war.wars:
result[result.index(war.id)] = war
return result
return war.wars[0] return war.wars[0]