Пример #1
0
 async def get_shop_embed(self, shop_name, server):
     roles = self.shops_database.execute(
         "SELECT role_id, price FROM '%s' ORDER BY price DESC" % shop_name)
     # Convert [id1, cost1, id2, cost2, ...] to [[id1, cost1], [id2, cost2], ...]
     roles = [[roles[i * 2], str(roles[i * 2 + 1])]
              for i in range(len(roles) // 2)]
     embed = discord.Embed(title="[%s]" % shop_name,
                           color=Utils.default_hex_color)
     if len(roles) > 0:
         for role_info in roles:
             role = Utils.get_role_by_id(server, role_info[0])
             embed.add_field(name=str(role),
                             value=role_info[1] + EconomyUtils.currency,
                             inline=True)
     else:
         embed.description = "No roles currently available."
     return embed
Пример #2
0
 async def give_role(self, server, user, role):
     old_role_id = self.users[server.id][user.id]
     # If the user has an old role -> Delete old role
     if old_role_id is not None:
         old_role = Utils.get_role_by_id(server, old_role_id)
         # If the role isn't what's needed -> Delete old role
         if old_role.name is not role.name:
             await self.remove_role(server, user, old_role)
     # Give role to user
     await Utils.client.add_roles(user, role)
     # Save new user role to user's data
     self.users[server.id][user.id] = role.id
     # Save user to the color's data
     # Color data exists        -> Append user id
     # Color data doesn't exist -> Create and append it
     if role.id in self.roles[server.id].keys():
         self.roles[server.id][role.id].append(user.id)
     else:
         self.roles[server.id][role.id] = [user.id]
Пример #3
0
 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.")
Пример #4
0
 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.")
Пример #5
0
 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.")