update transfer cmd

This commit is contained in:
un1xx committed 2024-10-10 20:06:07 +05:30
1 parent 91188e48f1
commit 9608585578
1 file changed
+236 -97
+236 -97
View File
@@ -330,7 +330,7 @@ class Banking(commands.Cog):
{
"receiver": rd.get("id"),
"receiver_type": 1,
"money": rd.get("money") + 1,
"money": rd.get("money"),
"food": rd.get("food"),
"uranium": rd.get("uranium"),
"gasoline": rd.get("gasoline"),
@@ -487,33 +487,46 @@ class Banking(commands.Cog):
]:
return await ctx.send("-# You are not in list of authorized users")
alliances = {10177: "Man I Love Frogs", 10198: "Damn I Love Frogs"}
m_flag = (await self.kit.query(
"alliances", {"id": 10177, "first": 1}, "flag"
)).alliances[0].flag
d_flag = (await self.dkit.query(
"alliances", {"id": 10198, "first": 1}, "flag"
)).alliances[0].flag
e = discord.Embed(
timestamp=datetime.datetime.now(),
title="Transfer Resources",
color=ctx.author.color,
)
e.add_field(name="Transferring From", value=alliances.get(10177))
sel_alliance = 10177
e.set_thumbnail(url=m_flag)
class TransferView(BasicView):
def __init__(self, timeout: float | None = 60, clear: bool | None = False):
def __init__(self, timeout: float | None = 60, clear: bool | None = False, kit: pnwkit.QueryKit = None, dkit: pnwkit.QueryKit = None):
super().__init__(timeout=timeout, clear=clear)
self.alliance_id = None
self.nation_id = None
self.resources = None
self.kit = kit
self.dkit = dkit
self.sel_alliance = 10177
@discord.ui.button(
label="change alliance", style=discord.ButtonStyle.blurple
label="change selected alliance", style=discord.ButtonStyle.gray
)
async def change_alliance(self, i: discord.Interaction, _):
nonlocal sel_alliance
sel_alliance = 10198 if sel_alliance == 10177 else 10177
self.sel_alliance = 10198 if self.sel_alliance == 10177 else 10177
flag = d_flag if self.sel_alliance == 10198 else m_flag
e.set_thumbnail(url=flag)
e.set_field_at(
0, name="Transferring From", value=alliances.get(sel_alliance)
0, name="Transferring From", value=alliances.get(self.sel_alliance)
)
e.remove_field(1)
await i.message.edit(embed=e)
await i.response.defer()
@discord.ui.button(
label="transfer to nation", style=discord.ButtonStyle.blurple
label="transfer to nation", style=discord.ButtonStyle.blurple, row=1
)
async def transfer_to_nation(self, i: discord.Interaction, _):
class NationModal(discord.ui.Modal, title="Nation Details"):
@@ -524,106 +537,232 @@ class Banking(commands.Cog):
max_length=32,
required=True,
)
money = discord.ui.TextInput(
label="Money",
style=discord.TextStyle.short,
max_length=15,
placeholder="money",
required=False,
)
food = discord.ui.TextInput(
label="Food",
style=discord.TextStyle.short,
max_length=15,
placeholder="food",
required=False,
)
uranium = discord.ui.TextInput(
label="Uranium",
style=discord.TextStyle.short,
max_length=15,
placeholder="uranium",
required=False,
)
coal = discord.ui.TextInput(
label="Coal",
style=discord.TextStyle.short,
max_length=15,
placeholder="coal",
required=False,
)
iron = discord.ui.TextInput(
label="Iron",
style=discord.TextStyle.short,
max_length=15,
placeholder="iron",
required=False,
)
oil = discord.ui.TextInput(
label="Oil",
style=discord.TextStyle.short,
max_length=15,
placeholder="oil",
required=False,
)
bauxite = discord.ui.TextInput(
label="Bauxite",
style=discord.TextStyle.short,
max_length=15,
placeholder="bauxite",
required=False,
)
lead = discord.ui.TextInput(
label="Lead",
style=discord.TextStyle.short,
max_length=15,
placeholder="lead",
required=False,
)
munitions = discord.ui.TextInput(
label="Munitions",
style=discord.TextStyle.short,
max_length=15,
placeholder="munitions",
required=False,
)
gasoline = discord.ui.TextInput(
label="Gasoline",
style=discord.TextStyle.short,
max_length=15,
placeholder="gasoline",
required=False,
)
aluminum = discord.ui.TextInput(
label="Aluminum",
style=discord.TextStyle.short,
max_length=15,
placeholder="aluminum",
required=False,
)
steel = discord.ui.TextInput(
label="Steel",
style=discord.TextStyle.short,
max_length=15,
placeholder="steel",
required=False,
rss = discord.ui.TextInput(
label="Resources",
style=discord.TextStyle.long,
max_length=1024,
placeholder=(
"money: 1000\n"
"food: 1000\n"
"... and so on"
),
required=True,
)
def __init__(self, view: TransferView):
super().__init__(timeout=60)
def __init__(self, view: TransferView, kit: pnwkit.QueryKit):
super().__init__(timeout=120)
self.view = view
self.kit = kit
async def on_submit(self, interaction: discord.Interaction):
async def on_submit(self, i: discord.Interaction):
await i.response.defer()
e = i.message.embeds[0]
e.description = ...
inp = self.nation.value
if inp.isdigit():
nation = await self.kit.query(
"nations",
{"id": int(inp), "first": 1},
"id, nation_name, leader_name, flag"
)
else:
nation = await self.kit.query(
"nations",
{"nation_name": inp, "first": 1},
"id, nation_name, leader_name, flag"
)
nation = nation.nations[0]
rss = self.rss.value.split("\n")
resources = dict()
for r in rss:
key, value = r.split(":")
resources[key.strip()] = int(value.strip())
e.description = (
"Send resources to: \n"
f"[{nation.nation_name}](https://politicsandwar.com/nation/id={nation.id})\n"
f"Leader: {nation.leader_name}\n\n"
f"{Emojis.money} {resources.get('money', 0):,}\n"
f"{Emojis.food} {resources.get('food', 0):,}\n"
f"{Emojis.uranium} {resources.get('uranium', 0):,}\n"
f"{Emojis.coal} {resources.get('coal', 0):,}\n"
f"{Emojis.iron} {resources.get('iron', 0):,}\n"
f"{Emojis.oil} {resources.get('oil', 0):,}\n"
f"{Emojis.bauxite} {resources.get('bauxite', 0):,}\n"
f"{Emojis.lead} {resources.get('lead', 0):,}\n"
f"{Emojis.munitions} {resources.get('munitions', 0):,}\n"
f"{Emojis.gasoline} {resources.get('gasoline', 0):,}\n"
f"{Emojis.aluminum} {resources.get('aluminum', 0):,}\n"
f"{Emojis.steel} {resources.get('steel', 0):,}\n"
)
e.set_image(url=nation.flag)
self.view.nation_id = nation.id
self.view.resources = {
"money" : resources.get('money', 0),
"food" : resources.get('food', 0),
"uranium" : resources.get('uranium', 0),
"coal" : resources.get('coal', 0),
"iron" : resources.get('iron', 0),
"oil" : resources.get('oil', 0),
"bauxite" : resources.get('bauxite', 0),
"lead" : resources.get('lead', 0),
"munitions" : resources.get('munitions', 0),
"gasoline" : resources.get('gasoline', 0),
"aluminum" : resources.get('aluminum', 0),
"steel" : resources.get('steel', 0),
}
self.view.confirm.disabled = False
self.view.transfer_to_nation.disabled = True
self.view.transfer_to_alliance.disabled = True
await i.message.edit(embed=e, view=self.view)
await i.response.send_modal(NationModal(self, self.kit))
@discord.ui.button(label="transfer to alliance", style=discord.ButtonStyle.blurple, row=1)
async def transfer_to_alliance(self, i: discord.Interaction, _):
class AllianceModal(discord.ui.Modal, title="Alliance Details"):
def __init__(self, view: TransferView, kit: pnwkit.QueryKit):
super().__init__(timeout=120)
self.view = view
self.kit = kit
alliance = discord.ui.TextInput(
label="Alliance ID/name",
style=discord.TextStyle.short,
placeholder="alliance id or name",
max_length=32,
required=True,
)
rss = discord.ui.TextInput(
label="Resources",
style=discord.TextStyle.long,
max_length=1024,
placeholder=(
"money: 1000\n"
"food: 1000\n"
"... and so on"
),
required=True,
)
@discord.ui.button(label="cancel", style=discord.ButtonStyle.red)
async def on_submit(self, i: discord.Interaction):
await i.response.defer()
e = i.message.embeds[0]
inp = self.alliance.value
if inp.isdigit():
alliance = await self.kit.query(
"alliances",
{"id": int(inp), "first": 1},
"id, name, flag"
)
else:
alliance = await self.kit.query(
"alliances",
{"name": inp, "first": 1},
"id, name, flag"
)
alliance = alliance.alliances[0]
rss = self.rss.value.split("\n")
resources = dict()
for r in rss:
key, value = r.split(":")
resources[key.strip()] = int(value.strip())
e.description = (
"Send resources to: \n"
f"[{alliance.name}](https://politicsandwar.com/allaince/id={alliance.id})\n\n"
f"{Emojis.money} {resources.get('money', 0):,}\n"
f"{Emojis.food} {resources.get('food', 0):,}\n"
f"{Emojis.uranium} {resources.get('uranium', 0):,}\n"
f"{Emojis.coal} {resources.get('coal', 0):,}\n"
f"{Emojis.iron} {resources.get('iron', 0):,}\n"
f"{Emojis.oil} {resources.get('oil', 0):,}\n"
f"{Emojis.bauxite} {resources.get('bauxite', 0):,}\n"
f"{Emojis.lead} {resources.get('lead', 0):,}\n"
f"{Emojis.munitions} {resources.get('munitions', 0):,}\n"
f"{Emojis.gasoline} {resources.get('gasoline', 0):,}\n"
f"{Emojis.aluminum} {resources.get('aluminum', 0):,}\n"
f"{Emojis.steel} {resources.get('steel', 0):,}\n"
)
e.set_image(url=alliance.flag)
self.view.alliance_id = alliance.id
self.view.resources = {
"money" : resources.get('money', 0),
"food" : resources.get('food', 0),
"uranium" : resources.get('uranium', 0),
"coal" : resources.get('coal', 0),
"iron" : resources.get('iron', 0),
"oil" : resources.get('oil', 0),
"bauxite" : resources.get('bauxite', 0),
"lead" : resources.get('lead', 0),
"munitions" : resources.get('munitions', 0),
"gasoline" : resources.get('gasoline', 0),
"aluminum" : resources.get('aluminum', 0),
"steel" : resources.get('steel', 0),
}
self.view.confirm.disabled = False
self.view.transfer_to_nation.disabled = True
self.view.transfer_to_alliance.disabled = True
await i.message.edit(embed=e, view=self.view)
await i.response.send_modal(AllianceModal(self, self.kit))
@discord.ui.button(label="confirm", style=discord.ButtonStyle.blurple, disabled=True, row=2)
async def confirm(self, i: discord.Interaction, _):
kit = self.kit if self.sel_alliance == 10177 else self.dkit
if self.nation_id:
await kit.mutation(
"bankWithdraw",
{
"receiver": self.nation_id,
"receiver_type": 1,
**self.resources,
},
"id",
)
url = f"https://politicsandwar.com/nation/id={self.nation_id}"
elif self.alliance_id:
await kit.mutation(
"bankWithdraw",
{
"receiver": self.alliance_id,
"receiver_type": 2,
**self.resources,
},
"id",
)
url = f"https://politicsandwar.com/alliance/id={self.alliance_id}"
e = i.message.embeds[0]
e.title = "Transaction Completed"
e.color = discord.Color.green()
e.description = (
"Resources have been transferred successfully\n"
f"Target => [link]({url})\n\n"
f"{Emojis.money} {self.resources.get('money'):,}\n"
f"{Emojis.food} {self.resources.get('food'):,}\n"
f"{Emojis.uranium} {self.resources.get('uranium'):,}\n"
f"{Emojis.coal} {self.resources.get('coal'):,}\n"
f"{Emojis.iron} {self.resources.get('iron'):,}\n"
f"{Emojis.oil} {self.resources.get('oil'):,}\n"
f"{Emojis.bauxite} {self.resources.get('bauxite'):,}\n"
f"{Emojis.lead} {self.resources.get('lead'):,}\n"
f"{Emojis.munitions} {self.resources.get('munitions'):,}\n"
f"{Emojis.gasoline} {self.resources.get('gasoline'):,}\n"
f"{Emojis.aluminum} {self.resources.get('aluminum'):,}\n"
f"{Emojis.steel} {self.resources.get('steel'):,}\n"
)
await i.message.edit(embed=e, view=None)
await i.response.defer()
@discord.ui.button(label="cancel", style=discord.ButtonStyle.red, row=2)
async def cancel(self, i: discord.Interaction, _):
await i.message.edit(
content="> transaction cancelled", embed=None, view=None
)
self.stop()
await ctx.send(embed=e, view=TransferView(kit=self.kit, dkit=self.dkit))
async def setup(bot: Brahmos):
await bot.add_cog(Banking(bot))