sync wars every 1hr manually
This commit is contained in:
1 parent
7b91a941ce
commit
e7e8dfba0b
1 file changed
+66
-37
+66
-37
@@ -34,6 +34,7 @@ ALLIANCE_ATK_CH = 1367461291685318686
|
|||||||
logger = logging.getLogger("discord")
|
logger = logging.getLogger("discord")
|
||||||
# decorator to log errors for a function manually
|
# decorator to log errors for a function manually
|
||||||
|
|
||||||
|
|
||||||
def log_errors(func):
|
def log_errors(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
async def async_wrapper(*args, **kwargs):
|
async def async_wrapper(*args, **kwargs):
|
||||||
@@ -53,6 +54,7 @@ def log_errors(func):
|
|||||||
|
|
||||||
return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper
|
return async_wrapper if asyncio.iscoroutinefunction(func) else sync_wrapper
|
||||||
|
|
||||||
|
|
||||||
class WarMonitor(commands.Cog):
|
class WarMonitor(commands.Cog):
|
||||||
def __init__(self, bot: Brahmos):
|
def __init__(self, bot: Brahmos):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
@@ -84,6 +86,14 @@ class WarMonitor(commands.Cog):
|
|||||||
self.war_update_task = self.bot.loop.create_task(self.war_update_subscription())
|
self.war_update_task = self.bot.loop.create_task(self.war_update_subscription())
|
||||||
self.war_attacks_task = self.bot.loop.create_task(self.warattack_subscription())
|
self.war_attacks_task = self.bot.loop.create_task(self.warattack_subscription())
|
||||||
|
|
||||||
|
@tasks.loop(hours=1)
|
||||||
|
async def war_check(self):
|
||||||
|
channel = await self.bot.fetch_channel(1353940740803133541)
|
||||||
|
message = await channel.send(" syncing wars", delete_after=4)
|
||||||
|
ctx = await self.bot.get_context(message)
|
||||||
|
command = self.bot.get_command("sync_wars")
|
||||||
|
await ctx.invoke(command, ctx=ctx)
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
category = await self.bot.fetch_channel(CATEGORY)
|
||||||
@@ -95,16 +105,11 @@ class WarMonitor(commands.Cog):
|
|||||||
async def set_allied_nations(self):
|
async def set_allied_nations(self):
|
||||||
"""Fetch allied nations."""
|
"""Fetch allied nations."""
|
||||||
result = await self.kit.query(
|
result = await self.kit.query(
|
||||||
"nations",
|
"nations", {"alliance_id": [10177, 10198, 11965]}, "id"
|
||||||
{"alliance_id": [10177, 10198, 11965]},
|
|
||||||
"id"
|
|
||||||
)
|
)
|
||||||
if not result:
|
if not result:
|
||||||
logger.warning("Failed to fetch allied nations")
|
logger.warning("Failed to fetch allied nations")
|
||||||
self.allied_nations.extend(
|
self.allied_nations.extend([nation.id for nation in result.nations])
|
||||||
[nation.id for nation in result.nations]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def cog_load(self):
|
async def cog_load(self):
|
||||||
self.war_create_task = self.bot.loop.create_task(self.war_create_subscription())
|
self.war_create_task = self.bot.loop.create_task(self.war_create_subscription())
|
||||||
@@ -115,13 +120,11 @@ class WarMonitor(commands.Cog):
|
|||||||
war_ids = [int(x) for x in war_ids if x.isdigit()]
|
war_ids = [int(x) for x in war_ids if x.isdigit()]
|
||||||
self.monitored_wars.extend(war_ids)
|
self.monitored_wars.extend(war_ids)
|
||||||
|
|
||||||
|
|
||||||
async def cog_unload(self):
|
async def cog_unload(self):
|
||||||
self.war_create_task.cancel()
|
self.war_create_task.cancel()
|
||||||
self.war_update_task.cancel()
|
self.war_update_task.cancel()
|
||||||
self.war_attacks_task.cancel()
|
self.war_attacks_task.cancel()
|
||||||
|
|
||||||
|
|
||||||
async def war_create_subscription(self):
|
async def war_create_subscription(self):
|
||||||
subscription = await self.kit.subscribe("war", "create")
|
subscription = await self.kit.subscribe("war", "create")
|
||||||
async for war in subscription:
|
async for war in subscription:
|
||||||
@@ -138,7 +141,9 @@ class WarMonitor(commands.Cog):
|
|||||||
try:
|
try:
|
||||||
await self.warattack_logger(war)
|
await self.warattack_logger(war)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
(await self.bot.fetch_channel(1363182271951011880)).send(f"```py\n{e.__traceback__}\n```")
|
(await self.bot.fetch_channel(1363182271951011880)).send(
|
||||||
|
f"```py\n{e.__traceback__}\n```"
|
||||||
|
)
|
||||||
|
|
||||||
@log_errors
|
@log_errors
|
||||||
async def war_logger(self, war: pnwkit.War):
|
async def war_logger(self, war: pnwkit.War):
|
||||||
@@ -171,7 +176,6 @@ class WarMonitor(commands.Cog):
|
|||||||
def_channel = await self.bot.fetch_channel(DEFENSIVE_LOG_CHANNEL)
|
def_channel = await self.bot.fetch_channel(DEFENSIVE_LOG_CHANNEL)
|
||||||
|
|
||||||
if war.att_alliance_id in MONITORED_ALLIANCES:
|
if war.att_alliance_id in MONITORED_ALLIANCES:
|
||||||
|
|
||||||
embed1 = await build_embed(war.att_id, discord.Color.green())
|
embed1 = await build_embed(war.att_id, discord.Color.green())
|
||||||
embed2 = await build_embed(war.def_id, discord.Color.green())
|
embed2 = await build_embed(war.def_id, discord.Color.green())
|
||||||
embed2.timestamp = datetime.datetime.now()
|
embed2.timestamp = datetime.datetime.now()
|
||||||
@@ -179,11 +183,12 @@ class WarMonitor(commands.Cog):
|
|||||||
view = timeline_button(war.id)
|
view = timeline_button(war.id)
|
||||||
|
|
||||||
await off_channel.send(
|
await off_channel.send(
|
||||||
embeds=[embed1, embed2], view=view, content=f"> WAR ({war.war_type.name})"
|
embeds=[embed1, embed2],
|
||||||
|
view=view,
|
||||||
|
content=f"> WAR ({war.war_type.name})",
|
||||||
)
|
)
|
||||||
|
|
||||||
if war.def_alliance_id in MONITORED_ALLIANCES:
|
if war.def_alliance_id in MONITORED_ALLIANCES:
|
||||||
|
|
||||||
embed1 = await build_embed(war.att_id, discord.Color.red())
|
embed1 = await build_embed(war.att_id, discord.Color.red())
|
||||||
embed2 = await build_embed(war.def_id, discord.Color.red())
|
embed2 = await build_embed(war.def_id, discord.Color.red())
|
||||||
embed2.timestamp = datetime.datetime.now()
|
embed2.timestamp = datetime.datetime.now()
|
||||||
@@ -191,7 +196,9 @@ class WarMonitor(commands.Cog):
|
|||||||
view = timeline_button(war.id)
|
view = timeline_button(war.id)
|
||||||
|
|
||||||
await def_channel.send(
|
await def_channel.send(
|
||||||
embeds=[embed1, embed2], view=view, content=f"> WAR ({war.war_type.name})"
|
embeds=[embed1, embed2],
|
||||||
|
view=view,
|
||||||
|
content=f"> WAR ({war.war_type.name})",
|
||||||
)
|
)
|
||||||
|
|
||||||
# create war channel for defensive wars
|
# create war channel for defensive wars
|
||||||
@@ -329,10 +336,11 @@ class WarMonitor(commands.Cog):
|
|||||||
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)
|
||||||
if member:
|
if member:
|
||||||
await channel.set_permissions(member, read_messages=True, send_messages=True, view_channel=True)
|
await channel.set_permissions(
|
||||||
|
member, read_messages=True, send_messages=True, view_channel=True
|
||||||
|
)
|
||||||
await m.pin()
|
await m.pin()
|
||||||
|
|
||||||
|
|
||||||
@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:
|
||||||
@@ -359,18 +367,27 @@ class WarMonitor(commands.Cog):
|
|||||||
f"Defender infra destroyed: {war.def_infra_destroyed:,} (${war.def_infra_destroyed_value:,})\n"
|
f"Defender infra destroyed: {war.def_infra_destroyed:,} (${war.def_infra_destroyed_value:,})\n"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await channel.send("> War has ended, neither side won. Either peaced or expired.")
|
await channel.send(
|
||||||
await channel.send("-# you can move this channel to archvie by sending fr archive")
|
"> War has ended, neither side won. Either peaced or expired."
|
||||||
|
)
|
||||||
|
await channel.send(
|
||||||
|
"-# you can move this channel to archvie by sending fr archive"
|
||||||
|
)
|
||||||
name = " ".join(channel.name.split("-")[:-1])
|
name = " ".join(channel.name.split("-")[:-1])
|
||||||
await channel.edit(name=name)
|
await channel.edit(name=name)
|
||||||
self.monitored_wars.remove(war.id)
|
self.monitored_wars.remove(war.id)
|
||||||
|
|
||||||
def construct_message(self, warattack: pnwkit.WarAttack, attacker: pnwkit.Nation, defender: pnwkit.Nation):
|
def construct_message(
|
||||||
|
self,
|
||||||
|
warattack: pnwkit.WarAttack,
|
||||||
|
attacker: pnwkit.Nation,
|
||||||
|
defender: pnwkit.Nation,
|
||||||
|
):
|
||||||
status = {
|
status = {
|
||||||
0: "Utter Failure",
|
0: "Utter Failure",
|
||||||
1: "Pyrrhic Victory",
|
1: "Pyrrhic Victory",
|
||||||
2: "Moderate Success",
|
2: "Moderate Success",
|
||||||
3: "Immense Triumph"
|
3: "Immense Triumph",
|
||||||
}
|
}
|
||||||
if warattack.type.value <= 5:
|
if warattack.type.value <= 5:
|
||||||
msg = (
|
msg = (
|
||||||
@@ -428,7 +445,6 @@ class WarMonitor(commands.Cog):
|
|||||||
|
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
||||||
@log_errors
|
@log_errors
|
||||||
async def warattack_logger(self, warattack: pnwkit.WarAttack):
|
async def warattack_logger(self, warattack: pnwkit.WarAttack):
|
||||||
await self.log_ally_attacks(warattack)
|
await self.log_ally_attacks(warattack)
|
||||||
@@ -436,7 +452,9 @@ class WarMonitor(commands.Cog):
|
|||||||
return
|
return
|
||||||
war = await get_war(self.kit, warattack.war_id)
|
war = await get_war(self.kit, warattack.war_id)
|
||||||
channel = await self.get_war_channel(war.id)
|
channel = await self.get_war_channel(war.id)
|
||||||
attacker, defender = await get_nation(self.kit, [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
|
||||||
@@ -447,13 +465,13 @@ class WarMonitor(commands.Cog):
|
|||||||
if not warattack.att_id in self.allied_nations:
|
if not warattack.att_id in self.allied_nations:
|
||||||
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(self.kit, [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(warattack.war_id)
|
button = timeline_button(warattack.war_id)
|
||||||
await channel.send(msg, view=button)
|
await channel.send(msg, view=button)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.has_role("The Croak Council")
|
@commands.has_role("The Croak Council")
|
||||||
async def sync_wars(self, ctx: CustomCtx):
|
async def sync_wars(self, ctx: CustomCtx):
|
||||||
@@ -466,21 +484,22 @@ class WarMonitor(commands.Cog):
|
|||||||
)
|
)
|
||||||
wars = wars.wars
|
wars = wars.wars
|
||||||
wars = [war for war in wars if war.def_alliance_id in MONITORED_ALLIANCES]
|
wars = [war for war in wars if war.def_alliance_id in MONITORED_ALLIANCES]
|
||||||
finished_wars_id = [war for war in self.monitored_wars if war not in [w.id for w in wars]]
|
finished_wars_id = [
|
||||||
|
war for war in self.monitored_wars if war not in [w.id for w in wars]
|
||||||
|
]
|
||||||
for war in wars:
|
for war in wars:
|
||||||
await self.defense_handler(war)
|
await self.defense_handler(war)
|
||||||
if finished_wars_id:
|
if finished_wars_id:
|
||||||
finished_wars = await self.kit.query(
|
finished_wars = await self.kit.query(
|
||||||
"wars",
|
"wars",
|
||||||
{"id": finished_wars_id, "active": False},
|
{"id": finished_wars_id, "active": False},
|
||||||
"id, att_id, def_id, turns_left, att_points, def_points, att_resistance, def_resistance, ground_control, air_superiority, naval_blockade, war_type, att_alliance_id, def_alliance_id, winner_id, att_infra_destroyed, def_infra_destroyed, att_infra_destroyed_value, def_infra_destroyed_value",
|
"id, att_id, def_id, turns_left, att_points, def_points, att_resistance, def_resistance, ground_control, air_superiority, naval_blockade, war_type, att_alliance_id, def_alliance_id, winner_id, att_infra_destroyed, def_infra_destroyed, att_infra_destroyed_value, def_infra_destroyed_value",
|
||||||
)
|
)
|
||||||
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")
|
await m.edit(content="> \u2705 synced wars")
|
||||||
|
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.has_permissions(administrator=True)
|
@commands.has_permissions(administrator=True)
|
||||||
@commands.has_role("The Croak Council")
|
@commands.has_role("The Croak Council")
|
||||||
@@ -489,8 +508,12 @@ class WarMonitor(commands.Cog):
|
|||||||
if ctx.channel not in category.text_channels:
|
if ctx.channel not in category.text_channels:
|
||||||
await ctx.send(f"this command only works inside {category.name}")
|
await ctx.send(f"this command only works inside {category.name}")
|
||||||
return
|
return
|
||||||
await ctx.channel.edit(category=(await self.bot.fetch_channel(ARCHIVE_CATEGORY)))
|
await ctx.channel.edit(
|
||||||
await ctx.channel.send("> channel moved to archive, you can also do fr delete to delete the channel or fr delall to delete all channels in archive.")
|
category=(await self.bot.fetch_channel(ARCHIVE_CATEGORY))
|
||||||
|
)
|
||||||
|
await ctx.channel.send(
|
||||||
|
"> channel moved to archive, you can also do fr delete to delete the channel or fr delall to delete all channels in archive."
|
||||||
|
)
|
||||||
# sync perms to archive category
|
# sync perms to archive category
|
||||||
await ctx.channel.edit(sync_permissions=True)
|
await ctx.channel.edit(sync_permissions=True)
|
||||||
|
|
||||||
@@ -502,7 +525,9 @@ class WarMonitor(commands.Cog):
|
|||||||
if ctx.channel not in category.text_channels:
|
if ctx.channel not in category.text_channels:
|
||||||
await ctx.send(f"this command only works inside {category.name}")
|
await ctx.send(f"this command only works inside {category.name}")
|
||||||
return
|
return
|
||||||
await ctx.channel.set_permissions(member, read_messages=True, send_messages=True, view_channel=True)
|
await ctx.channel.set_permissions(
|
||||||
|
member, read_messages=True, send_messages=True, view_channel=True
|
||||||
|
)
|
||||||
await ctx.channel.send(f"added {member.mention} to {ctx.channel.name}")
|
await ctx.channel.send(f"added {member.mention} to {ctx.channel.name}")
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@@ -529,7 +554,6 @@ class WarMonitor(commands.Cog):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
await ctx.send(f"failed to delete channel: {e}")
|
await ctx.send(f"failed to delete channel: {e}")
|
||||||
|
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def monitored(self, ctx: CustomCtx):
|
async def monitored(self, ctx: CustomCtx):
|
||||||
await ctx.send(self.monitored_wars)
|
await ctx.send(self.monitored_wars)
|
||||||
@@ -544,13 +568,18 @@ class WarMonitor(commands.Cog):
|
|||||||
async def inrange(self, ctx: CustomCtx):
|
async def inrange(self, ctx: CustomCtx):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
category = await self.bot.fetch_channel(CATEGORY)
|
||||||
if ctx.channel not in category.text_channels:
|
if ctx.channel not in category.text_channels:
|
||||||
await ctx.send(f"this command only works inside {category.name}", delete_after=5)
|
await ctx.send(
|
||||||
|
f"this command only works inside {category.name}", delete_after=5
|
||||||
|
)
|
||||||
return
|
return
|
||||||
id = int(ctx.channel.name.split("-")[-1])
|
id = int(ctx.channel.name.split("-")[-1])
|
||||||
war = await get_war(self.kit, id)
|
war = await get_war(self.kit, id)
|
||||||
if not id:
|
if not id:
|
||||||
return await ctx.send("> can only use in tracked war channels with IDs in their name.")
|
return await ctx.send(
|
||||||
|
"> can only use in tracked war channels with IDs in their name."
|
||||||
|
)
|
||||||
await self.calc_inrange(war.att_id, ctx.channel)
|
await self.calc_inrange(war.att_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))
|
||||||
Reference in new issue
Block a user