add task to reload subscriptions every 24h

This commit is contained in:
un1xx committed 2025-08-27 21:35:35 +05:30
1 parent 4137b8bd36
commit 252c97f5b2
2 files changed
+35 -24

No files matched your search

+26 -16
View File
@@ -40,8 +40,8 @@ def log_errors(func):
try:
return await func(*args, **kwargs)
except Exception:
logger.exception(f"Error in {func.__name__}")
raise
logger.exception(f"Error in {func.__name__}")
raise
@functools.wraps(func)
def sync_wrapper(*args, **kwargs):
@@ -74,6 +74,16 @@ class WarMonitor(commands.Cog):
self.monitored_wars = []
self.allied_nations = []
@tasks.loop(hours=24)
async def reload_subscriptions(self):
self.war_create_task.cancel()
self.war_update_task.cancel()
self.war_attacks_task.cancel()
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())
self.war_attacks_task = self.bot.loop.create_task(self.warattack_subscription())
@commands.Cog.listener()
async def on_ready(self):
category = await self.bot.fetch_channel(CATEGORY)
@@ -81,7 +91,7 @@ class WarMonitor(commands.Cog):
war_ids = [int(x) for x in war_ids if x.isdigit()]
self.monitored_wars.extend(war_ids)
await self.set_allied_nations()
async def set_allied_nations(self):
"""Fetch allied nations."""
result = await self.kit.query(
@@ -94,7 +104,7 @@ class WarMonitor(commands.Cog):
self.allied_nations.extend(
[nation.id for nation in result.nations]
)
async def cog_load(self):
self.war_create_task = self.bot.loop.create_task(self.war_create_subscription())
@@ -121,7 +131,7 @@ class WarMonitor(commands.Cog):
subscription = await self.kit.subscribe("war", "update")
async for war in subscription:
await self.war_update_handler(war)
async def warattack_subscription(self):
subscription = await self.kit.subscribe("warattack", "create")
async for war in subscription:
@@ -354,7 +364,7 @@ class WarMonitor(commands.Cog):
name = " ".join(channel.name.split("-")[:-1])
await channel.edit(name=name)
self.monitored_wars.remove(war.id)
def construct_message(self, warattack: pnwkit.WarAttack, attacker: pnwkit.Nation, defender: pnwkit.Nation):
status = {
0: "Utter Failure",
@@ -415,7 +425,7 @@ class WarMonitor(commands.Cog):
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]:
msg = warattack.loot_info
return msg
@@ -423,16 +433,16 @@ class WarMonitor(commands.Cog):
async def warattack_logger(self, warattack: pnwkit.WarAttack):
await self.log_ally_attacks(warattack)
if not warattack.war_id in self.monitored_wars:
return
return
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
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 self.allied_nations:
return
@@ -442,7 +452,7 @@ class WarMonitor(commands.Cog):
button = timeline_button(warattack.war_id)
await channel.send(msg, view=button)
@commands.command()
@commands.has_role("The Croak Council")
@@ -470,7 +480,7 @@ class WarMonitor(commands.Cog):
await self.defense_handler(war)
await m.edit(content="> \u2705 synced wars")
@commands.command()
@commands.has_permissions(administrator=True)
@commands.has_role("The Croak Council")
@@ -483,7 +493,7 @@ class WarMonitor(commands.Cog):
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
await ctx.channel.edit(sync_permissions=True)
@commands.command()
@commands.has_permissions(administrator=True)
@commands.has_role("The Croak Council")
@@ -494,7 +504,7 @@ class WarMonitor(commands.Cog):
return
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}")
@commands.command()
@commands.has_permissions(administrator=True)
@commands.has_role("The Croak Council")
@@ -504,7 +514,7 @@ class WarMonitor(commands.Cog):
await ctx.send(f"this command only works inside {category.name}")
return
await ctx.channel.delete()
@commands.command()
@commands.has_permissions(administrator=True)
@commands.has_role("The Croak Council")
@@ -529,7 +539,7 @@ class WarMonitor(commands.Cog):
self.monitored_wars.append(id)
await ctx.send(f"added war {id} to monitored wars")
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)