diff --git a/botclass.py b/botclass.py index c81df19..c35f580 100644 --- a/botclass.py +++ b/botclass.py @@ -28,6 +28,26 @@ class CustomCtx(Context): **kwargs, ): await self.Paginator(pages=pages, pagetype=pagetype).start(ctx=self, **kwargs) + + async def cbsend(self, content: Optional[str] = None, lang: Optional[str] = "py"): + """ + send content in a paginated codeblock + """ + codeblock = "```" + lang + "\n{}" + "\n```" + if not len(content) > 1200: + await self.send(content=codeblock.format(content)) + return + pages = [] + string = "" + for p in content.splitlines("\n"): + if not len(string) > 1900: + string += p + else: + pages.append(codeblock.format(string)) + string = "" + string += p + await self.paginate(pages=pages, pagetype="text") + class Brahmos(Bot): @@ -48,12 +68,13 @@ class Brahmos(Bot): return False async def setup_hook(self): + if self.db_pool is None: + self.db_pool = await create_db_pool() print(f"logged in as: {self.user.name}") await self.load_extension("jishaku") for ext in self.exts: await self.load_extension(f"exts.{ext}") - if self.db_pool is None: - self.db_pool = await create_db_pool() + async def get_context(self, message, *, cls=CustomCtx): return await super().get_context(message, cls=cls) diff --git a/exts/banking.py b/exts/banking.py index 2a52ed1..61a9de0 100644 --- a/exts/banking.py +++ b/exts/banking.py @@ -89,9 +89,11 @@ class Banking(commands.Cog): async with self.bot.db_pool.acquire() as conn: query = "SELECT id FROM warchest" rows = await conn.fetch(query) - self.FILL = [row[0] for row in rows] + self.WARCHEST_IDS = [row[0] for row in rows] + return self.WARCHEST_IDS except Exception as e: - self.WARCHEST_IDS = [] + self.WARCHEST_IDS = [] + raise e @commands.has_any_role("Government", "The Croak Council") @commands.command() @@ -292,7 +294,7 @@ class Banking(commands.Cog): f"{Emojis.aluminum} {total.get('aluminum'):,}\n" f"{Emojis.steel} {total.get('steel100'):,}" ) - embeds.append(e) + embeds.insert(0, e) class ConfirmButton(discord.ui.Button): def __init__( @@ -390,7 +392,7 @@ class Banking(commands.Cog): ctx=ctx, custom_items=[ConfirmButton(bot=self.bot, p=paginator)] ) - @commands.has_role(1353931003365691392) + @commands.has_any_role("Government", "The Croak Council") @commands.command() async def rss( self, @@ -828,6 +830,92 @@ class Banking(commands.Cog): v.message = m + @commands.has_any_role("The Croak Council", "Government") + @commands.command(aliases=["wca"]) + async def wcadd(self, ctx: CustomCtx, ids: commands.Greedy[int]): + """ + add ids to warchest. + """ + if not ids: + return await ctx.send("no ids provided") + await self.fetch_warchest_ids() + in_list = [] + notin_list = [] + for nation in ids: + if nation in self.WARCHEST_IDS: + in_list.append(nation) + else: + notin_list.append(nation) + if in_list: + await ctx.send(f"> nations already in warchest refill list : `{in_list}`") + if not notin_list: + await ctx.send("> nation ids provided are already in list.") + return + async with self.bot.db_pool.acquire() as conn: + async with conn.transaction(): + for nation in notin_list: + await conn.execute( + "INSERT INTO warchest (id) VALUES ($1)", + nation + ) + + await ctx.send(f"> inserted nations in war refill list: {notin_list}") + await ctx.invoke(discord.utils.get(self.bot.commands, name='wclist')) + + + + @commands.has_any_role("The Croak Council", "Government") + @commands.command(aliases=["wcl"]) + async def wclist(self, ctx: CustomCtx): + await self.fetch_warchest_ids() + result = await self.kit.query( + "nations", + {"id": self.WARCHEST_IDS}, + "id, nation_name" + ) + i = 0 + content = "Nations in warchest refill list\n" + for nation in result.nations: + i+=1 + content += f"{i:>3}. {nation.id} {nation.nation_name}\n" + await ctx.cbsend(content=content) + + + @commands.has_any_role("The Croak Council", "Government") + @commands.command(aliases=["wcr"]) + async def wcremove(self, ctx: commands.Context, ids: commands.Greedy[int]): + """ + Remove ids from warchest. + """ + if not ids: + return await ctx.send("No ids provided.") + await self.fetch_warchest_ids() + removed_list = [] + not_in_list = [] + + for nation in ids: + if nation in self.WARCHEST_IDS: + removed_list.append(nation) + else: + not_in_list.append(nation) + + if not_in_list: + await ctx.send(f"> Nations not found in warchest refill list: `{not_in_list}`") + + if removed_list: + async with self.bot.db_pool.acquire() as conn: + async with conn.transaction(): + for nation in removed_list: + await conn.execute( + "DELETE FROM warchest WHERE id = ($1)", + nation + ) + await ctx.send(f"> Removed nations from warchest refill list: `{removed_list}`") + await ctx.invoke(discord.utils.get(self.bot.commands, name='wclist')) + else: + await ctx.send("> None of the provided nations were in the warchest refill list.") + + async def setup(bot: Brahmos): await bot.add_cog(Banking(bot)) diff --git a/exts/misc.py b/exts/misc.py index 33b715f..895abf2 100644 --- a/exts/misc.py +++ b/exts/misc.py @@ -1,9 +1,13 @@ +import aiohttp import datetime +import os import random from typing import List, Literal, Optional import discord from discord.ext import commands +import pnwkit +from pnwkit import Order, OrderBy from botclass import Brahmos, CustomCtx @@ -11,6 +15,18 @@ from botclass import Brahmos, CustomCtx class Misc(commands.Cog): def __init__(self, bot: Brahmos): self.bot = bot + self.kit = pnwkit.QueryKit( + api_key=os.getenv("pnwAPIKEY"), + bot_key=os.getenv("pnwBOTKEY"), + bot_key_api_key=os.getenv("pnwACCKEY"), + aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop), + ) + self.dkit = pnwkit.QueryKit( + api_key=os.getenv("pnwDILFKEY"), + bot_key=os.getenv("pnwBOTKEY"), + bot_key_api_key=os.getenv("pnwACCKEY"), + aiohttp_session=aiohttp.ClientSession(loop=self.bot.loop), + ) @commands.command(name="sex") async def sex(self, ctx: CustomCtx): @@ -52,6 +68,26 @@ class Misc(commands.Cog): color=0x2F3136, ) await ctx.send(embed=embed) + + @commands.command(name="alliance") + async def alliance(self, ctx: CustomCtx, id: Optional[int] = 10177): + try: + result = await self.kit.query( + "nations", + {"alliance_id": 10177, + "orderBy": OrderBy("cities", Order.DESC) + }, + "id, nation_name, num_cities, alliance {name}" + ) + except Exception as e: + return await ctx.cbsend(e) + string = f"Nations in alliance {result.nations[0].alliance.name}\n" + i=0 + for nation in result.nations: + i+=1 + string += f"{i:>3}. {nation.id} c{nation.num_cities} {nation.nation_name}\n" + await ctx.cbsend(string) + async def setup(bot: commands.Bot):