From d880c84b1a5d9c58dd3a8581b7eaebe060900557 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Tue, 6 Apr 2021 13:36:27 +0200 Subject: [PATCH] :broom: Cleaned up starwars funcs --- cogs/StarWarsCog.py | 42 ++------------ funcs/starWarsFuncs/starWarsChar.py | 23 ++++++-- funcs/starWarsFuncs/starWarsDestiny.py | 28 ++++++---- funcs/starWarsFuncs/starWarsRoll.py | 77 +++++++++++++++----------- 4 files changed, 85 insertions(+), 85 deletions(-) diff --git a/cogs/StarWarsCog.py b/cogs/StarWarsCog.py index 850e4f2..57e39f0 100644 --- a/cogs/StarWarsCog.py +++ b/cogs/StarWarsCog.py @@ -2,16 +2,9 @@ import discord, string, json from discord.ext import commands from discord_slash import cog_ext -from utils import Options, cap +from utils import getParams -with open("resources/slashParameters.json", "r") as f: - params = json.load(f) - -options = Options() - -if options.testing: - for p in params: - params[p]["guild_ids"] = options.guildIds +params = getParams() class starWarsCog(commands.Cog): @@ -22,46 +15,23 @@ class starWarsCog(commands.Cog): # Rolls star wars dice @cog_ext.cog_slash(**params["starWarsRoll"]) async def starWarsRoll(self, ctx, dice = ""): - command = cap(dice) - newMessage = self.bot.starWars.roll.parseRoll("#"+str(ctx.author.id),command) - messageList = newMessage.split("\n") - await ctx.send(messageList[0]) - if len(messageList) > 1: - for messageItem in messageList[1:]: - await ctx.channel.send(messageItem) + await self.bot.starWars.roll.parseRoll(ctx, dice) # Controls destiny points @cog_ext.cog_slash(**params["starWarsDestiny"]) async def starWarsDestiny(self, ctx, parameters = ""): - newMessage = self.bot.starWars.destiny.parseDestiny("#"+str(ctx.author.id),parameters) - messageList = newMessage.split("\n") - await ctx.send(messageList[0]) - if len(messageList) > 1: - for messageItem in messageList[1:]: - await ctx.channel.send(messageItem) + await self.bot.starWars.destiny.parseDestiny(ctx, parameters) # Rolls for critical injuries @cog_ext.cog_slash(**params["starWarsCrit"]) async def starWarsCrit(self, ctx, severity : int = 0): - newMessage = self.bot.starWars.roll.critRoll(int(severity)) - - messageList = newMessage.split("\n") - await ctx.send(messageList[0]) - if len(messageList) > 1: - for messageItem in messageList[1:]: - await ctx.channel.send(messageItem) + await self.bot.starWars.roll.critRoll(ctx, severity) # Accesses and changes character sheet data with the parseChar function # from funcs/starWarsFuncs/starWarsCharacter.py @cog_ext.cog_slash(**params["starWarsCharacter"]) async def starWarsCharacter(self, ctx, parameters = ""): - command = string.capwords(parameters.replace("+","+ ").replace("-","- ").replace(",",", ")) - title, desc = self.bot.starWars.character.parseChar("#"+str(ctx.author.id),command) - if title != "": - em1 = discord.Embed(title = title, description = desc, colour=0xDEADBF) - await ctx.send(embed = em1) - else: - await ctx.send(desc) + await self.bot.starWars.character.parseChar(ctx, parameters) def setup(bot): bot.add_cog(starWarsCog(bot)) \ No newline at end of file diff --git a/funcs/starWarsFuncs/starWarsChar.py b/funcs/starWarsFuncs/starWarsChar.py index 71f56c9..30cb2f3 100644 --- a/funcs/starWarsFuncs/starWarsChar.py +++ b/funcs/starWarsFuncs/starWarsChar.py @@ -1,5 +1,6 @@ import json import string +import discord class StarWarsChar(): def __init__(self, bot): @@ -470,7 +471,10 @@ class StarWarsChar(): return cmd - def parseChar(self,user : str, cmd : str): + async def parseChar(self, ctx, parameters : str): + user = f"#{ctx.author.id}" + cmd = string.capwords(parameters.replace("+","+ ").replace("-","- ").replace(",",", ")) + returnEmbed = False cmd = self.replaceSpaces(cmd) @@ -487,8 +491,9 @@ class StarWarsChar(): if cmd == "": if userCharacter != None: - text1, text2 = self.characterSheet(userCharacter) - return text1, self.replaceWithSpaces(text2) + title, text = self.characterSheet(userCharacter) + text = self.replaceWithSpaces(text) + returnEmbed = True else: self.bot.log("Makin' a character for "+self.bot.databaseFuncs.getName(user)) with open("resources/starWars/starwarstemplates.json", "r") as f: @@ -496,14 +501,20 @@ class StarWarsChar(): newChar = templates["Character"] newChar["_id"] = user self.bot.database["starwars characters"].insert_one(newChar) - return "", "Character for " + self.bot.databaseFuncs.getName(user) + " created" + await ctx.send("Character for " + self.bot.databaseFuncs.getName(user) + " created") else: if cmd == "Purge": self.bot.log("Deleting "+self.bot.databaseFuncs.getName(user)+"'s character") self.bot.database["starwars characters"].delete_one({"_id":user}) - return "", "Character for " + self.bot.databaseFuncs.getName(user) + " deleted" + await ctx.send("Character for " + self.bot.databaseFuncs.getName(user) + " deleted") else: - return "", self.replaceWithSpaces(str(self.charData(user,cmd))) + await ctx.send(self.replaceWithSpaces(str(self.charData(user,cmd)))) + + if returnEmbed: + em = discord.Embed(title = title, description = text, colour=0xDEADBF) + await ctx.send(embed = em) + + def lightsaberChar(self,user : str): userCharacter = self.bot.database["starwars characters"].find_one({"_id":user}) diff --git a/funcs/starWarsFuncs/starWarsDestiny.py b/funcs/starWarsFuncs/starWarsDestiny.py index f12ddac..527d31a 100644 --- a/funcs/starWarsFuncs/starWarsDestiny.py +++ b/funcs/starWarsFuncs/starWarsDestiny.py @@ -4,13 +4,13 @@ class StarWarsDestiny(): def destinyNew(self, num : int): self.bot.log("Creating a new destiny pool with "+str(num)+" players") - roll, diceResults = self.bot.starwarsroll.roll(0,0,0,0,0,0,num) + roll, diceResults = self.bot.starWars.roll.roll(0,0,0,0,0,0,num) roll = "".join(sorted(roll)) with open("resources/starWars/destinyPoints.txt","wt") as f: f.write(roll) - return "Rolled for Destiny Points and got:\n"+self.bot.starwarsroll.diceResultToEmoji(diceResults)+"\n"+self.bot.starwarsroll.resultToEmoji(roll) + return "Rolled for Destiny Points and got:\n"+self.bot.starWars.roll.diceResultToEmoji(diceResults)+"\n"+self.bot.starWars.roll.resultToEmoji(roll) def destinyUse(self, user : str): with open("resources/starWars/destinyPoints.txt","rt") as f: @@ -24,7 +24,7 @@ class StarWarsDestiny(): with open("resources/starWars/destinyPoints.txt","wt") as f: f.write(points) self.bot.log("Did it") - return "Used a dark side destiny point. Destiny pool is now:\n"+self.bot.starwarsroll.resultToEmoji(points) + return "Used a dark side destiny point. Destiny pool is now:\n"+self.bot.starWars.roll.resultToEmoji(points) else: self.bot.log("There were no dark side destiny points") return "No dark side destiny points" @@ -36,31 +36,37 @@ class StarWarsDestiny(): with open("resources/starWars/destinyPoints.txt","wt") as f: f.write(points) self.bot.log("Did it") - return "Used a light side destiny point. Destiny pool is now:\n"+self.bot.starwarsroll.resultToEmoji(points) + return "Used a light side destiny point. Destiny pool is now:\n"+self.bot.starWars.roll.resultToEmoji(points) else: self.bot.log("There were no dark side destiny points") return "No light side destiny points" - def parseDestiny(self, user : str, cmd : str): + async def parseDestiny(self, ctx, cmd : str): + user = f"#{ctx.author.id}" if cmd != "": while cmd[0] == ' ': cmd = cmd[1:] if cmd == "": break - if cmd == "": self.bot.log("Retrieving destiny pool info") with open("resources/starWars/destinyPoints.txt","rt") as f: - return self.bot.starwarsroll.resultToEmoji(f.read()) + sendMessage = self.bot.starWars.roll.resultToEmoji(f.read()) else: commands = cmd.upper().split(" ") if commands[0] == "N": if len(commands) > 1: - return self.destinyNew(int(commands[1])) + sendMessage = self.destinyNew(int(commands[1])) else: - return "You need to give an amount of players (error code 921)" + sendMessage = "You need to give an amount of players (error code 921)" elif commands[0] == "U": - return self.destinyUse(user) + sendMessage = self.destinyUse(user) else: - return "I didn't quite understand that (error code 922)" + sendMessage = "I didn't quite understand that (error code 922)" + + messageList = sendMessage.split("\n") + await ctx.send(messageList[0]) + if len(messageList) > 1: + for messageItem in messageList[1:]: + await ctx.channel.send(messageItem) diff --git a/funcs/starWarsFuncs/starWarsRoll.py b/funcs/starWarsFuncs/starWarsRoll.py index 0cd045d..e7174ad 100644 --- a/funcs/starWarsFuncs/starWarsRoll.py +++ b/funcs/starWarsFuncs/starWarsRoll.py @@ -239,7 +239,7 @@ class StarWarsRoll(): return random.choice(table) # Rolls for critical injury - def critRoll(self, addington : int): + async def critRoll(self, ctx, addington : int): dd = "<:difficulty:690973992470708296>" sd = "<:setback:690972157890658415>" bd = "<:boost:690972178216386561>" @@ -253,14 +253,14 @@ class StarWarsRoll(): "**Discouraging Wound**: Flip one light side Destiny point to a dark side Destiny point (reverse if NPC), "+dd] * 5 + [ "**Stunned**: The target is staggered until the end of his next turn, "+dd] * 5 + [ "**Stinger**: Increase the difficulty of next check by one, "+dd] * 5 + [ - "**Bowled Over**: The target is knocked prone and suffers 1 sttrain, "+dd+dd] * 5 + [ + "**Bowled Over**: The target is knocked prone and suffers 1 strain, "+dd+dd] * 5 + [ "**Head Ringer**: The target increases the difficulty of all Intellect and Cunning checks by one until the end of the encounter, "+dd+dd] * 5 + [ "**Fearsome Wound**: The target increases the difficulty of all Presence and Willpower checks by one until the end of the encounter, "+dd+dd] * 5 + [ "**Agonizing Wound**: The target increases the difficulty of all Brawn and Agility checks by one until the end of the encounter, "+dd+dd] * 5 + [ "**Slightly Dazed**: The target is disoriented until the end of the encounter, "+dd+dd] * 5 + [ "**Scattered Senses**: The target removes all "+bd+" from skill checks until the end of the encounter, "+dd+dd] * 5 + [ "**Hamstrung**: The target loses his free maneuver until the end of the encounter, "+dd+dd] * 5 + [ - "**Overpowered**: The target leaves himselp open, and the attacker may immediately attempt another free attack agains him, using the exact same pool as the original, "+dd+dd] * 5 + [ + "**Overpowered**: The target leaves himself open, and the attacker may immediately attempt another free attack agains him, using the exact same pool as the original, "+dd+dd] * 5 + [ "**Winded**: Until the end of the encounter, the target cannot voluntarily suffer strain to activate any abilities or gain additional maneuvers, "+dd+dd] * 5 + [ "**Compromised**: Incerase difficulty of all skill checks by one until the end of the encounter, "+dd+dd] * 5 + [ "**At the brink**: The target suffers 1 strain each time he performs an action, "+dd+dd+dd] * 5 + [ @@ -288,56 +288,64 @@ class StarWarsRoll(): characteristic = random.choice(["brawn"] * 3 + ["agility"] * 3 + ["intellect", "cunning", "presence"]) results = "**Gruesome Injury**: The target's "+characteristic+" is permanently one lower, "+dd+dd+dd+dd - return "Roll: "+str(roll)+"\nInjury:\n"+results + sendMessage = "Roll: "+str(roll)+"\nInjury:\n"+results + + messageList = sendMessage.split("\n") + await ctx.send(messageList[0]) + if len(messageList) > 1: + for messageItem in messageList[1:]: + await ctx.channel.send(messageItem) # Parses the command into something the other functions understand - def parseRoll(self, user : str,cmd : str = ""): + async def parseRoll(self, ctx, cmd : str = ""): + user = f"#{ctx.author.id}" cmd = re.sub(' +',' ',cmd.upper()) + " " if cmd[0] == " ": cmd = cmd[1:] - cmd = self.bot.starwarschar.replaceSpaces(string.capwords(cmd)) + cmd = self.bot.starWars.character.replaceSpaces(string.capwords(cmd)) commands = cmd.split(" ") + validCommand = False + if commands[0] == "": rollParameters = [1,0,3,0,0,0,0] else: rollParameters = [0,0,0,0,0,0,0] if string.capwords(commands[0]) == "Obligations": - try: - return self.obligationRoll() - except: - self.bot.log("Obligation fucked up (error code 911)") - return "An error ocurred (error code 911)" + sendMessage = self.obligationRoll() elif string.capwords(commands[0]) in skillData: self.bot.log("Oh look! This guy has skills!") - if self.bot.starwarschar.userHasChar: + if self.bot.starWars.character.userHasChar(user): self.bot.log("They have a character. That much we know") - skillLevel = self.bot.starwarschar.charData(user,"Skills " + string.capwords(commands[0])) + skillLevel = self.bot.starWars.character.charData(user,"Skills " + string.capwords(commands[0])) if string.capwords(commands[0]) == "Lightsaber": self.bot.log("The skill is lightsaber") - charLevel = self.bot.starwarschar.charData(user,"Characteristics " + self.bot.starwarschar.lightsaberChar(user)) + charLevel = self.bot.starWars.character.charData(user,"Characteristics " + self.bot.starWars.character.lightsaberChar(user)) else: - charLevel = self.bot.starwarschar.charData(user,"Characteristics " + skillData[string.capwords(commands[0])]) + charLevel = self.bot.starWars.character.charData(user,"Characteristics " + skillData[string.capwords(commands[0])]) abilityDice = abs(charLevel-skillLevel) proficiencyDice = min(skillLevel,charLevel) commands = [str(abilityDice)] + [str(proficiencyDice)] + commands[1:] self.bot.log("Converted skill to dice") + validCommand = True else: - self.bot.log("Okay, no they don't i guess (error code 912)") - return "You don't have a user. You can make one with /starwarscharacter (error code 912)" + self.bot.log("Okay, no they don't i guess") + sendMessage = "You don't have a user. You can make one with /starwarscharacter" elif string.capwords(commands[0]) in ["Ranged","Piloting"]: self.bot.log("They fucked up writing the name of a ranged or piloting skill") if string.capwords(commands[0]) == "Ranged": - return "Did you mean \"Ranged - Heavy\" or \"Ranged - Light\" (error code 913)" + sendMessage = "Did you mean \"Ranged - Heavy\" or \"Ranged - Light\" (error code 913)" else: - return "Did you mean \"Piloting - Planetary\" or \"Piloting - Space\" (error code 913)" + sendMessage = "Did you mean \"Piloting - Planetary\" or \"Piloting - Space\" (error code 913)" + else: + validCommand = True - try: + if validCommand: self.bot.log("Converting commands to dice") for x, command in enumerate(commands): if command != "": @@ -358,21 +366,26 @@ class StarWarsRoll(): rollParameters[6] = int(command.replace("F","")) else: rollParameters[x] = int(command) - except: - self.bot.log("Someone fucked u-up! (it was them) (error code 914)") - return "Invalid input! (error code 914)" - self.bot.log("Rolling "+str(rollParameters)) - rollResults, diceResults = self.roll(rollParameters[0],rollParameters[1],rollParameters[2],rollParameters[3],rollParameters[4],rollParameters[5],rollParameters[6]) + self.bot.log("Rolling "+str(rollParameters)) + rollResults, diceResults = self.roll(rollParameters[0],rollParameters[1],rollParameters[2],rollParameters[3],rollParameters[4],rollParameters[5],rollParameters[6]) - simplified = self.simplify(rollResults) + simplified = self.simplify(rollResults) - name = self.bot.starwarschar.getCharName(user) + name = self.bot.starWars.character.getCharName(user) - self.bot.log("Returns results and simplified results") + self.bot.log("Returns results and simplified results") - if simplified == "": - return name + " rolls: " + "\n" + self.diceResultToEmoji(diceResults) + "\nEverything cancels out!" - else: - return name + " rolls: " + "\n" + self.diceResultToEmoji(diceResults) + "\n" + self.resultToEmoji(simplified) + if simplified == "": + sendMessage = name + " rolls: " + "\n" + self.diceResultToEmoji(diceResults) + "\nEverything cancels out!" + else: + sendMessage = name + " rolls: " + "\n" + self.diceResultToEmoji(diceResults) + "\n" + self.resultToEmoji(simplified) + messageList = sendMessage.split("\n") + await ctx.send(messageList[0]) + if len(messageList) > 1: + for messageItem in messageList[1:]: + if messageItem == "": + self.bot.log("Tried to send empty message") + else: + await ctx.channel.send(messageItem)