From b3b81b2b50abcc3a169a6a62bd5dddc75281995b Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Tue, 6 Apr 2021 10:00:27 +0200 Subject: [PATCH] :sparkles: Converted all lookup functionality to slash commands --- cogs/LookupCog.py | 54 ++----------------------- funcs/lookup/lookupFuncs.py | 81 ++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 79 deletions(-) diff --git a/cogs/LookupCog.py b/cogs/LookupCog.py index aa1366e..1b23cd1 100644 --- a/cogs/LookupCog.py +++ b/cogs/LookupCog.py @@ -1,9 +1,7 @@ -import discord, json from discord.ext import commands from discord_slash import cog_ext -from discord_slash import SlashCommandOptionType as scot -from utils import getParams, cap +from utils import getParams params = getParams() @@ -15,58 +13,12 @@ class LookupCog(commands.Cog): # Looks up a spell @cog_ext.cog_slash(**params["spell"]) async def spell(self, ctx, query): - spell = self.bot.lookupFuncs.spellFunc(cap(query)) - if len(spell) > 2000: - await ctx.send(spell[:2000]) - await ctx.send(spell[2000:]) - else: - await ctx.send(spell) + await self.bot.lookupFuncs.spellFunc(ctx, query) # Looks up a monster @cog_ext.cog_slash(**params["monster"]) async def monster(self, ctx, query): - title, text1, text2, text3, text4, text5 = self.bot.lookupFuncs.monsterFunc(cap(query)) - em1 = discord.Embed(title = title, description = text1, colour=0xDEADBF) - - # Sends the received information. Separates into separate messages if - # there is too much text - await ctx.send(embed = em1) - if text2 != "": - if len(text2) < 2048: - em2 = discord.Embed(title = "Special Abilities", description = text2, colour=0xDEADBF) - await ctx.send(embed = em2) - else: - em2 = discord.Embed(title = "Special Abilities", description = text2[:2048], colour=0xDEADBF) - await ctx.send(embed = em2) - em2_2 = discord.Embed(title = "", description = text2[2048:], colour=0xDEADBF) - await ctx.send(embed = em2_2) - if text3 != "": - if len(text3) < 2048: - em3 = discord.Embed(title = "Actions", description = text3, colour=0xDEADBF) - await ctx.send(embed = em3) - else: - em3 = discord.Embed(title = "Actions", description = text3[:2048], colour=0xDEADBF) - await ctx.send(embed = em3) - em3_2 = discord.Embed(title = "", description = text3[2048:], colour=0xDEADBF) - await ctx.send(embed = em3_2) - if text4 != "": - if len(text4) < 2048: - em4 = discord.Embed(title = "Reactions", description = text4, colour=0xDEADBF) - await ctx.send(embed = em4) - else: - em4 = discord.Embed(title = "Reactions", description = text4[:2048], colour=0xDEADBF) - await ctx.send(embed = em4) - em4_2 = discord.Embed(title = "", description = text4[2048:], colour=0xDEADBF) - await ctx.send(embed = em4_2) - if text5 != "": - if len(text5) < 2048: - em5 = discord.Embed(title = "Legendary Actions", description = text5, colour=0xDEADBF) - await ctx.send(embed = em5) - else: - em5 = discord.Embed(title = "Legendary Actions", description = text5[:2048], colour=0xDEADBF) - await ctx.send(embed = em5) - em5_2 = discord.Embed(title = "", description = text5[2048:], colour=0xDEADBF) - await ctx.send(embed = em5_2) + await self.bot.lookupFuncs.monsterFunc(ctx, query) def setup(bot): bot.add_cog(LookupCog(bot)) \ No newline at end of file diff --git a/funcs/lookup/lookupFuncs.py b/funcs/lookup/lookupFuncs.py index 3cfe9aa..7d0654c 100644 --- a/funcs/lookup/lookupFuncs.py +++ b/funcs/lookup/lookupFuncs.py @@ -1,5 +1,6 @@ import math import json +import discord from utils import cap @@ -18,27 +19,28 @@ class LookupFuncs(): return(str(mods)) # Looks up a monster - def monsterFunc(self, command): - self.bot.log("Looking up "+command) + async def monsterFunc(self, ctx, query): + query = cap(query) + self.bot.log("Looking up "+query) # 1-letter monsters don't exist - if len(command) < 2: - self.bot.log("Monster name too short (error code 601)") - return("I don't know that monster... (error code 601)","","","","","") + if len(query) < 2: + self.bot.log("Monster name too short") + await ctx.send("I don't know that monster...") else: # Opens "monsters.json" data = json.load(open('resources/lookup/monsters.json', encoding = "utf8")) for monster in data: - if "name" in monster and str(command) == monster["name"]: + if "name" in monster and str(query) == monster["name"]: self.bot.log("Found it!") # Looks at the information about the monster and returns that information # in separate variables, allowing Gwendolyn to know where to separate # the messages if monster["subtype"] != "": - typs = (monster["type"]+" ("+monster["subtype"]+")") + types = (monster["type"]+" ("+monster["subtype"]+")") else: - typs = monster["type"] + types = monster["type"] con_mod = math.floor((monster["constitution"]-10)/2) hit_dice = monster["hit_dice"] @@ -80,10 +82,10 @@ class LookupFuncs(): if c_immunities != "": c_immunities = "\n**Condition Immunities** "+c_immunities - spec_ab = "" + specialAbilities = "" if "special_abilities" in monster: for ability in monster["special_abilities"]: - spec_ab += "\n\n***"+ability["name"]+".*** "+ability["desc"] + specialAbilities += "\n\n***"+ability["name"]+".*** "+ability["desc"] act = "" if "actions" in monster: @@ -95,10 +97,10 @@ class LookupFuncs(): for reaction in monster["reactions"]: react += "\n\n***"+reaction["name"]+".*** "+reaction["desc"] - leg_act = "" + legendaryActions = "" if "legendary_actions" in monster: for action in monster["legendary_actions"]: - leg_act += "\n\n***"+action["name"]+".*** "+action["desc"] + legendaryActions += "\n\n***"+action["name"]+".*** "+action["desc"] if con_mod < 0: hit_dice += (" - "+str(con_mod * int(monster["hit_dice"].replace("d"," ").split()[0])*(-1))) @@ -107,33 +109,56 @@ class LookupFuncs(): new_part = "\n--------------------" - monster_type = monster["size"]+" "+typs+", "+monster["alignment"]+"*" + monster_type = monster["size"]+" "+types+", "+monster["alignment"]+"*" basic_info = "\n**Armor Class** "+str(monster["armor_class"])+"\n**Hit Points** "+str(monster["hit_points"])+" ("+hit_dice+")\n**Speed **"+monster["speed"]+new_part+"\n" - text1 = (monster_type+new_part+basic_info+stats+new_part+saving_throws+skills+vulnerabilities+resistances+immunities+c_immunities+"\n**Senses** "+monster["senses"]+"\n**Languages** "+monster["languages"]+"\n**Challenge** "+monster["challenge_rating"]) + info = (monster_type+new_part+basic_info+stats+new_part+saving_throws+skills+vulnerabilities+resistances+immunities+c_immunities+"\n**Senses** "+monster["senses"]+"\n**Languages** "+monster["languages"]+"\n**Challenge** "+monster["challenge_rating"]) - text2 = (spec_ab) - text3 = (act) - text4 = (react) - text5 = (leg_act) + monsterInfo = [(info, query), + (specialAbilities, "Special Abilities"), + (act, "Actions"), + (react, "Reactions"), + (legendaryActions, "Legendary Actions")] self.bot.log("Returning monster information") - return(str(command),text1,text2,text3,text4,text5) - self.bot.log("Monster not in database (error code 602)") - return("I don't know that monster... (error code 602)","","","","","") + + # Sends the received information. Separates into separate messages if + # there is too much text + await ctx.send(f"Result for \"{query}\"") + for text, title in monsterInfo: + if text != "": + if len(text) < 2000: + em = discord.Embed(title = title, description = text, colour=0xDEADBF) + await ctx.channel.send(embed = em) + else: + index = text[:2000].rfind(".")+1 + em1 = discord.Embed(title = title, description = text[:index], colour=0xDEADBF) + await ctx.channel.send(embed = em1) + em2 = discord.Embed(title = "", description = text[index+1:], colour=0xDEADBF) + await ctx.channel.send(embed = em2) + + break + else: + self.bot.log("Monster not in database") + await ctx.send("I don't know that monster...") # Looks up a spell - def spellFunc(self, command): - self.bot.log("Looking up "+command) + async def spellFunc(self, ctx, query): + query = cap(query) + self.bot.log("Looking up "+query) # Opens "spells.json" data = json.load(open('resources/lookup/spells.json', encoding = "utf8")) - if str(command) in data: + if query in data: self.bot.log("Returning spell information") - spell_output = ("***"+str(command)+"***\n*"+str(data[str(command)]["level"])+" level "+str(data[str(command)]["school"])+"\nCasting Time: "+str(data[str(command)]["casting_time"])+"\nRange: "+str(data[str(command)]["range"])+"\nComponents: "+str(data[str(command)]["components"])+"\nDuration: "+str(data[str(command)]["duration"])+"*\n \n"+str(data[str(command)]["description"])) + sendMessage = (f"***{query}***\n*{data[query]['level']} level {data[query]['school']}\nCasting Time: {data[query]['casting_time']}\nRange:{data[query]['range']}\nComponents:{data[query]['components']}\nDuration:{data[query]['duration']}*\n \n{data[query]['description']}") else: self.bot.log("I don't know that spell (error code 501)") - spell_output = "I don't think that's a spell (error code 501)" - self.bot.log("Successfully ran /spell") - return(spell_output) + sendMessage = "I don't think that's a spell (error code 501)" + + if len(sendMessage) > 2000: + await ctx.send(sendMessage[:2000]) + await ctx.send(sendMessage[2000:]) + else: + await ctx.send(sendMessage)