From 73f97cfb20eb48311339fe26c7fff4d139634829 Mon Sep 17 00:00:00 2001 From: un1xx Date: Mon, 21 Apr 2025 21:58:07 +0530 Subject: [PATCH] update --- exts/banking.py | 2 +- exts/war_monitor.py | 49 +++++++++++++++++++++++++++------------------ utils/funcs.py | 18 ++++++++++++----- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/exts/banking.py b/exts/banking.py index e0c962a..95cf482 100644 --- a/exts/banking.py +++ b/exts/banking.py @@ -305,7 +305,7 @@ class Banking(commands.Cog): logstream = io.StringIO() await interaction.response.defer() self.label = "processing" - self.emoji = "" + self.emoji = "" self.style = discord.ButtonStyle.blurple self.v.clear_items() self.v.add_item(self) diff --git a/exts/war_monitor.py b/exts/war_monitor.py index 1c4176f..9740799 100644 --- a/exts/war_monitor.py +++ b/exts/war_monitor.py @@ -118,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 get_nation(nation_id) + nation = await get_nation(self.kit, nation_id) em = discord.Embed( title=f"{nation.nation_name} c{nation.num_cities}", @@ -216,7 +216,7 @@ class WarMonitor(commands.Cog): color=discord.Color.blurple(), 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="Attacker MAPs", value=war.att_points) em.add_field(name="Defender MAPs", value=war.def_points) @@ -247,17 +247,19 @@ class WarMonitor(commands.Cog): 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", {"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 - attacker = await get_nation(war.att_id) + attacker = await get_nation(self.kit, attacker_id) score = attacker.score in_range = [] for nation in nations: + if nation.vacation_mode_turns != 0: + continue min_score = nation.score * 0.75 max_score = nation.score * 2.50 if min_score <= score <= max_score: @@ -277,14 +279,14 @@ class WarMonitor(commands.Cog): @log_errors 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: 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, 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( name=f"{attacker.nation_name.lower()}-{war.id}" ) @@ -294,7 +296,7 @@ class WarMonitor(commands.Cog): await channel.send(embeds=[atk_card, def_card]) button = timeline_button(war.id) 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 guild = await self.bot.fetch_guild(GUILD) member = discord.utils.get(guild.members, name=defender.discord) @@ -304,7 +306,7 @@ class WarMonitor(commands.Cog): @log_errors async def war_update_handler(self, war: pnwkit.War): 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: return war_card = await self.build_war_card(war) @@ -317,7 +319,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 get_nation(war.winner_id) + nation = await get_nation(self.kit, war.winner_id) await channel.send( f"> War has Ended." f" {nation.nation_name} has won the war.\n" @@ -387,7 +389,7 @@ class WarMonitor(commands.Cog): if warattack.type.value == 12: msg = f"[{attacker.nation_name}]() fortified their nation." if warattack.type.value == 13: - msg = f"[{attacker.nation_name}]() offered/cancelled peace." + msg = f"[{attacker.nation_name}]() offered/cancelled peace with [{defender.nation_name}]()." if warattack.type.value in [14, 15]: msg = warattack.loot_info @@ -399,9 +401,9 @@ class WarMonitor(commands.Cog): 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]) + war = await get_war(self.kit, warattack.war_id) + channel = await self.get_war_channel(war.id) + attacker, defender = await get_nation(self.kit, [warattack.att_id, warattack.def_id]) if not channel: return @@ -412,9 +414,9 @@ class WarMonitor(commands.Cog): 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]) + attacker, defender = await get_nation(self.kit, [warattack.att_id, warattack.def_id]) msg = self.construct_message(warattack, attacker, defender) - button = timeline_button(war.id) + button = timeline_button(warattack.war_id) await channel.send(msg, view=button) @@ -423,7 +425,7 @@ class WarMonitor(commands.Cog): @commands.has_permissions(administrator=True) async def sync_wars(self, ctx: CustomCtx): # get all alliance active wars - await ctx.send("> syncing wars...") + m = await ctx.send("> syncing wars") wars = await self.kit.query( "wars", {"alliance_id": MONITORED_ALLIANCES, "status": pnwkit.WarActivity.ACTIVE}, @@ -443,6 +445,7 @@ class WarMonitor(commands.Cog): finished_wars = finished_wars.wars for war in finished_wars: await self.defense_handler(war) + await m.edit(content="> \u2705 synced wars") @commands.command() @@ -487,8 +490,16 @@ 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 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): await bot.add_cog(WarMonitor(bot)) diff --git a/utils/funcs.py b/utils/funcs.py index 2139dee..b862c4f 100644 --- a/utils/funcs.py +++ b/utils/funcs.py @@ -7,7 +7,7 @@ from typing import List, Union, Optional, Union import aiohttp import discord import pnwkit -from base import BasicView +from utils.base import BasicView async def get_webhook( channel: Union[discord.TextChannel, discord.ForumChannel, discord.VoiceChannel] @@ -32,7 +32,7 @@ def format_count(num): else: return num -def timeline_button(self, id: int): +def timeline_button(id: int): class UrlButton(discord.ui.Button): def __init__(self, id: int): super().__init__( @@ -50,10 +50,14 @@ async def get_nation(kit: pnwkit.QueryKit, id: Union[List[int], int]) -> Union[p nation = await kit.query( "nations", {"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): - return nation.nations + result = [] + result.extend(id) + for nation in nation.nations: + result[result.index(nation.id)] = nation + return result return nation.nations[0] 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" ) 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]