add cmd tp update offshore key

This commit is contained in:
un1xx committed 2025-11-04 23:36:55 +05:30
1 parent ae8d1708c4
commit 75dee03687
3 files changed
+160 -31

No files matched your search

+116 -4
View File
@@ -5,6 +5,7 @@ import json
import os
import pprint
from sqlite3 import Date
from typing import List, Optional
import aiohttp
@@ -15,7 +16,7 @@ from discord.utils import format_dt
from numerize_denumerize import denumerize
import math
from pnwkit.data import Alliance
from pnwkit.data import Alliance, ApiKeyDetails
from botclass import Brahmos, CustomCtx
from utils.base import BasicView
@@ -81,7 +82,7 @@ class TransferFlags(commands.FlagConverter, delimiter=" ", prefix="--"):
class Banking(commands.Cog):
def __init__(self, bot: Brahmos):
async def __init__(self, bot: Brahmos):
self.bot = bot
self.kit = pnwkit.QueryKit(
api_key=os.getenv("pnwAPIKEY"),
@@ -96,17 +97,21 @@ class Banking(commands.Cog):
aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop),
)
self.okit = pnwkit.QueryKit(
api_key=os.getenv("pnwOFFKEY"),
api_key=await self.get_offshore_key(),
bot_key=os.getenv("pnwBOTKEY"),
bot_key_api_key=os.getenv("pnwACCKEY"),
aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop),
)
self.WARCHEST_IDS = []
async def get_kit_alliance(self, kit: pnwkit.QueryKit) -> pnwkit.data.Alliance:
async def get_kit_alliance(
self, kit: pnwkit.QueryKit, me: bool = False
) -> pnwkit.data.Alliance | ApiKeyDetails:
res = await kit.query(
"me", dict(), "nation {nation_name, id, flag, alliance {name, id, flag}}"
)
if me:
return res.me
return res.me.nation.alliance
async def cog_load(self):
@@ -125,6 +130,113 @@ class Banking(commands.Cog):
self.WARCHEST_IDS = []
raise e
async def set_offshore(self, key: str):
"""Set offshore key"""
try:
async with self.bot.db_pool.acquire() as conn:
await conn.execute("UPDATE offshore SET key = ($1)", key)
except Exception as e:
raise e
async def get_offshore_key(self):
"""get offshore key"""
try:
async with self.bot.db_pool.acquire() as conn:
query = "SELECT key FROM offshorekey"
row = await conn.fetchrow(query)
key = row["key"]
return key
except Exception as e:
raise e
@commands.command()
async def offkey(self, ctx: CustomCtx):
"""set offshore key"""
if ctx.author.id not in [
367740727624466433,
322156710590676993,
407135783678640128,
444896160105103361,
]:
return await ctx.send("-# You are not in list of authorized users")
# view adding key
class View(BasicView):
def __init__(
self,
timeout: float | None = 60,
clear: bool | None = False,
bank: Banking = None,
):
super().__init__(timeout, clear)
self.bank = bank
@discord.ui.button(label="cancel", style=discord.ButtonStyle.red)
async def cancel(self, i: discord.Interaction, _):
await i.message.edit(
content="> action cancelled", embed=None, view=None
)
self.stop()
@discord.ui.button(label="set key", style=discord.ButtonStyle.green)
async def confirm(self, i: discord.Interaction, _):
class TextInput(discord.ui.Modal, title="Enter API Key"):
key = discord.ui.TextInput(
label="Nation ID/name",
style=discord.TextStyle.short,
placeholder="api key",
max_length=32,
required=True,
)
def __init__(self, bank: Banking = None):
super().__init__(timeout=200)
self.bank = bank
async def on_submit(self, i: discord.Interaction):
await i.response.defer()
embed = i.message.embeds[0]
key = self.key.value
try:
kit = pnwkit.QueryKit(
api_key=key,
bot_key=os.getenv("pnwBOTKEY"),
bot_key_api_key=os.getenv("pnwACCKEY"),
aiohttp_session=aiohttp.ClientSession(
loop=self.bank.bot.loop
),
)
data: ApiKeyDetails = await self.bank.get_kit_alliance(
kit, me=True
)
embed.description = "offshore key updated"
embed.add_field(
name="nation",
value=f"{data.nation.nation_name} ({data.nation.id})",
)
embed.add_field(
name="alliance",
value=f"{data.nation.alliance.name}({data.nation.alliance.id})",
)
embed.colour = discord.Color.green()
await self.bank.set_offshore(key=key)
await i.message.edit(embed=embed, view=None)
except Exception:
embed.color = discord.Color.red()
embed.description = "invalid key entered."
await i.message.edit(embed=embed, view=None)
await i.response.send_modal(TextInput(bank=self.bank))
embed = discord.Embed(
description="Key updation menu",
color=0x2D3236,
timestamp=datetime.datetime.now(),
)
view = View(bank=self)
await ctx.send(embed=embed, view=view)
@commands.has_any_role("Government", "The Croak Council")
@commands.command()
async def tmilf(self, ctx: CustomCtx):