stormbrigade_sheriff/cogs/relationship_manager.py

75 lines
4.1 KiB
Python

from disnake.ext import commands
import disnake
from main import StronkBot
from loguru import logger
import httpx
from thefuzz import process
from datetime import datetime
class Relationship_Manager(commands.Cog):
def __init__(self,bot:StronkBot):
self.bot = bot
self.houses = ["House 1 - The Heavenly Ones","House 2 - High Castle","House 3 - The Dragons","House 4 - The Farmers","House 5 - The Navigators","House 6 - The Free Folk","House 7 - The Royals","House 8 - The Roses","House 9 - The Rams","House 10 - Fighters","House 11 - Heroes","House 12 - Stags","House 13 - Oak","House 14 - Beasts","House 15 - The Pure","House 16 - Lionheart","House 17 - The Insane","House 18 - Sinners","House 19 - The Double-Eagles","House 20 - Maidens",]
@commands.slash_command()
async def set_relationship(self,inter:disnake.ApplicationCommandInteraction,
target_type:str = commands.Param(choices=["House", "Player"]),
relation:str = commands.Param(choices=["Faction", "Allied", "Neutral", "Peace", "Enemy", "Rogue"]),
world:str = commands.Param(description="What world does this relation apply? Leave blank to affect all worlds", default = None),
target:str = commands.Param(description="Type the name of the target of this relation change"),
comment:str = commands.Param(default="",description="If you want you can add some info about this relationship for clarity. Might not be used anywhere")):
#Validate target name
if target_type == "Player":
async with httpx.AsyncClient() as client:
response = await client.get(f'http://login.strongholdkingdoms.com/ajaxphp/username_search.php?term={target}')
if target not in response.json(): return await inter.send(ephemeral=True, content=f"You submitted {target} this name does not seem valid. Make sure the name has the right capitalization. Or the player is banned")
await self.bot.database.players.update_one({"in_game_name": target},{"$set": {"in_game_name": target}},upsert=True)
if target_type == "House":
if target not in self.houses: return await inter.send(ephemeral=True,content=f"You submitted {target} this name does not seem valid. Make sure to pick from the list")
target = target.partition(" - ")[0]
document = {
"guild_id": inter.guild_id,
"world": world,
"target": target,
"type": target_type,
"state": relation,
"comment": comment,
"author": {
"discord_id" : inter.author.id,
"discord_name" : inter.author.display_name,
"date": datetime.now()
}
}
#Upsert the correct document to the relationship collection
await self.bot.database.relationships.update_one({"guild_id": inter.guild_id, "type": target_type, "target": target}, {"$set": document},upsert=True)
await inter.send(ephemeral=True,content=f"Done! Set {target} as {relation}!\nNote that this does not update adverts, any changes will be updated during their scheduled updates.")
@set_relationship.autocomplete("target")
async def autocomp_input(self, inter:disnake.ApplicationCommandInteraction,user_input:str):
if len(user_input) == 0: return []
target_type = inter.filled_options.get("target_type")
if target_type is None: return ["Please set a target type first"]
if target_type == "Player":
async with httpx.AsyncClient() as client:
response = await client.get(f'http://login.strongholdkingdoms.com/ajaxphp/username_search.php?term={user_input}')
return response.json()
if target_type == "House":
return [option[0] for option in process.extract(user_input,self.houses,limit=4)]
@set_relationship.autocomplete("world")
async def autocomp_world(self, inter:disnake.ApplicationCommandInteraction,user_input:str):
server_document = await self.bot.database.servers.find_one({"_id":inter.guild_id})
return server_document["worlds"].keys()
def setup(bot):
logger.info(f"{__name__} Loaded")
bot.add_cog(Relationship_Manager(bot))