Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51a40d052c | ||
|
|
15207fe11a |
No files matched your search
@@ -6,3 +6,5 @@ store
|
|||||||
*.pyo
|
*.pyo
|
||||||
*.pyd
|
*.pyd
|
||||||
.venv
|
.venv
|
||||||
|
krampus_calc.java
|
||||||
|
pyrightconfig.json
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
# Brahmos
|
||||||
|
|
||||||
|
Brahmos is a bot built using **discord.py**, designed for interacting with politics and war API.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Tech Stack
|
||||||
|
|
||||||
|
- **Language:** Python 3.11+
|
||||||
|
- **Library:** [discord.py](https://discordpy.readthedocs.io/)
|
||||||
|
- **Database (optional):** asyncpg / PostgreSQL (for persistent storage)
|
||||||
|
- **Hosting:** Oracle Cloud / Local
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### 1. Clone the repository
|
||||||
|
```bash
|
||||||
|
git clone https://git.sudobh.space/sudobh/brahmos.git
|
||||||
|
cd brahmos
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Create a virtual enviornment
|
||||||
|
```bash
|
||||||
|
uv venv --python 3.11
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Install Dependencies
|
||||||
|
```bash
|
||||||
|
uv pip install -r requirements.txt
|
||||||
|
````
|
||||||
|
|
||||||
|
### 4. Install pm2
|
||||||
|
- [Install Nodejs](https://nodejs.org/en/download)
|
||||||
|
```bash
|
||||||
|
sudo npm install -g pm2
|
||||||
|
```
|
||||||
|
|
||||||
|
### 5. Create config file for the bot
|
||||||
|
create a file named bot.config.js
|
||||||
|
```js
|
||||||
|
module.exports = {
|
||||||
|
apps : [
|
||||||
|
|
||||||
|
{
|
||||||
|
name : 'brahmos',
|
||||||
|
script: './brahmos.py',
|
||||||
|
cwd: './brahmos',
|
||||||
|
interpreter: '/home/user/brahmos/.venv/bin/python',
|
||||||
|
env:{
|
||||||
|
TOKEN: "BOT token",
|
||||||
|
pnwBOTKEY: "API KEY",
|
||||||
|
pnwAPIKEY: "API KEY",
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- replace path `/home/user/brahmos` with your system's path
|
||||||
|
- set the tokens for [discord bot](https://www.writebots.com/discord-bot-token/) and [pnw API](https://politicsandwar.com/pwpedia/article/what-is-the-api).
|
||||||
|
|
||||||
|
### 6. Start pm2 process
|
||||||
|
```bash
|
||||||
|
pm2 start bot.config.js
|
||||||
|
```
|
||||||
+2
-2
@@ -45,9 +45,9 @@ class CustomCtx(Context):
|
|||||||
else:
|
else:
|
||||||
pages.append(codeblock.format(string))
|
pages.append(codeblock.format(string))
|
||||||
string = ""
|
string = ""
|
||||||
string += p
|
string += p + '\n'
|
||||||
if string:
|
if string:
|
||||||
pages.append(string)
|
pages.append(codeblock.format(string))
|
||||||
await self.paginate(pages=pages, pagetype="text")
|
await self.paginate(pages=pages, pagetype="text")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+15
-2
@@ -199,8 +199,21 @@ class Main(commands.Cog):
|
|||||||
async def shell(self, ctx: commands.Context, *, cmd: codeblock_converter = None):
|
async def shell(self, ctx: commands.Context, *, cmd: codeblock_converter = None):
|
||||||
if cmd is None:
|
if cmd is None:
|
||||||
return
|
return
|
||||||
jskgit = bot.get_command("jsk sh")
|
jsksh = bot.get_command("jsk sh")
|
||||||
await ctx.invoke(jskgit, argument=cmd)
|
await ctx.invoke(jsksh, argument=cmd)
|
||||||
|
|
||||||
|
|
||||||
|
@commands.is_owner()
|
||||||
|
@commands.command(name="pullr")
|
||||||
|
async def pullr(self, ctx: CustomCtx):
|
||||||
|
"""
|
||||||
|
git pull and restart the bot.
|
||||||
|
"""
|
||||||
|
git = bot.get_command("jsk git")
|
||||||
|
await ctx.invoke(git, argument=codeblock_converter("pull"))
|
||||||
|
restart = bot.get_command("restart")
|
||||||
|
await ctx.invoke(restart)
|
||||||
|
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
|
|||||||
+1021
-122
File diff suppressed because it is too large.
Load diff
+91
-31
@@ -1,12 +1,15 @@
|
|||||||
import aiohttp
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
from typing import List, Literal, Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands
|
|
||||||
import pnwkit
|
import pnwkit
|
||||||
|
import pytz
|
||||||
|
from discord.ext import commands, tasks
|
||||||
|
from discord.utils import format_dt
|
||||||
from pnwkit import Order, OrderBy
|
from pnwkit import Order, OrderBy
|
||||||
|
|
||||||
from botclass import Brahmos, CustomCtx
|
from botclass import Brahmos, CustomCtx
|
||||||
@@ -27,32 +30,11 @@ class Misc(commands.Cog):
|
|||||||
bot_key_api_key=os.getenv("pnwACCKEY"),
|
bot_key_api_key=os.getenv("pnwACCKEY"),
|
||||||
aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop),
|
aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop),
|
||||||
)
|
)
|
||||||
|
self.send_daily_message.start()
|
||||||
|
|
||||||
@commands.command(name="sex")
|
def cog_unload(self):
|
||||||
async def sex(self, ctx: CustomCtx):
|
self.send_daily_message.cancel()
|
||||||
"""
|
|
||||||
sex
|
|
||||||
"""
|
|
||||||
replies_rare = [
|
|
||||||
"https://media.discordapp.net/attachments/943540698492334123/1215965590037069944/QQmVi9s.png",
|
|
||||||
"https://tenor.com/view/houshou-marine-sexy-treasure-box-im-your-treasure-box-ride-gif-26351095",
|
|
||||||
]
|
|
||||||
|
|
||||||
replies_common = [
|
|
||||||
"# SEXOOOO",
|
|
||||||
"nope",
|
|
||||||
"kys",
|
|
||||||
"yes",
|
|
||||||
"not with u lmao",
|
|
||||||
"in your dreams",
|
|
||||||
"no",
|
|
||||||
]
|
|
||||||
|
|
||||||
num = random.randint(1, 10)
|
|
||||||
reply = random.choice(replies_common)
|
|
||||||
if num <= 2:
|
|
||||||
reply = replies_rare[random.randint(0, 1)]
|
|
||||||
await ctx.send(reply)
|
|
||||||
|
|
||||||
@commands.command(name="snowflake")
|
@commands.command(name="snowflake")
|
||||||
async def snowflake(self, ctx: CustomCtx, snowflake: int):
|
async def snowflake(self, ctx: CustomCtx, snowflake: int):
|
||||||
@@ -74,24 +56,102 @@ class Misc(commands.Cog):
|
|||||||
try:
|
try:
|
||||||
result = await self.kit.query(
|
result = await self.kit.query(
|
||||||
"nations",
|
"nations",
|
||||||
{"alliance_id": id,
|
{
|
||||||
|
"alliance_id": id,
|
||||||
"orderBy": OrderBy("cities", Order.DESC),
|
"orderBy": OrderBy("cities", Order.DESC),
|
||||||
"first": 300
|
"first": 300,
|
||||||
},
|
},
|
||||||
"id, nation_name, num_cities, alliance {name}"
|
"id, nation_name, num_cities, alliance {name}",
|
||||||
)
|
)
|
||||||
string = f"Nations in alliance {result.nations[0].alliance.name}\n"
|
string = f"Nations in alliance {result.nations[0].alliance.name}\n"
|
||||||
i = 0
|
i = 0
|
||||||
for nation in result.nations:
|
for nation in result.nations:
|
||||||
i += 1
|
i += 1
|
||||||
string += f"{i:>3}. {nation.id} c{nation.num_cities} {nation.nation_name}\n"
|
string += (
|
||||||
|
f"{i:>3}. {nation.id} c{nation.num_cities} {nation.nation_name}\n"
|
||||||
|
)
|
||||||
await ctx.cbsend(string)
|
await ctx.cbsend(string)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return await ctx.send("> alliance not found.")
|
return await ctx.send("> alliance not found.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return await ctx.cbsend(e)
|
return await ctx.cbsend(e)
|
||||||
|
|
||||||
|
@commands.command(name="color")
|
||||||
|
async def color(self, ctx):
|
||||||
|
"""
|
||||||
|
return members not olive colored in the alliance
|
||||||
|
"""
|
||||||
|
res = await self.kit.query(
|
||||||
|
"nations",
|
||||||
|
{"alliance_id": [10177, 10198]},
|
||||||
|
"id, leader_name, nation_name, color, vacation_mode_turns",
|
||||||
|
)
|
||||||
|
nations = [
|
||||||
|
n for n in res.nations if n.color != "olive" and n.vacation_mode_turns == 0
|
||||||
|
]
|
||||||
|
await ctx.send(
|
||||||
|
"> nations not olive:\n"
|
||||||
|
+ "\n".join(
|
||||||
|
[
|
||||||
|
f"[{n.nation_name}](<https://politicsandwar.com/nation/id={n.id}>) [{n.leader_name}]"
|
||||||
|
for n in nations
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def seconds_until_target(self, hour: int, minute: int = 0):
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
target = now.replace(hour=hour, minute=minute, second=0, microsecond=0)
|
||||||
|
if target < now:
|
||||||
|
target += datetime.timedelta(days=1)
|
||||||
|
return (target - now).total_seconds()
|
||||||
|
|
||||||
|
@tasks.loop(hours=24)
|
||||||
|
async def send_daily_message(self):
|
||||||
|
channel = self.bot.get_channel(1356234853217075200)
|
||||||
|
res = await self.kit.query(
|
||||||
|
"nations",
|
||||||
|
{"alliance_id": [10177, 10198], "alliance_position": [2, 3, 4, 5]},
|
||||||
|
"id, leader_name, nation_name, color, vacation_mode_turns, last_active",
|
||||||
|
)
|
||||||
|
nations = [
|
||||||
|
n
|
||||||
|
for n in res.nations
|
||||||
|
if n.color not in ["olive", "beige"] and n.vacation_mode_turns == 0
|
||||||
|
]
|
||||||
|
await channel.send(
|
||||||
|
"> nations not olive:\n"
|
||||||
|
+ "\n".join(
|
||||||
|
[
|
||||||
|
f"[{n.nation_name}](<https://politicsandwar.com/nation/id={n.id}>) [{n.leader_name}]"
|
||||||
|
for n in nations
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
inactives = []
|
||||||
|
for nation in res.nations:
|
||||||
|
if nation.vacation_mode_turns != 0:
|
||||||
|
continue
|
||||||
|
utcnow = datetime.datetime.now().replace(tzinfo=pytz.utc)
|
||||||
|
delta = utcnow - nation.last_active
|
||||||
|
if delta > datetime.timedelta(days=3, hours=12):
|
||||||
|
inactives.append(nation)
|
||||||
|
if inactives:
|
||||||
|
await channel.send(
|
||||||
|
"> Inactive nations:\n"
|
||||||
|
+ "\n".join(
|
||||||
|
[
|
||||||
|
f"[{n.nation_name}](<https://politicsandwar.com/nation/id={n.id}>) [{n.leader_name}] {format_dt(n.last_active)}"
|
||||||
|
for n in inactives
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
@send_daily_message.before_loop
|
||||||
|
async def before_send_daily_message(self):
|
||||||
|
await self.bot.wait_until_ready()
|
||||||
|
seconds = self.seconds_until_target(2, 5)
|
||||||
|
await asyncio.sleep(seconds)
|
||||||
|
|
||||||
|
|
||||||
async def setup(bot: commands.Bot):
|
async def setup(bot: commands.Bot):
|
||||||
|
|||||||
+136
-70
@@ -1,29 +1,21 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
import functools
|
import functools
|
||||||
import io
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import pprint
|
|
||||||
import random
|
|
||||||
from typing import List, Literal, Optional, Union
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
import pnwkit
|
import pnwkit
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
from discord.utils import format_dt
|
|
||||||
|
|
||||||
from botclass import Brahmos, CustomCtx
|
from botclass import Brahmos, CustomCtx
|
||||||
from utils.base import BasicView
|
|
||||||
from utils.emojis import Emojis
|
from utils.emojis import Emojis
|
||||||
from utils.paginator import Paginator
|
from utils.funcs import format_count, get_nation, get_war, timeline_button
|
||||||
from utils.funcs import format_count, timeline_button, get_nation, get_war
|
|
||||||
|
|
||||||
ALLIANCE_ID = 10177
|
ALLIANCE_ID = 10177
|
||||||
GUILD = 1353930666311421952
|
GUILD = 1353930666311421952
|
||||||
CATEGORY = 1356240812861821048
|
CATEGORIES = [1356240812861821048, 1438002866177445889]
|
||||||
ARCHIVE_CATEGORY = 1367458846708334703
|
ARCHIVE_CATEGORY = 1367458846708334703
|
||||||
OFFENSIVE_LOG_CHANNEL = 1353940560771027034
|
OFFENSIVE_LOG_CHANNEL = 1353940560771027034
|
||||||
DEFENSIVE_LOG_CHANNEL = 1353940670246686843
|
DEFENSIVE_LOG_CHANNEL = 1353940670246686843
|
||||||
@@ -34,6 +26,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 +46,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
|
||||||
@@ -74,44 +68,59 @@ class WarMonitor(commands.Cog):
|
|||||||
self.monitored_wars = []
|
self.monitored_wars = []
|
||||||
self.allied_nations = []
|
self.allied_nations = []
|
||||||
|
|
||||||
|
@tasks.loop(hours=12)
|
||||||
|
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())
|
||||||
|
|
||||||
|
@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)
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
war_ids = [ch.name.split("-")[-1] for ch in category.text_channels]
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
|
war_ids = [ch.name.split("-")[-1] for ch in channels]
|
||||||
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)
|
||||||
await self.set_allied_nations()
|
await self.set_allied_nations()
|
||||||
|
self.war_check.start()
|
||||||
|
self.reload_subscriptions.start()
|
||||||
|
|
||||||
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())
|
||||||
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())
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
war_ids = [ch.name.split("-")[-1] for ch in category.text_channels]
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
|
war_ids = [ch.name.split("-")[-1] for ch in channels]
|
||||||
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:
|
||||||
@@ -128,7 +137,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):
|
||||||
@@ -161,7 +172,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()
|
||||||
@@ -169,11 +179,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()
|
||||||
@@ -181,16 +192,19 @@ 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
|
||||||
await self.defense_handler(war)
|
await self.defense_handler(war)
|
||||||
|
|
||||||
async def get_war_channel(self, id: int) -> discord.TextChannel:
|
async def get_war_channel(self, ch_id) -> discord.TextChannel | None:
|
||||||
id = str(id)
|
ch_id = str(ch_id)
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
channel = [c for c in category.text_channels if id in c.name]
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
|
channel = [c for c in channels if ch_id in c.name]
|
||||||
if channel:
|
if channel:
|
||||||
channel = channel[0]
|
channel = channel[0]
|
||||||
return channel
|
return channel
|
||||||
@@ -301,11 +315,18 @@ class WarMonitor(commands.Cog):
|
|||||||
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)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
|
ch_count = sum([len(cat.text_channels) for cat in categories])
|
||||||
attacker, defender = await get_nation(self.kit, [war.att_id, war.def_id])
|
attacker, defender = await get_nation(self.kit, [war.att_id, war.def_id])
|
||||||
|
if defender.alliance_position.name == "APPLICANT":
|
||||||
|
return
|
||||||
|
# try creating channels
|
||||||
|
pos = ch_count // 50
|
||||||
|
category = categories[pos]
|
||||||
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}"
|
||||||
)
|
)
|
||||||
|
|
||||||
atk_card = await self.build_nation_card(attacker, discord.Color.red())
|
atk_card = await self.build_nation_card(attacker, discord.Color.red())
|
||||||
def_card = await self.build_nation_card(defender, discord.Color.green())
|
def_card = await self.build_nation_card(defender, discord.Color.green())
|
||||||
war_card = await self.build_war_card(war)
|
war_card = await self.build_war_card(war)
|
||||||
@@ -314,13 +335,19 @@ class WarMonitor(commands.Cog):
|
|||||||
m = await channel.send(embeds=[war_card], view=button)
|
m = await channel.send(embeds=[war_card], view=button)
|
||||||
await self.calc_inrange(war.att_id, channel)
|
await self.calc_inrange(war.att_id, channel)
|
||||||
|
|
||||||
guild = await self.bot.fetch_guild(GUILD)
|
# adding member to the channel
|
||||||
member = discord.utils.get(guild.members, name=defender.discord)
|
guild = self.bot.get_guild(GUILD)
|
||||||
|
member = guild.get_member_named(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
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await channel.send(
|
||||||
|
f"> couldn't add {defender.leader_name} to the channel, ask them to link their discord in nation settings."
|
||||||
|
)
|
||||||
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:
|
||||||
@@ -347,18 +374,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 = (
|
||||||
@@ -416,32 +452,33 @@ 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.data.WarAttack):
|
||||||
await self.log_ally_attacks(warattack)
|
await self.log_ally_attacks(warattack)
|
||||||
if not warattack.war_id in self.monitored_wars:
|
if warattack.war_id not in self.monitored_wars:
|
||||||
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
|
||||||
msg = self.construct_message(warattack, attacker, defender)
|
msg = self.construct_message(warattack, attacker, defender)
|
||||||
await channel.send(msg)
|
await channel.send(msg)
|
||||||
|
|
||||||
async def log_ally_attacks(self, warattack: pnwkit.WarAttack):
|
async def log_ally_attacks(self, warattack: pnwkit.data.WarAttack):
|
||||||
if not warattack.att_id in self.allied_nations:
|
if warattack.att_id not 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):
|
||||||
@@ -454,7 +491,9 @@ 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:
|
||||||
@@ -468,34 +507,40 @@ class WarMonitor(commands.Cog):
|
|||||||
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_role("The Croak Council")
|
|
||||||
async def archive(self, ctx: CustomCtx):
|
async def archive(self, ctx: CustomCtx):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
if ctx.channel not in category.text_channels:
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
await ctx.send(f"this command only works inside {category.name}")
|
archive = await self.bot.fetch_channel(ARCHIVE_CATEGORY)
|
||||||
|
|
||||||
|
if ctx.channel not in channels:
|
||||||
|
await ctx.send(f"this command only works inside {archive.name}")
|
||||||
return
|
return
|
||||||
await ctx.channel.edit(category=(await self.bot.fetch_channel(ARCHIVE_CATEGORY)))
|
if len(archive.text_channels) == 50:
|
||||||
await ctx.channel.send("channel moved to archived category")
|
await ctx.send(
|
||||||
|
"> hit channel limit in archive, please delete some channels first"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
await ctx.channel.edit(category=archive)
|
||||||
|
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)
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.has_permissions(administrator=True)
|
|
||||||
@commands.has_role("The Croak Council")
|
|
||||||
async def addm(self, ctx: CustomCtx, *, member: discord.Member):
|
async def addm(self, ctx: CustomCtx, *, member: discord.Member):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
if ctx.channel not in category.text_channels:
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
|
if ctx.channel not in 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()
|
||||||
@commands.has_permissions(administrator=True)
|
|
||||||
@commands.has_role("The Croak Council")
|
|
||||||
async def delete(self, ctx: CustomCtx):
|
async def delete(self, ctx: CustomCtx):
|
||||||
category = await self.bot.fetch_channel(ARCHIVE_CATEGORY)
|
category = await self.bot.fetch_channel(ARCHIVE_CATEGORY)
|
||||||
if ctx.channel not in category.text_channels:
|
if ctx.channel not in category.text_channels:
|
||||||
@@ -503,10 +548,25 @@ class WarMonitor(commands.Cog):
|
|||||||
return
|
return
|
||||||
await ctx.channel.delete()
|
await ctx.channel.delete()
|
||||||
|
|
||||||
|
@commands.command()
|
||||||
|
@commands.has_any_role("The Croak Council", "Government", "Military Affairs [MA]")
|
||||||
|
async def delall(self, ctx: CustomCtx):
|
||||||
|
category = await self.bot.fetch_channel(ARCHIVE_CATEGORY)
|
||||||
|
if ctx.channel not in category.text_channels:
|
||||||
|
await ctx.send(f"this command only works inside {category.name}")
|
||||||
|
return
|
||||||
|
for channel in category.text_channels:
|
||||||
|
try:
|
||||||
|
await channel.delete()
|
||||||
|
except Exception as e:
|
||||||
|
await ctx.send(f"failed to delete channel: {e}")
|
||||||
|
|
||||||
|
@commands.has_any_role("The Croak Council", "Government", "Military Affairs [MA]")
|
||||||
@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)
|
||||||
|
|
||||||
|
@commands.has_any_role("The Croak Council", "Government", "Military Affairs [MA]")
|
||||||
@commands.command()
|
@commands.command()
|
||||||
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)
|
||||||
@@ -515,15 +575,21 @@ class WarMonitor(commands.Cog):
|
|||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def inrange(self, ctx: CustomCtx):
|
async def inrange(self, ctx: CustomCtx):
|
||||||
category = await self.bot.fetch_channel(CATEGORY)
|
categories = [await self.bot.fetch_channel(cat) for cat in CATEGORIES]
|
||||||
if ctx.channel not in category.text_channels:
|
channels = [ch for cat in categories for ch in cat.text_channels]
|
||||||
await ctx.send(f"this command only works inside {category.name}", delete_after=5)
|
if ctx.channel not in channels:
|
||||||
|
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))
|
||||||
+20
-11
@@ -9,8 +9,9 @@ import discord
|
|||||||
import pnwkit
|
import pnwkit
|
||||||
from utils.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],
|
||||||
) -> discord.Webhook:
|
) -> discord.Webhook:
|
||||||
"""Get a webhook created by bot in the channel"""
|
"""Get a webhook created by bot in the channel"""
|
||||||
wname = "brahmos"
|
wname = "brahmos"
|
||||||
@@ -26,12 +27,14 @@ async def get_webhook(
|
|||||||
def discord_bg_embed(desc: str = "\u200b") -> discord.Embed:
|
def discord_bg_embed(desc: str = "\u200b") -> discord.Embed:
|
||||||
return discord.Embed(color=0x36393E, description=desc)
|
return discord.Embed(color=0x36393E, description=desc)
|
||||||
|
|
||||||
|
|
||||||
def format_count(num):
|
def format_count(num):
|
||||||
if num >= 1000:
|
if num >= 1000:
|
||||||
return f"{num / 1000:,.1f}K"
|
return f"{num / 1000:,.1f}K"
|
||||||
else:
|
else:
|
||||||
return num
|
return num
|
||||||
|
|
||||||
|
|
||||||
def timeline_button(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):
|
||||||
@@ -45,26 +48,31 @@ def timeline_button(id: int):
|
|||||||
view.add_item(UrlButton(id))
|
view.add_item(UrlButton(id))
|
||||||
return view
|
return view
|
||||||
|
|
||||||
async def get_nation(kit: pnwkit.QueryKit, id: Union[List[int], int]) -> Union[pnwkit.Nation, List[pnwkit.Nation]]:
|
|
||||||
|
|
||||||
nation = await kit.query(
|
async def get_nation(
|
||||||
|
kit: pnwkit.QueryKit, id: Union[List[int], int]
|
||||||
|
) -> Union[pnwkit.data.Nation, List[pnwkit.data.Nation]]:
|
||||||
|
query_result = 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, vacation_mode_turns",
|
"nation_name, flag, num_cities, soldiers, tanks, aircraft, ships, missiles, score, nukes, id, alliance {name, id, flag}, discord, vacation_mode_turns, color, alliance_position, leader_name",
|
||||||
)
|
)
|
||||||
if isinstance(id, list):
|
if isinstance(id, list):
|
||||||
result = []
|
result = []
|
||||||
result.extend(id)
|
result.extend(id)
|
||||||
for nation in nation.nations:
|
for n in query_result.nations:
|
||||||
result[result.index(nation.id)] = nation
|
result[result.index(n.id)] = n
|
||||||
return result
|
return result
|
||||||
return nation.nations[0]
|
return query_result.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: int | List[int]
|
||||||
|
) -> pnwkit.data.War | List[pnwkit.War] | None:
|
||||||
war = await kit.query(
|
war = await kit.query(
|
||||||
"wars",
|
"wars",
|
||||||
{"id": id},
|
{"id": id, "active": False},
|
||||||
"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):
|
||||||
result = []
|
result = []
|
||||||
@@ -72,5 +80,6 @@ async def get_war(kit: pnwkit.QueryKit, id: Union[int, List[int]]) -> Union[pnwk
|
|||||||
for war in war.wars:
|
for war in war.wars:
|
||||||
result[result.index(war.id)] = war
|
result[result.index(war.id)] = war
|
||||||
return result
|
return result
|
||||||
|
if war.wars:
|
||||||
return war.wars[0]
|
return war.wars[0]
|
||||||
|
return None
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import pnwkit
|
|
||||||
|
|
||||||
|
|
||||||
class Bank:
|
|
||||||
def __init__(self, kit: pnwkit.QueryKit):
|
|
||||||
self.kit = kit
|
|
||||||
|
|
||||||
async def balance(self, id: int):
|
|
||||||
"""
|
|
||||||
receiver_type:
|
|
||||||
()
|
|
||||||
"""
|
|
||||||
resources = await self.kit.query(
|
|
||||||
"alliances",
|
|
||||||
{"id": 10177, "first": 1},
|
|
||||||
"money, food, uranium, coal, iron, oil, bauxite, iron, lead, munitions, gasoline, aluminum, steel",
|
|
||||||
)
|
|
||||||
Reference in new issue
Block a user