sbsheriff/app/modals.py

293 lines
15 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from datetime import datetime
from typing import Text
import disnake
from disnake import TextInputStyle
import helpers
import json
import embed_factory
import time
import requests
class LLModal(disnake.ui.Modal):
def __init__(self,cp,pike=0,archer=0):
components = [
disnake.ui.TextInput(
label="Enter CP Rank if number below is wrong",
placeholder="Last CP was: {cp}".format(cp=cp),
custom_id="cp",
style=TextInputStyle.short,
max_length=5,
required=False,
value = cp,
),
disnake.ui.TextInput(
label="Villa ID's seperate with space",
placeholder="1234 4352 54234 ...etc",
custom_id="villa_ids",
style=TextInputStyle.short,
),
disnake.ui.TextInput(
label="How many Archers?",
placeholder="200",
custom_id="archers",
value=pike,
style=TextInputStyle.short,
),
disnake.ui.TextInput(
label="How many Pikes?",
placeholder="300",
custom_id="pikes",
value=archer,
style=TextInputStyle.short,
),
]
super().__init__(
title="Liege Lord Request Form",
custom_id="submit_ll",
components=components
)
async def callback(self,inter: disnake.ModalInteraction):
await inter.response.defer(ephemeral=True)
playerCP = inter.text_values['cp']
prefArchers = inter.text_values['archers']
prefPikes = inter.text_values['pikes']
print("{player} is cp {cp} and input: {villaids}".format(player=inter.author.display_name,cp=playerCP,villaids=inter.text_values['villa_ids']))
helpers.sql_set("INSERT INTO user(dID,d_name,cp,gID,prefArchers,prefPikes) VALUES(?,?,?,?,?,?) ON CONFLICT(dID) DO UPDATE SET cp = excluded.cp,prefArchers = excluded.prefArchers,prefPikes = excluded.prefPikes",(inter.author.id,inter.author.display_name,playerCP,inter.guild_id,prefArchers,prefPikes))
result = json.loads(helpers.sql_get("SELECT advert_data FROM guild WHERE gID = {gID}".format(gID = inter.guild.id))[0][0])
if 'll' not in result.keys():
await inter.response.send_message(content="Failed, no thread open", ephemeral=True)
return
thread = inter.guild.get_channel_or_thread(result['ll'][1])
if ',' in inter.text_values['villa_ids']:
villa_ids = [villa for villa in inter.text_values['villa_ids'].split(',')]
else:
villa_ids = [villa for villa in inter.text_values['villa_ids'].split()]
if len(villa_ids):
for villa in villa_ids:
button_done = disnake.ui.Button(emoji='',custom_id='llcheck')
#button_done.callback = sbComponents.ll_button_callback
embed = disnake.Embed(title="LL Request - {id} from {name} CP: {cp}".format(id=villa,cp=playerCP,name=inter.author.display_name))
embed.description = "{name} is looking for a LL for {id}, if you fulfill this request, or have gotten a LL click the ✅ below to remove it from the list".format(name=inter.author.display_name,id=villa)
embed.add_field(name="Archers",value=prefArchers,inline=True)
embed.add_field(name="Pikes",value=prefPikes,inline=True)
view = disnake.ui.View()
view.add_item(button_done)
msg = await thread.send(embed=embed,view=view)
helpers.sql_set("INSERT INTO llrequests(parentID,villaID,ownerID,ownerCP,fulfilled,messageID,displayName) VALUES(?,?,?,?,?,?,?)",(thread.id,villa,inter.author.id,playerCP,0,msg.id,inter.author.display_name))
channel = inter.guild.get_channel_or_thread(result['ll'][0])
message = await channel.fetch_message(result['ll'][1])
button_r_ll = disnake.ui.Button(emoji='<:LiegeLord:1003401484295213066>',custom_id='request_ll',label="Click here to request a Liege Lord")
embed_view = disnake.ui.View()
embed_view.add_item(button_r_ll)
await message.edit(embed=embed_factory.lfll(result['ll'][1]),view=embed_view)
await channel.send(content="Request has been added!",delete_after=30)
await inter.followup.send(content="Done! Should be listed here: <#{id}>".format(id=channel.id))
return
class timesModal(disnake.ui.Modal):
def __init__(self,multi=4):
components = [
disnake.ui.TextInput(
label="name|hh:mm:ss or hh:mm:ss can add multiple",
placeholder="Times to convert",
custom_id="times",
required=True,
),
disnake.ui.TextInput(
label="Time multiplier",
placeholder="4",
custom_id="multi_value",
style= TextInputStyle.short,
max_length=1,
value = multi,
),
]
super().__init__(
title="Calculate attack times",
custom_id="submit_calc",
components=components
)
async def callback(self, interaction: disnake.ModalInteraction):
await interaction.response.defer()
times = interaction.text_values['times']
multi = int(interaction.text_values['multi_value'])
#do tests on times:
response = helpers.calc_times(times,multi)
view = disnake.ui.View()
add_to_attack = disnake.ui.Button(emoji='<:Captain_blue:947543163608924181>',custom_id='add_to_attack',label='Copy to timed attack')
view.add_item(add_to_attack)
message:disnake.Message = await interaction.followup.send(view = view,content=response[0],delete_after=540)
for time in response[1]:
helpers.sql_set("INSERT INTO times(dID,mID,gID,seconds,name,multi,modifier) VALUES(?,?,?,?,?,?,?)",(interaction.author.id,message.id,interaction.guild_id,time[1],time[0],time[2],time[3]))
return
################################################
# Remember to mirror changes in the main!! #
################################################
class timed_add(disnake.ui.Modal):
def __init__(self):
components = [
disnake.ui.TextInput(
label="name|hh:mm:ss or hh:mm:ss can add multiple",
placeholder="Times to add",
custom_id="times",
required=True,
),
disnake.ui.TextInput(
label="Time multiplier",
value=1,
custom_id="multi_value",
style= TextInputStyle.short,
max_length=1,
),
disnake.ui.TextInput(
label="Help",
style=TextInputStyle.paragraph,
placeholder="Attacks named Breaker or starts with a > will have 10 seconds removed from them",
custom_id="help_line",
required=False,
),
disnake.ui.TextInput(
label=" ",
style=TextInputStyle.paragraph,
placeholder="Changing time multi alters time entered",
custom_id="help_line2",
required=False,
),
]
super().__init__(
title="Add times to attack",
custom_id="submit_timed_add",
components=components
)
async def callback(self, interaction: disnake.ModalInteraction):
await interaction.response.defer(ephemeral=True)
current_time = int(time.time())
times = interaction.text_values['times']
multi = int(interaction.text_values['multi_value'])
#do tests on times:
response = helpers.calc_times(times,multi)
for atk_time in response[1]:
helpers.sql_set("INSERT INTO times(dID,gID,seconds,name,multi,modifier,target_mID,mID) VALUES(?,?,?,?,?,?,?,0)",(interaction.author.id,interaction.guild_id,atk_time[1],atk_time[0],atk_time[2],atk_time[3],interaction.message.id))
helpers.sql_set("UPDATE attack_targets SET update_time = {cur_time} WHERE mID = {mID}".format(cur_time = current_time,mID = interaction.message.id))
await interaction.followup.send(content="done",delete_after=1)
return
#class add_attack_modal(disnake.ui.Modal):
# def __init__(self,target):
# return
class add_vm(disnake.ui.Modal):
def __init__(self):
components = [
disnake.ui.TextInput(
label="Name",
placeholder="Check spelling!",
custom_id="user",
required=True,
),
]
super().__init__(
title="Report VM",
custom_id="add_vm",
components=components
)
async def callback(self, interaction: disnake.ModalInteraction):
await interaction.response.defer(ephemeral=True)
user = interaction.text_values['user']
shield = requests.get('https://login.strongholdkingdoms.com/ajaxphp/get_shield_url.php?username={user}&transparent=1'.format(user=user))
if 'error' in shield.json().keys():
await interaction.response.send_message("Unknown player are you sure you typed correctly? - "+user)
return
else:
query_response = helpers.sql_get("SELECT * FROM vm_entries WHERE name = '{user}' AND finished IS NULL".format(user=user),True)
if len(query_response) > 0:
await interaction.response.send_message("This player is already marked as on VM")
return
#TODO addbutton
helpers.sql_set("INSERT INTO vm_entries(gID,name,added) VALUES(?,?,?)",(interaction.guild_id,user,int(time.time())))
advert_result = json.loads(helpers.sql_get("SELECT advert_data FROM guild WHERE gID = {gID}".format(gID = interaction.guild.id))[0][0])
channel = interaction.guild.get_channel_or_thread(advert_result['vm'][0])
message = await channel.fetch_message(advert_result['vm'][1])
button_add_vm = disnake.ui.Button(emoji='',custom_id='add_vm',label="Report VM")
button_rem_vm = disnake.ui.Button(emoji="",custom_id="rem_vm",label="Remove VM")
embed_view = disnake.ui.View()
embed_view.add_item(button_add_vm)
embed_view.add_item(button_rem_vm)
await message.edit(view=embed_view,embed=embed_factory.vm_advert(interaction.guild_id))
await interaction.followup.send(ephemeral=True,content="{user} added to list".format(user=user))
class rem_vm(disnake.ui.Modal):
def __init__(self,candidates1:list,candidates2:list):
components = [
disnake.ui.TextInput(
placeholder="Check spelling!",
custom_id="user",
required=True,
label="Name"
),
]
super().__init__(
title="Remove VM",
custom_id="rem_vm",
components=components
)
async def callback(self, interaction: disnake.ModalInteraction):
await interaction.response.defer(ephemeral=True)
user = interaction.text_values['user']
query_result = helpers.sql_get("SELECT name,added FROM vm_entries WHERE name = '{user}' AND gID = {gID} AND finished IS NULL".format(user=user,gID=interaction.guild_id))
if len(query_result) == 0:
await interaction.followup.send(ephemeral=True,content="Not found or already ended")
return
helpers.sql_set("UPDATE vm_entries SET finished = ? WHERE gID = ? AND name = ? AND added = ?",(int(time.time()),interaction.guild_id,query_result[0][0],query_result[0][1]))
advert_result = json.loads(helpers.sql_get("SELECT advert_data FROM guild WHERE gID = {gID}".format(gID = interaction.guild.id))[0][0])
channel = interaction.guild.get_channel_or_thread(advert_result['vm'][0])
message = await channel.fetch_message(advert_result['vm'][1])
button_add_vm = disnake.ui.Button(emoji='',custom_id='add_vm',label="Report VM")
button_rem_vm = disnake.ui.Button(emoji='',custom_id="rem_vm",label="Remove VM")
embed_view = disnake.ui.View()
embed_view.add_item(button_add_vm)
embed_view.add_item(button_rem_vm)
await message.edit(view=embed_view,embed=embed_factory.vm_advert(interaction.guild_id))
await interaction.followup.send(ephemeral=True,content="{user} removed from the list".format(user=user))
class capture_castle(disnake.ui.Modal):
def __init__(self,user,filename):
components = [
disnake.ui.TextInput(
placeholder="Who's castle is this?",
custom_id="user",
required=True,
label="Castle Owner",
value=user,
style=TextInputStyle.multi_line
),
disnake.ui.TextInput(
placeholder="If you see this abort",
custom_id="image_id",
required=True,
label="Don't Touch!",
value=filename
),
]
super().__init__(
title="Capture Castle",
custom_id="capture_castle",
components=components
)
async def callback(self, interaction: disnake.ModalInteraction):
await interaction.response.defer(ephemeral=True)
shield = requests.get('https://login.strongholdkingdoms.com/ajaxphp/get_shield_url.php?username={user}&transparent=1'.format(user=interaction.text_values['user']))
if 'error' in shield.json().keys():
await interaction.response.send_message("Unknown player are you sure you typed correctly? - "+interaction.text_values['user'])
return
query_response=helpers.sql_get("SELECT castle_designs FROM players WHERE gID = {gID} AND LOWER(name) = LOWER('{user}')".format(gID = interaction.guild_id,user=interaction.text_values['user']))
if len(query_response) > 0:
json_object = json.loads(query_response[0][0])
else:
json_object = {}
json_object[interaction.text_values['image_id']] = interaction.text_values['image_id']
helpers.sql_set("INSERT INTO players(gID,name,castle_designs) VALUES(?,?,?) ON CONFLICT(name) DO UPDATE SET castle_designs = excluded.castle_designs",(interaction.guild_id,interaction.text_values['user'].lower(),json.dumps(json_object)))
await interaction.followup.send(ephemeral=True,content="Nom nom, thats {num} images for {user}".format(user=interaction.text_values['user'],num=len(json_object)))