async def roll_income(self, message, command_name): server, channel, author = message.guild, message.channel, message.author command_config = self.config.get_data(key="Commands")[command_name] user_cash = EconomyUtils.get_cash(server.id, author.id) # Pick success or failure win_mode, change_mode, balance_change = ( "Success", "Payout", 1) if roll( int( self.config.get_data(key="Commands")[command_name] ["Default Success Rate"])) else ("Failure", "Deduction", -1) balance_change_range = command_config[win_mode][change_mode] cash_change = random.randint( balance_change_range["Min"], balance_change_range["Max"]) * balance_change messages = command_config[win_mode]["Messages"] if len(messages) > 0: reply = messages[rng(len(messages) - 1)] EconomyUtils.set_cash(server.id, author.id, user_cash + cash_change) for section in reply.split(" "): if re.fullmatch(r"{[0-9]{18}}", section) is not None: reply = reply.replace( section, Utils.get_user_by_id(server, section[1:-1]).mention) await Utils.simple_embed_reply( channel, "[" + str(author) + "]", reply.replace("{amount}", str(abs(cash_change)) + EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[" + str(author) + "]", str(cash_change) + EconomyUtils.currency)
async def set_income_reply(self, message, is_success): server, channel, author = message.guild, message.channel, message.author split_message = message.content.split(" ") if len(split_message) > 2: income_command = split_message[1].lower() reply = message.content[len(split_message[0]) + len(split_message[1]) + 2:] if len(reply) < 1500: if income_command in ("s**t", "work", "crime"): income_command = get_income_command(income_command) economy_config = self.config.get_data() reply_type = "Success" if is_success else "Failure" # Check to make sure that and {user_id}s supplied are valid for section in reply.split(" "): if re.fullmatch(r"{[0-9]{18}}", section) is not None: if Utils.get_user_by_id(server, section[1:-1]) is None: return await Utils.simple_embed_reply( channel, "[Error]", "User ID `" + section[1:-1] + "` not found.") economy_config["Commands"][income_command][reply_type][ "Messages"].append(reply) self.config.write_data(economy_config) await Utils.simple_embed_reply( message.channel, "[Success]", "Added `" + reply + "` to `" + income_command + "`'s replies.") else: await Utils.simple_embed_reply( message.channel, "[Error]", "Income command parameter is incorrect.") else: await Utils.simple_embed_reply( message.channel, "[Error]", "Reply message is too long - it must be < 1500 characters") else: await Utils.simple_embed_reply( message.channel, "[Error]", "Insufficient parameters supplied.")
async def command_called(self, message, command): split_message = message.content.split(" ") server, channel, author = message.guild, message.channel, message.author if command is self.commands["Set Currency Command"]: if len(split_message) > 1: new_currency = split_message[1] self.config.write_data(new_currency, key="Currency") EconomyUtils.currency = new_currency await Utils.simple_embed_reply( channel, "[Set Currency]", "Currency has been set to `%s`." % new_currency) else: await Utils.simple_embed_reply( channel, "[Error]", "Currency parameter not supplied.") elif command is self.commands["Set Starting Balance Command"]: if len(split_message) > 1: starting_balance = split_message[1] if starting_balance.isdigit(): self.config.write_data(int(starting_balance), key="Starting Balance") await Utils.simple_embed_reply( channel, "[Success]", "Starting balance set to `%s`." % starting_balance) else: await Utils.simple_embed_reply( channel, "[Error]", "Starting balance command parameter is incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Starting balance command parameter not supplied.") elif command is self.commands["Balance Command"]: user = author if len(split_message) > 1: given_user = Utils.get_user(server, split_message[1]) if given_user is not None: user = given_user else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") user_cash = EconomyUtils.get_cash(server.id, user.id) user_bank = EconomyUtils.get_bank(server.id, user.id) user_rank = EconomyUtils.get_rank(server.id, user.id) rank_text = Utils.add_number_abbreviation(user_rank) user_worth = user_cash + user_bank embed = discord.Embed(title=" [%s]" % str(user), description="Server Rank: %s" % str(rank_text), color=Utils.default_hex_color) embed.add_field(name="Cash", value=str(user_cash) + EconomyUtils.currency, inline=True) embed.add_field(name="Bank", value=str(user_bank) + EconomyUtils.currency, inline=True) embed.add_field(name="Net Worth", value=str(user_worth) + EconomyUtils.currency, inline=True) await channel.send(embed=embed) elif command is self.commands["Deposit Command"]: if len(split_message) > 1: deposit_amount = split_message[1] user_cash = EconomyUtils.get_cash(server.id, author.id) user_bank = EconomyUtils.get_bank(server.id, author.id) if user_cash != 0: if deposit_amount.isdigit(): deposit_amount = int(deposit_amount) if user_cash >= deposit_amount: EconomyUtils.set_cash(server.id, author.id, user_cash - deposit_amount) EconomyUtils.set_bank(server.id, author.id, user_bank + deposit_amount) await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Deposited %s%s into your bank account." % (str(deposit_amount), EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Sorry, but you don't have enough money to do that." ) elif deposit_amount == "all": EconomyUtils.set_cash(server.id, author.id, 0) EconomyUtils.set_bank(server.id, author.id, user_bank + user_cash) await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Deposited %s%s into your bank account." % (str(user_cash), EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[Error]", "Amount parameter is incorrect.") else: await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Sorry, but you don't have any money to deposit.") else: await Utils.simple_embed_reply( channel, "[Error]", "Amount command parameter not supplied.") elif command is self.commands["Withdraw Command"]: if len(split_message) > 1: withdraw_amount = split_message[1] user_cash = EconomyUtils.get_cash(server.id, author.id) user_bank = EconomyUtils.get_bank(server.id, author.id) if user_bank != 0: if withdraw_amount.isdigit(): withdraw_amount = int(withdraw_amount) if user_bank >= withdraw_amount: EconomyUtils.set_cash(server.id, author.id, user_cash + withdraw_amount) EconomyUtils.set_bank(server.id, author.id, user_bank - withdraw_amount) await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Withdrew %s%s into cash." % (str(withdraw_amount), EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Sorry, but you don't have enough money to do that." ) elif withdraw_amount == "all": EconomyUtils.set_bank(server.id, author.id, 0) EconomyUtils.set_cash(server.id, author.id, user_cash + user_bank) await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Withdrew %s%s into cash." % (str(user_bank), EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[Error]", "Amount parameter is incorrect.") else: await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Sorry, but you don't have any money to withdraw.") else: await Utils.simple_embed_reply( channel, "[Error]", "Amount command parameter not supplied.") elif command is self.commands["Give Command"]: if len(split_message) > 2: user = Utils.get_user(server, split_message[1]) if user is not None: author_cash = EconomyUtils.get_cash(server.id, author.id) user_cash = EconomyUtils.get_cash(server.id, user.id) if split_message[2].isdigit(): give_amount = int(split_message[2]) if author_cash < int(split_message[2]): return await Utils.simple_embed_reply( channel, "[Error]", "You don't have enough cash to do that.") elif split_message[2] == "all": give_amount = EconomyUtils.get_cash( server.id, author.id) else: return await Utils.simple_embed_reply( channel, "[Error]", "Amount parameter is incorrect.") EconomyUtils.set_cash(server.id, author.id, author_cash - give_amount) EconomyUtils.set_cash(server.id, user.id, user_cash + give_amount) await Utils.simple_embed_reply( channel, "[Success]", "You gave %s %s%s." % (str(user), str(give_amount), EconomyUtils.currency)) else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") # TODO: Optimize this? elif command is self.commands["Leaderboard Command"]: page = 1 if len(split_message) > 1: page = split_message[1] if page.isdigit(): page = int(page) else: return await Utils.simple_embed_reply( channel, "[Error]", "Page number parameter is incorrect.") user_rank_order = EconomyUtils.database_execute( "SELECT user FROM '%s' ORDER BY bank + cash DESC" % server.id) max_page = int((len(user_rank_order) + 9) // 10) if page <= max_page: if (len(user_rank_order) + 10) / 10 >= page: embed = discord.Embed(title="[%s Leaderboard]" % str(server), color=Utils.default_hex_color) for i in range(min(10, len(user_rank_order))): user_rank = (page - 1) * 10 + i rank_text = Utils.add_number_abbreviation(user_rank + 1) if len(user_rank_order) <= user_rank: break user_id = user_rank_order[user_rank] user = Utils.get_user_by_id(server, user_id) user_worth = EconomyUtils.get_bank( server.id, user_id) + EconomyUtils.get_cash( server.id, user_id) embed.add_field( name="%s : %s" % (str(user), rank_text), value=str(user_worth) + EconomyUtils.currency, inline=False) embed.set_footer(text="Page %d/%d" % (page, max_page)) await channel.send(embed=embed) else: await Utils.simple_embed_reply(channel, "[Error]", "Page number is too high.") else: await Utils.simple_embed_reply( channel, "[Error]", "You can only view a page between 1 and %d." % max_page) elif command is self.commands["Bank Command"]: embed = discord.Embed( description="**The bank never goes Bankrupt!**", color=Utils.default_hex_color) # Remove this if? if server.icon is None: embed.set_author(name="%s Bank" % str(server), icon_url=author.default_avatar_url) else: embed.set_author( name="%s Bank" % str(server), icon_url="https://media.discordapp.net/icons/%s/%s.jpg" % (server.id, server.icon)) embed.set_thumbnail(url=self.config.get_data("Bank Icon URL")) total_balance = int( EconomyUtils.database_execute( "SELECT SUM(bank + cash) FROM `%s`" % server.id)[0]) embed.add_field(name="Balance:", value=str(total_balance) + EconomyUtils.currency, inline=True) embed.add_field(name="Interest:", value=str(self.config.get_data("Interest Rate"))) embed.set_footer(text="%s" % str(time.strftime("%m-%d-%Y"))) await channel.send(embed=embed) elif command is self.commands["Award Command"]: await self.award_take(message, True) elif command is self.commands["Take Command"]: await self.award_take(message, False)
async def command_called(self, message, command): split_message = message.content.split(" ") channel, author, server = message.channel, message.author, message.guild try: # Adding a role if command is self.commands['Add Color Command']: # Check command format if len(split_message) > 1: # If the first parameter is hex if Utils.is_hex(split_message[1]): hex_color = split_message[1].upper() # If role hasn't been created and max color count hasn't been reached -> Create Role if len(self.roles[ server.id]) < self.config['Max Colors']: if self.get_role_by_hex(server, hex_color) is None: new_color_role = await self.create_role( server, hex_color) # Role already exists -> Get it else: new_color_role = self.get_role_by_hex( server, hex_color) # Give the user their color await self.give_role(server, author, new_color_role) await Utils.simple_embed_reply( channel, "[Add Role]", "Added " + hex_color + " to your roles.", hex_color) else: await Utils.simple_embed_reply( channel, "[Added Color]", "Max role count reached.", hex_color=hex_color) # First parameter is not a valid hex value -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid hex value.", split_message[1]) # Hex parameter not supplied -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Missing color parameter.") # Removing a role elif command is self.commands["Remove Color Command"]: # Get current role info current_color_role_id = self.users[server.id][author.id] current_color_role = Utils.get_role_by_id( server, current_color_role_id) hex_color = current_color_role.name # Remove the role await self.remove_role(server, author, current_color_role) # Reply await Utils.simple_embed_reply(channel, "[Removed Color]", "Removed " + hex_color + " from your roles.", hex_color=hex_color) # Deleting a role elif command is self.commands["Delete Color Command"]: # If the hex color was supplied if len(split_message) > 1: if Utils.is_hex(split_message[1]): hex_color = split_message[1].upper() color_role = self.get_role_by_hex(server, hex_color) # If the role doesn't exist -> Error if color_role is None: await Utils.simple_embed_reply( channel, "[Error]", "Color not found.", hex_color) # Role found -> Delete it and let the user know else: await self.delete_role(server, color_role) # Reply await Utils.simple_embed_reply(channel, "[Deleted Color]", "Deleted " + hex_color + ".", hex_color=hex_color) # First parameter is not a valid hex value -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid hex value.", split_message[1]) # Hex parameter not supplied -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Missing color parameter.") # Listing roles elif command is self.commands["List Colors Command"]: roles_text = "" # If roles exist if len(self.roles[server.id]) > 0: # Build text from every role name for role in self.roles[server.id]: roles_text += Utils.get_role_by_id(server, role).name + "\n" # No roles exist -> state so else: roles_text = "No roles exist." # Reply with the list await Utils.simple_embed_reply(channel, "[Color List]", roles_text) # Listing users equipped with role elif command is self.commands["Equipped Users Command"]: # If the hex color was supplied if len(split_message) > 1: if Utils.is_hex(split_message[1]): hex_color = split_message[1].upper() role = self.get_role_by_hex(server, hex_color) # If the role exists if role is not None: users_text = "" # Check if users are equipped with this role if len(self.roles[server.id][role.id]) > 0: for user_id in self.roles[server.id][role.id]: user = Utils.get_user_by_id( server, user_id) users_text += user.name + "\n" # No users are equipped -> State so else: users_text = "No users are equipped with this role." # Reply with the equipped roles await Utils.simple_embed_reply( channel, "[" + role.name + " Equipped List]", users_text, hex_color) # Hex parameter doesn't have an associated role -> Error else: await Utils.simple_embed_reply( channel, "[Error]", "Color not found.", hex_color) # First parameter is not a valid hex value -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid hex value.", split_message[1]) # Hex parameter not supplied -> Error else: await Utils.simple_embed_reply(channel, "[Error]", "Missing color parameter.") # List all info known by this mod for current server elif command is self.commands["Color Info Command"]: # If roles exist if len(self.roles[server.id]) > 0: # Begin reply crafting embed = discord.Embed(title="[Info]", Utils.default_hex_color) # Cycle all the roles, creating user list per role for role_id in self.roles[server.id]: role = Utils.get_role_by_id(server, role_id) users_text = "" for user_id in self.roles[server.id][role_id]: user = Utils.get_user_by_id(server, user_id) users_text += user.name + "\n" # Create embed field per role embed.add_field(name=role.name, value=users_text) # Reply await channel.send(embed=embed) # No used roles -> state so else: await Utils.simple_embed_reply(channel, "[Info]", "No color exist.") # Purge a given role elif command is self.commands["Clear Colors Command"]: for role_id in [role for role in self.roles[server.id]]: role = Utils.get_role_by_id(server, role_id) # Delete the role await self.delete_role(server, role) # Let the user know all await Utils.simple_embed_reply(channel, "[Purged Color]", "Purged all colors.") # Bot isn't supplied with sufficient perms -> Error except discord.errors.Forbidden as e: await Utils.simple_embed_reply(channel, "[Error]", "Bot does not have enough perms.") logging.exception("An error occurred.") # Some error I don't know of occurred, PING ALIEN! except Exception as e: # Leave as a general exception! await Utils.simple_embed_reply( channel, "[Error]", "Unknown error occurred (Ping Alien).") logging.exception("An error occurred.")
async def command_called(self, message, command): split_message = message.content.split(" ") server, channel, author = message.guild, message.channel, message.author if command is self.commands["Claim Command"]: if len(split_message) > 2: # Try and get a user from the passed arguments user = Utils.get_user(server, split_message[1]) # Check if a user was found (arguments were valid) if user is not None: # Check if the author is trying to claim themselves if user.id != author.id: amount = split_message[2].lower() if amount == "all": amount = str( EconomyUtils.get_cash(server.id, author.id)) # Check if the "Amount" parameter is a digit so it can be used correctly later if amount.isdigit(): amount = int(amount) user_cash = EconomyUtils.get_cash( server.id, author.id) # Calculate the waifu price -> DB Waifu Price + Claim Addition Amount waifu_price = int( self.waifus_db.execute( "SELECT price FROM '%s' WHERE user_id='%s'" % (server.id, user.id))[0]) + int( self.config.get_data( "Claim Addition Amount")) # Check if the user has enough cash if user_cash >= amount: # Check if the given argument is at least the minimum waifu price if amount >= waifu_price: # Set the user's (waifu) owner's ID in the DB self.waifus_db.execute( "UPDATE '%s' SET owner_id='%s', price='%d' WHERE user_id='%s'" % (server.id, author.id, amount, user.id)) # Deduct from the user's cash the amount they spent EconomyUtils.set_cash( server.id, user.id, user_cash - amount) # Tell them it was a successful waifu claim await Utils.simple_embed_reply( channel, "[Waifu]", "You claimed %s for %d%s." % (str(user), amount, EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[Error]", "You must pay at least %d to claim them!" % waifu_price) else: await Utils.simple_embed_reply( channel, "[Error]", "You don't have enough cash to do that.") else: await Utils.simple_embed_reply( channel, "[Error]", "Invalid amount supplied.") else: await Utils.simple_embed_reply( channel, "[Error]", "You cannot claim yourself.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Waifu Info Command"]: # Get the user this command is being called on - the author by default user = author if len(split_message) > 1: given_user = Utils.get_user(server, split_message[1]) if given_user is not None: user = given_user else: return await Utils.simple_embed_reply( channel, "[Error]", "Invalid user supplied.") # Start a new embed for the info to be displayed on embed = discord.Embed(title="[Waifu Info]", description="Waifu info for %s" % str(user), color=Utils.default_hex_color) # Create text of the user's waifus from the DB waifus = ''.join([ str(Utils.get_user(server, i)) + "\n" for i in self.waifus_db.execute( "SELECT user_id FROM '%s' WHERE owner_id='%s'" % (server.id, user.id)) ])[:-1] waifus = "None" if waifus == '' else waifus # Grab the price and owner of the user from the Db gifts = self.config.get_data("Gifts") # Get gift names from gifts DB (Table names) for gift and pocket gifts text db_gift_names = [gift_name for gift_name in gifts] # db_gift_names = self.gifts_db.execute("SELECT name FROM sqlite_master WHERE type='table'") # ---------- GIFT TEXT ---------- gifts_text = "" pocket_text = "" for gift_name in db_gift_names: # Get the number of gifts of that type the user has gift_amount, pocket_amount = tuple( self.gifts_db.execute( "SELECT amount, pocket_amount FROM '%s' WHERE server_id='%s' AND user_id='%s'" % (gift_name, server.id, user.id))) # If they have at least one of that gift, add it to the gift text if gift_amount > 0: gifts_text += "%s x%d" % (gifts[gift_name]["Symbol"], gift_amount) if pocket_amount > 0: pocket_text += "%s x%d" % (gifts[gift_name]["Symbol"], pocket_amount) # If the user doesn't have any gifts, set the text to "None" gifts_text = "None" if gifts_text == "" else gifts_text # If the user doesn't have any pocket gifts, set the text to "None" pocket_text = "None" if pocket_text == "" else pocket_text # Grab more info for the embed from the DB price, claimed_by, affinity, changes_of_heart, divorces = tuple( self.waifus_db.execute( "SELECT price, owner_id, affinity, changes_of_heart, divorces FROM '%s' WHERE user_id='%s'" % (server.id, user.id))) # Grab user info - it will be "None" if it doesn't apply claimed_by_user = Utils.get_user(server, str(claimed_by)) affinity_user = Utils.get_user(server, str(affinity)) # Generate the rest of the embed embed.add_field(name="Claimed By", value=str(claimed_by_user), inline=True) embed.add_field(name="Price", value=price, inline=True) embed.add_field(name="Divorces", value=divorces, inline=True) embed.add_field(name="Gifts", value=gifts_text, inline=False) embed.add_field(name="Pocket", value=pocket_text, inline=False) embed.add_field(name="Affinity", value=str(affinity_user), inline=True) embed.add_field(name="Changes of Heart", value=changes_of_heart, inline=True) embed.add_field(name="Waifus", value=waifus, inline=True) # Send the embed as the reply await channel.send(embed=embed) # TODO: Delete marriage role elif command is self.commands["Divorce Command"]: if len(split_message) > 1: # Try and get a user from the passed arguments user = Utils.get_user(server, split_message[1]) # Check if a user was found (arguments were valid) if user is not None: # Get the message author's waifus from DB waifus = self.waifus_db.execute( "SELECT user_id FROM '%s' WHERE owner_id='%s'" % (server.id, author.id)) # Check if the passed user is actually one of the author's waifus if user.id in waifus: # Calculate how much each user will get back from the divorce waifu_cost_split = round( self.waifus_db.execute( "SELECT price FROM '%s' WHERE user_id='%s' LIMIT 1" % (server.id, user.id))[0] / 2) # Grab cash values of each user and update the balance with the divorce money author_cash = EconomyUtils.get_cash( server.id, author.id) user_cash = EconomyUtils.get_cash(server.id, user.id) EconomyUtils.set_cash(server.id, user.id, user_cash + waifu_cost_split) EconomyUtils.set_cash(server.id, author.id, author_cash + waifu_cost_split) # Remove the owner from the user self.waifus_db.execute( "UPDATE '%s' SET owner_id=NULL WHERE user_id='%s'" % (server.id, user.id)) # Increase author divorce count self.waifus_db.execute( "UPDATE '%s' SET divorces=divorces+1 WHERE user_id='%s'" % (server.id, author.id)) # Let the user know it was a successful divorce and how much they got back await Utils.simple_embed_reply( channel, "[Divorce]", "You divorced %s and received %d back!" % (str(user), waifu_cost_split)) else: await Utils.simple_embed_reply( channel, "[Error]", "That is not one of your waifus.") command.reset_cool_down(author.id) else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") command.reset_cool_down(author.id) else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") command.reset_cool_down(author.id) elif command is self.commands["Gifts Command"]: # Config gifts_per_page = 6 # Default 1st page, or set it as the passed argument if it exists and is valid page = 1 if len(split_message) > 1: if split_message[1].isdigit(): page = int(split_message[1]) # There is no 0th page, but this is a valid digit - so error if it's 0 if page == 0: return await Utils.simple_embed_reply( channel, "[Error]", "Page parameter is incorrect.") else: return await Utils.simple_embed_reply( channel, "[Error]", "Page parameter is incorrect.") # Start the embed for returning embed = discord.Embed(title="[Waifu Gifts]", description="Gifts for your waifus!", color=Utils.default_hex_color) # Grab all the gifts from the config gifts = self.config.get_data("Gifts") # Calculate the page count based on gift count and gifts per page page_count = (len(gifts) + gifts_per_page - 1) // gifts_per_page # Check if the page for display exists if page <= page_count: # Put the gift names into a list from each gift gift_names = [gift for gift in gifts] # Loop through gifts_per_page or the remaining number of gifts times for gift_number in range( min(gifts_per_page, len(gifts) - (page - 1) * gifts_per_page)): # Get the gift name and then the gift itself gift_name = gift_names[gifts_per_page * (page - 1) + gift_number] gift = gifts[gift_name] # Add found info to the embed as a new field embed.add_field(name="%s - %s" % (gift["Symbol"], gift_name), value=gift["Cost"], inline=True) # Set page number info footer embed.set_footer(text="%d/%d" % (page, page_count)) # Send the created embed as a reply await channel.send(embed=embed) else: await Utils.simple_embed_reply(channel, "[Error]", "That page doesn't exist.") elif command is self.commands["Gift Command"]: if len(split_message) > 2: user = Utils.get_user(server, split_message[1]) if user is not None: if user is not author: # Build the given gift name (since it caN CONTAIN spaces) # ["A", "goOD", "gIfT"] -> "a good gift" given_gift_name = (' '.join(split_message[2:])).lower() raw_gifts = self.config.get_data("Gifts") if given_gift_name in [ gift_name.lower() for gift_name in raw_gifts ]: for gift_name in raw_gifts: # Check if the lowercase gift name is the same as the lowercase given gift name if gift_name.lower() == given_gift_name: gift = raw_gifts[gift_name] author_cash = EconomyUtils.get_cash( server.id, author.id) if author_cash >= gift["Cost"]: # Add one to gift counter in DB self.gifts_db.execute( "UPDATE '%s' SET amount=amount+1 WHERE server_id='%s' AND user_id='%s'" % (gift_name, server.id, user.id)) # Update author cash EconomyUtils.set_cash( server.id, author.id, author_cash - gift["Cost"]) # Let the author know the user got the gift await Utils.simple_embed_reply( channel, "[Gift]", "%s was gifted **%s**." % (str(user), "%s %s" % (gift_name, gift["Symbol"]))) break else: await Utils.simple_embed_reply( channel, "[Error]", "You don't have enough cash to do that." ) else: await Utils.simple_embed_reply( channel, "[Error]", "Gift not found.") else: await Utils.simple_embed_reply( channel, "[Error]", "You cannot give yourself a gift.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Affinity Command"]: if len(split_message) > 1: # Try and get a user from what was passed user = Utils.get_user(server, split_message[1]) # Check if a valid user was given if user is not None: if user is not author: current_affinity = self.waifus_db.execute( "SELECT affinity FROM '%s' WHERE user_id='%s'" % (server.id, author.id))[0] if current_affinity != user.id: # If the current author affinity is not Null, then increase the changes of heart if current_affinity is not None: self.waifus_db.execute( "UPDATE '%s' SET changes_of_heart=changes_of_heart+1 WHERE user_id='%s'" % (server.id, author.id)) # Set the affinity in the DB self.waifus_db.execute( "UPDATE '%s' SET affinity='%s' WHERE user_id='%s'" % (server.id, user.id, author.id)) # Let the user know the affinity was set await Utils.simple_embed_reply( channel, "[Affinity]", "Your affinity is now set towards %s." % str(user)) else: await Utils.simple_embed_reply( channel, "[Error]", "You already have your affinity set towards %s." % str(user)) else: await Utils.simple_embed_reply( channel, "[Error]", "You cannot set your affinity to yourself.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid user supplied.") elif command is self.commands["Waifu Leaderboard Command"]: # Grab ALL user IDs user_ids = self.waifus_db.execute("SELECT user_id FROM '%s'" % server.id) # Get Owner IDs (basically 1 person from each couple) owner_ids = self.waifus_db.execute( "SELECT owner_id FROM '%s' WHERE owner_id IS NOT NULL" % server.id) # If there's at least one couple if len(owner_ids) > 0: gifts = self.config.get_data("Gifts") # User ID : Gift Value gift_values = {} # Populate the dict with user IDs for user_id in user_ids: gift_values[user_id] = 0 # Calculate gift values for each owner for gift_name in gifts: db_data = self.gifts_db.execute( "SELECT user_id, amount FROM '%s' WHERE server_id='%s' AND amount IS NOT 0" % (gift_name, server.id)) # If there was data found if db_data: db_data = [[db_data[i], db_data[i + 1]] for i in range(0, len(db_data), 2)] # Add up each gift price for each user for user_info in db_data: gift_values[user_info[ 0]] += gifts[gift_name]["Cost"] * user_info[1] # Owner ID : Total Value owner_total_values = {} # Add up all the parameters to calculate total value for owner_id in owner_ids: owner_affinity = tuple( self.waifus_db.execute( "SELECT affinity FROM '%s' WHERE user_id='%s'" % (server.id, owner_id))) # Get the other ID of the person in the couple, their value and affinity waifu_id, waifu_value, waifu_affinity = tuple( self.waifus_db.execute( "SELECT user_id, price, affinity FROM '%s' WHERE owner_id='%s'" % (server.id, owner_id))) total_value = waifu_value + gift_values[waifu_id] # Add 10% to the value if both users have their affinities set to each other if waifu_affinity == owner_id and owner_affinity == waifu_id: owner_total_values[owner_id] = int(total_value * 1.1) else: owner_total_values[owner_id] = total_value # Get the top ten values by sorting the keys in the dictionary, reversing it and grabbing 10 or the max top_ten = sorted(owner_total_values.items(), key=lambda x: x[1] )[::-1][:min(len(owner_total_values), 11)] embed = discord.Embed(title=" [Waifu Leaderboard]", color=Utils.default_hex_color) for i in range(len(top_ten)): # Get the info from the current spot in the top 10 owner_id, value = top_ten[i] # Grab the owner's affinity owner_affinity = self.waifus_db.execute( "SELECT affinity FROM '%s' WHERE user_id='%s'" % (server.id, owner_id))[0] # Get the other ID of the person in the couple and their affinity waifu_id, waifu_affinity = tuple( self.waifus_db.execute( "SELECT user_id, affinity FROM '%s' WHERE owner_id='%s'" % (server.id, owner_id))) owner_user = Utils.get_user_by_id(server, owner_id) waifu_user = Utils.get_user_by_id(server, waifu_id) desc = "**%s** - Claimed by **%s**\n" % (str(waifu_user), str(owner_user)) if waifu_affinity == owner_id and owner_affinity == waifu_id: desc += "... and %s likes %s too!" % (str(waifu_user), str(owner_user)) else: other_user = Utils.get_user_by_id( server, waifu_affinity) desc += "... but %s likes %s!" % (str(waifu_user), str(other_user)) embed.add_field(name="%s - %s%s" % (Utils.add_number_abbreviation(i + 1), value, EconomyUtils.currency), value=desc) await channel.send(embed=embed) else: await Utils.simple_embed_reply( channel, "[Waifu Leaderboard]", "No waifus are currently claimed!") elif command is self.commands["Delete Waifu Leaderboard Command"]: # Reset all waifu info self.waifus_db.execute( "UPDATE '%s' SET owner_id=NULL, affinity=NULL, divorces=0, changes_of_heart=0, price='%d'" % (server.id, self.config.get_data("Default Claim Amount"))) # Pocket all gifts for gift_name in self.config.get_data("Gifts"): self.gifts_db.execute( "UPDATE '%s' SET pocket_amount=pocket_amount+amount, amount=0" % gift_name) # Reply await Utils.simple_embed_reply( channel, "[Waifu Leaderboard]", "The leaderboard has been deleted.")