async def get_character(self, session, name, user_id) -> (Character_model, User_model): user = session.query(User_model).get(user_id) if user is None: await self.bot.say( util.get_random_line(config.lines.user_error.no_user)) raise NoUserError() try: char = user.get_character(name=name) if char is None: if name is None: await self.bot.say( util.get_random_line(config.lines.user_error.no_char)) else: await self.bot.say( util.get_random_line( config.lines.user_error.wrong_char)) raise NoUserError() except sql.roleplay_model.TooManyCharactersError: await self.bot.say( util.get_random_line(config.lines.user_error.too_many_char)) raise NoUserError() return char, user
async def charlist(self, ctx): ''' List out all of the characters you have ''' user_id = ctx.message.author.id session = sql.sql.getSession() user = session.query(User_model).get(user_id) if user is None: await self.bot.say( util.get_random_line(config.lines.user_error.no_user)) return characters = user.characters if len(characters) is 0: await self.bot.say( util.get_random_line(config.lines.user_error.no_char)) return message = "Here is a list of {}'s characters\n".format( ctx.message.author.name) for char in characters: message += "- {}\n".format(util.format_name(char.name)) await self.bot.say(message)
async def setlevel(self, ctx, *args): '''Set the level of a character''' if len(args) is 1: character = None else: character = Character.get_user_name(args[:-1]) try: level = int(args[-1]) if level < 1: raise ValueError() except ValueError: await self.bot.say("{} is not a valid level".format(level)) return session = sql.sql.getSession() try: char, _ = await self.get_character(session, character, self.get_user_id(ctx)) except NoUserError: return classs = char.get_class() if classs is None: await self.bot.say( "{} doesn't have a class yet. You need to give them a class first" .format(util.format_name(char.name))) return if classs.max_level is not -1 and level > classs.max_level: await self.bot.say("{}\n{}s have a max level of {}".format( util.get_random_line(config.lines.critFails), char.classname, classs.max_level)) return old_xp = char.xp if level is classs.get_level(old_xp): await self.bot.say("{}\n{} is already at that level!".format( util.get_random_line(config.lines.dumb), util.format_name(char.name))) return char.xp = classs.get_xp(level) + 1 session.commit() await self.bot.say( "{} has received {:,} xp. and is now level {} ({})".format( util.format_name(char.name), char.xp - old_xp, level, classs.title[level - 1]))
async def levelup(self, ctx, *args): '''Level up your character''' character = Character.get_user_name(args) character = None if character == '' else character session = sql.sql.getSession() try: char, _ = await self.get_character(session, character, self.get_user_id(ctx)) except NoUserError: return classs = char.get_class() if classs is None: await self.bot.say( "{} doesn't have a class yet. You need to give them a class first" .format(char.name)) return old_xp = char.xp level = classs.get_level(old_xp) if level is classs.max_level: await self.bot.say("{} is already at their max level".format( char.name)) return level += 1 char.xp = classs.get_xp(level) + 1 session.commit() await self.bot.say( "{}\n{} has received {:,} xp and has leveled up to level {} ({})". format(util.get_random_line(config.lines.crits), util.format_name(char.name), char.xp - old_xp, level, classs.title[level - 1]))
async def newchar(self, ctx, *args): """This command creates a character unique to your Discord user id. Usage: !newchar <name>""" name = Character.get_user_name(args) if name == '': await self.bot.say("Usage: !newchar <Name>") return session = sql.sql.getSession() user_id = ctx.message.author.id # Check if the user is saved in the database user = session.query(User_model).get(user_id) if user is None: # Add the user to the database user = User_model(id=user_id) session.add(user) session.commit() # Check if there is already a character by that name that the user owns exist_characters = user.get_character(name=name) if exist_characters is not None: await self.bot.say( "{} already exists! There is no need to recreate him.".format( util.format_name(name))) return new_character = Character_model(name=name) user.characters.append(new_character) session.commit() await self.bot.say( "{}\nSuccessfully created a new character by the name of {}". format(util.get_random_line(config.lines.crits), util.format_name(name))) print("Created new Character: {}\nThat belongs to {}".format( new_character, user))
async def setxp(self, ctx, *args): '''Set the xp of your character''' if len(args) is 1: character = None else: character = Character.get_user_name(args[:-1]) xp = args[-1] try: xp = int(xp) if xp < 0: raise ValueError() except ValueError: await self.bot.say("{} is not a valid number. Try again.".format( args[-1])) return session = sql.sql.getSession() try: char, _ = await self.get_character(session, character, self.get_user_id(ctx)) except NoUserError: return classs = char.get_class() if classs is None: await self.bot.say( "{} doesn't have a class yet. You need to give them a class first" .format(char.name)) return old_xp = char.xp old_level = classs.get_level(old_xp) if xp == old_xp: await self.bot.say(util.get_random_line(config.lines.dumb)) return char.xp = xp session.commit() new_level = classs.get_level(char.xp) if old_level is not new_level: await self.bot.say( "{} has received {:,} xp and has leveled {} to level {} ({})". format(util.format_name(char.name), char.xp - old_xp, 'up' if char.xp - old_xp > 0 else 'down', new_level, classs.title[new_level - 1])) return next_level = "\nOnly {:,} xp to next level".format( classs.xp[new_level - 1] - char.xp + 1) await self.bot.say( "{} has received {:,} xp and now has {:,} xp{}".format( util.format_name(char.name), char.xp - old_xp, char.xp, next_level if classs.max_level is not new_level else ""))
async def setstat(self, ctx, *args): ''' Set a specific stat value. Usage: !setStat [character] <str|int|wis|dex|con|cha|com> <value> ''' # Load the arguments if len(args) is 2: character = None elif len(args) >= 3: character = Character.get_user_name(args[:-2]) else: await self.bot.say( "Usage: !setStat [character] <str|int|wis|dex|con|cha|com> <value>" ) return pre_stat = args[-2] value = args[-1] # make sure the stat is lowercase and only the first 3 characters stat = pre_stat.lower()[:3] val = 10 # Parse the value try: val = int(value) except ValueError: await self.bot.say("{} is not a number".format(value)) return session = sql.sql.getSession() try: char, _ = await self.get_character(session, character, self.get_user_id(ctx)) except NoUserError: return # Set the stat if stat == 'str': char.strength = val elif stat == 'int': char.intelligence = val elif stat == 'wis': char.wisdom = val elif stat == 'dex': char.dexterity = val elif stat == 'con': char.constitution = val elif stat == 'cha': char.charisma = val elif stat == 'com': char.comeliness = val else: await self.bot.say("{} is not a valid stat".format(pre_stat)) return session.commit() await self.bot.say( "{}\nSuccessfully changed your {} stat to {}".format( util.get_random_line(config.lines.crits), stat, val)) print("Changed {}'s {} stat to be {}\n{}".format( char.id, stat, val, char))