stormbrigade_sheriff/cogs/setup.py

66 lines
3.6 KiB
Python

from disnake.ext import commands
from PIL import Image
import disnake
from main import StronkBot
from loguru import logger
class Setup(commands.Cog):
def __init__(self,bot:StronkBot):
self.bot = bot
self.worlds = []
self.advert_types = []
@commands.slash_command(description="Assign a world to this server",default_member_permissions=disnake.Permissions(manage_guild=True))
async def set_server_world(self,
inter:disnake.ApplicationCommandInteraction,
world:str,
api_key:str = None):
if not world in self.worlds:
await inter.response.send_message(content="Not found, use suggestions please",ephemeral=True)
return
world_document = await self.bot.database.worlds.find_one({"world_name": world})
if not world_document:
return await inter.response.send_message(ephemeral=True,content="Can't find the world, contact Strix please")
server_document = await self.bot.database.servers.find_one({"_id": inter.guild_id})
if not server_document:
new_document = {"_id": inter.guild_id,"server_name": inter.guild.name,"worlds":{world_document["world_name"]:api_key}}
result = await self.bot.database.servers.insert_one(new_document)
return await inter.response.send_message(ephemeral=True, content=f'Great! Server registered and added {world_document["world_name"]} to your server')
world_list = server_document.get("worlds")
if world_list and world_document["_id"] in world_list:
return await inter.response.send_message(ephemeral=True, content="This world is already registered on this world!")
await self.bot.database.servers.find_one_and_update({"_id": inter.guild_id},{"$set":{"worlds.{}".format(world_document["world_name"]):api_key}},upsert=True)
await inter.response.send_message(f"Done, added {world_document['world_name']}")
@set_server_world.autocomplete("world")
async def autocomp_worlds(self, inter:disnake.ApplicationCommandInteraction, user_input:str):
if len(self.worlds) == 0:
cursor = self.bot.database.worlds.find()
world_docs = await cursor.to_list(length=None)
self.worlds = [world["world_name"] for world in world_docs if not world["ended"]]
return [world for world in self.worlds if user_input.lower() in world.lower()][:25]
@commands.slash_command(description="Define one of your channels as an advert channel")
async def set_advert_channel(self, inter:disnake.ApplicationCommandInteraction, ad_type:str, channel:disnake.TextChannel):
if not ad_type in self.advert_types:
await inter.response.send_message(content="Not found, use suggestions please",ephemeral=True)
return
result = await self.bot.database.adverts.update_one({"guild_id": inter.guild_id}, {"$set":{"advert_type": ad_type, "channel_id": channel.id}}, upsert=True)
await inter.send(content=f"Ok, registered {channel.name} as a {ad_type}. The advert should appear shortly.")
#TODO: Trigger individual refresh of this advert.
@set_advert_channel.autocomplete("ad_type")
async def autocomp_worlds(self, inter:disnake.ApplicationCommandInteraction, user_input:str):
if len(self.advert_types) == 0:
results = await self.bot.database.config.find_one({"name":"advert_types"})
self.advert_types = results.get("advert_types")
return [ad_type for ad_type in self.advert_types if user_input.lower() in ad_type.lower()][:25]
def setup(bot):
logger.info(f"{__name__} Loaded")
bot.add_cog(Setup(bot))