async def award_take(self, message, is_award): server, channel, author = message.guild, message.channel, message.author split_message = message.content.split(" ") mode_text, mode_change = ("awarded", 1) if is_award else ("deducted", -1) if len(split_message) > 2: amount = split_message[1] if amount.isdigit(): amount = int(amount) user = Utils.get_user(server, split_message[2]) if user is not None: EconomyUtils.set_cash( server.id, user.id, EconomyUtils.get_cash(server.id, user.id) + amount * mode_change) await Utils.simple_embed_reply( channel, "[%s]" % str(author), "User `%s` was %s %d%s." % (str(user), mode_text, amount, EconomyUtils.currency)) else: given_role = Utils.get_role(server, ''.join(split_message[2])) users = [] if given_role is not None: for user in server.members: if given_role in user.roles: EconomyUtils.set_cash( server.id, user.id, EconomyUtils.get_cash(server.id, user.id) + amount * mode_change) users.append(user) if len(users) > 0: await Utils.simple_embed_reply( channel, "[%s]" % str(author), "Users with the role `%s` were %s %d%s." % (str(given_role), mode_text, amount, EconomyUtils.currency)) else: await Utils.simple_embed_reply( channel, "[Error]", "No users are equipped with that role.") else: await Utils.simple_embed_reply( channel, "[Error]", "Invalid user or role supplied.") else: await Utils.simple_embed_reply( channel, "[Error]", "Amount parameter is incorrect.") else: await Utils.simple_embed_reply( 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 Shop Command"]: if len(split_message) > 1: shop_name = split_message[1] if is_valid_shop_name(shop_name): # Drop old shop table if needed self.delete_shop_by_channel_id(channel.id) # Create new tables and rows self.shops_database.execute( """CREATE TABLE IF NOT EXISTS '%s'(role_id TEXT UNIQUE, price NUMERIC, time_added REAL, duration REAL)""" % shop_name) self.shop_info_database.execute( "REPLACE INTO shops VALUES('%s', '%s', 0)" % (channel.id, shop_name)) await Utils.simple_embed_reply( channel, "[Shop Created]", "`%s` has been assigned `%s.`" % (str(channel), shop_name)) else: await Utils.simple_embed_reply( channel, "[Error]", "Shop parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["List Shops Command"]: shop_names = [] info = self.shop_info_database.execute( "SELECT channel_id, shop_name FROM shops") known_channels = [channel.id for channel in server.channels] for i in range(0, len(info), 2): if info[i] in known_channels: shop_names.append(info[i + 1]) if len(shop_names) > 0: shop_text = ''.join( [shop_name + "\n" for shop_name in shop_names])[:-1] await Utils.simple_embed_reply(channel, "[Shops]", shop_text) else: await Utils.simple_embed_reply(channel, "[Shops]", "No shops exist.") elif command is self.commands["Delete Shop Command"]: if len(split_message) > 1: shop_name = split_message[1] if is_valid_shop_name(shop_name): shop_names = self.shop_info_database.execute( "SELECT shop_name FROM shops") if len(shop_names) > 0: if shop_name in shop_names: self.shops_database.execute( "DROP TABLE IF EXISTS '%s'" % shop_name) self.shop_info_database.execute( "DELETE FROM shops WHERE shop_name='%s'" % shop_name) self.shop_info_database.execute( "DELETE FROM messages WHERE shop_name='%s'" % shop_name) await Utils.simple_embed_reply( channel, "[Shops]", "Shop `%s` was deleted." % shop_name) else: await Utils.simple_embed_reply( channel, "[Error]", "That shop doesn't exist.") else: await Utils.simple_embed_reply(channel, "[Error]", "No shops exist.") else: await Utils.simple_embed_reply( channel, "[Error]", "Shop parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Set Shop Role Command"]: if len(split_message) > 4: shop_name, price, duration, role_text = split_message[1], split_message[2].lower(), split_message[3], \ split_message[4] if is_valid_shop_name(shop_name): if self.shop_exists(shop_name): if Utils.isfloat(duration) or duration == "permanent": duration = -1 if duration == "permanent" or 0 else float( duration) if price.isdigit(): price = int(price) role = Utils.get_role(server, role_text) if role is not None: role_ids = [ name.lower() for name in self.shops_database. execute("SELECT role_id FROM '%s'" % shop_name) ] role_names = [ str( Utils.get_role_by_id( server, role_id)) for role_id in role_ids ] if str(role).lower() not in role_names: self.shops_database.execute( "INSERT OR IGNORE INTO '%s' VALUES('%s', '%d', '%s', '%s')" % (shop_name, role.id, int(price), time.time(), duration)) await Utils.simple_embed_reply( channel, "[Shops]", "`%s` has been assigned to `%s` at the price of `%s` for `%s` hours." % (str(role), shop_name, str(price), str("infinite" if duration == -1 else duration))) await self.update_messages() else: if role.id in role_ids: self.shops_database.execute( "REPLACE INTO '%s' VALUES('%s', '%d', '%s', '%s')" % (shop_name, role.id, int(price), time.time(), duration)) await Utils.simple_embed_reply( channel, "[Shops]", "The role `%s` within `%s` now has a price of `%s` for `%s` hours." % (str(role), shop_name, str(price), str("infinite" if duration == -1 else duration))) await self.update_messages() else: await Utils.simple_embed_reply( channel, "[Error]", "Duplicate role names not allowed. (lowercase-checked)" ) else: await Utils.simple_embed_reply( channel, "[Error]", "That role doesn't exist.") else: await Utils.simple_embed_reply( channel, "[Error]", "Price parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Duration parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "That shop doesn't exist.") else: await Utils.simple_embed_reply( channel, "[Error]", "Shop parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Toggle Shop Autodelete Command"]: if len(split_message) > 1: shop_name = split_message[1] if is_valid_shop_name(shop_name): if self.shop_exists(shop_name): new_value = int( self.shop_info_database.execute( "SELECT is_purge FROM shops WHERE shop_name='%s'" % shop_name)[0]) ^ 1 self.shop_info_database.execute( "UPDATE shops SET is_purge='%d' WHERE shop_name='%s'" % (new_value, shop_name)) await Utils.simple_embed_reply( channel, "[Shops]", "`%s`'s delete mode set to `%r`" % (shop_name, bool(new_value))) else: await Utils.simple_embed_reply( channel, "[Error]", "That shop doesn't exist.") else: await Utils.simple_embed_reply( channel, "[Error]", "Shop parameter incorrect.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Shop Command"]: if len(split_message) > 1: shop_name = split_message[1] if self.shop_exists(shop_name): embed = await self.get_shop_embed(shop_name, server) shop_message = await channel.send(embed=embed) self.shop_info_database.execute( "REPLACE INTO messages VALUES('%s', '%s', '%s')" % (shop_name, shop_message.id, channel.id)) else: await Utils.simple_embed_reply(channel, "[Error]", "That shop doesn't exist.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Buy Command"]: if len(split_message) > 1: given_name = ' '.join(split_message[1:]).lower() shop = self.shop_info_database.execute( "SELECT shop_name FROM shops where channel_id='%s'" % message.channel.id) if len(shop) > 0: shop = shop[0] role_ids = self.shops_database.execute( "SELECT role_id FROM '%s'" % shop) role_costs = self.shops_database.execute( "SELECT price FROM '%s'" % shop) if len(role_ids) > 0: role_names = [ str(Utils.get_role_by_id(server, role_id)) for role_id in role_ids ] if given_name in [ str(name).lower() for name in role_names ]: for i in range(len(role_ids)): role_id, role_name, role_cost = role_ids[ i], role_names[i], role_costs[i] if role_name.lower() == given_name: user_cash = EconomyUtils.get_cash( server.id, author.id) if user_cash >= role_cost: EconomyUtils.set_cash( server.id, author.id, user_cash - role_cost) role = Utils.get_role_by_id( server, role_id) if role not in author.roles: await Utils.client.add_roles( author, role) await Utils.simple_embed_reply( channel, "[%s]" % shop, "You have purchased `%s`." % role_name) else: await Utils.simple_embed_reply( channel, "[Error]", "You already have that role.") 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]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "No roles found.") else: await Utils.simple_embed_reply( channel, "[Error]", "Shop not found for this channel.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.") elif command is self.commands["Delete Shop Role Command"]: if len(split_message) > 1: given_name = ' '.join(split_message[1:]).lower() shop = self.shop_info_database.execute( "SELECT shop_name FROM shops where channel_id='%s'" % channel.id) if len(shop) > 0: shop = shop[0] role_ids = self.shops_database.execute( "SELECT role_id FROM '%s'" % shop) if len(role_ids) > 0: role_names = [ str(Utils.get_role_by_id(server, role_id)) for role_id in role_ids ] if given_name in [ str(name).lower() for name in role_names ]: for i in range(len(role_ids)): self.shops_database.execute( "DELETE FROM '%s' WHERE role_name='%s'" % (shop, role_names[i])) await Utils.simple_embed_reply( channel, "[%s]" % shop, "Role `%s` has been deleted from `%s`." % (role_names[i], shop)) else: await Utils.simple_embed_reply( channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "No roles found.") else: await Utils.simple_embed_reply( channel, "[Error]", "Shop not found for this channel.") else: await Utils.simple_embed_reply( channel, "[Error]", "Insufficient parameters supplied.")
async def command_called(self, message, command): # Slip the message on space to help parse the command split_message = message.content.split(" ") # Extract the server, channel and author from the message server, channel, author = message.guild, message.channel, message.author if command is self.commands["I Am Command"]: if len(split_message) > 1: given_name = split_message[1].lower() if is_valid_name(given_name): role_id = self.roles_db.execute( "SELECT role_id FROM '%s' where name='%s'" % (server.id, given_name)) if len(role_id) > 0: role = Utils.get_role_by_id(server, role_id[0]) if role is not None: await Utils.client.add_roles(author, role) await Utils.simple_embed_reply( channel, "[SelfRoles]", "You now have the `%s` role!" % given_name) else: await Utils.simple_embed_reply( channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid role supplied.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid parameters supplied.") elif command is self.commands["I Am Not Command"]: if len(split_message) > 1: given_name = split_message[1].lower() if is_valid_name(given_name): role_id = self.roles_db.execute( "SELECT role_id FROM '%s' where name='%s'" % (server.id, given_name)) if len(role_id) > 0: role = Utils.get_role_by_id(server, role_id[0]) if role is not None: await Utils.client.remove_roles(author, role) await Utils.simple_embed_reply( channel, "[SelfRoles]", "You no longer have the `%s` role!" % given_name) else: await Utils.simple_embed_reply( channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid role supplied.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid parameters supplied.") elif command is self.commands["Add Self Role Command"]: if len(split_message) > 2: role_name = split_message[1].lower() if is_valid_name(role_name): role = Utils.get_role(server, split_message[2]) if role is not None: self.roles_db.execute( "INSERT OR IGNORE INTO '%s' VALUES ('%s', '%s')" % (server.id, role.id, role_name)) await Utils.simple_embed_reply( channel, "[SelfRoles]", "Role `%s` was added as `%s`." % (role.name, role_name)) else: await Utils.simple_embed_reply(channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid name supplied.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid parameters supplied.") elif command is self.commands["Delete Self Role Command"]: if len(split_message) > 1: role_name = split_message[1].lower() if is_valid_name(role_name): if len( self.roles_db.execute( "SELECT name FROM `%s` WHERE name='%s'" % (server.id, role_name))) > 0: self.roles_db.execute( "DELETE FROM '%s' where name='%s'" % (server.id, role_name)) await Utils.simple_embed_reply( channel, "[SelfRoles]", "Role `%s` was deleted." % role_name) else: await Utils.simple_embed_reply(channel, "[Error]", "Role not found.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid name supplied.") else: await Utils.simple_embed_reply(channel, "[Error]", "Invalid parameters supplied.") elif command is self.commands["List Self Roles Command"]: role_names = self.roles_db.execute("SELECT name from '%s'" % server.id) if len(role_names) > 0: roles_string = ''.join([i + "\n" for i in role_names]) # TODO: Add role listing cap (to not break the embed max) # List the roles await channel.send( embed=discord.Embed(title="[Self Roles]", description=roles_string[0:-1], color=self.embed_color)) else: await Utils.simple_embed_reply( channel, "[Self Roles]", "There are currently no self roles.")