Esempio n. 1
0
    async def reload_(self, ctx, *extensions):
        if (utils.check_perms(ctx.author.id)):
            if "*" in extensions:
                title = "Reloading all extensions"
            elif len(extensions) > 1:
                title = "Reloading extensions"
            else:
                title = f"Reloading {extensions[0]}"

            embed = make_embed(author_url=[title, "", config.THUMBNAIL],
                               color=config.EMBED_INFO)  #, title=title
            m = await ctx.send(embed=embed)
            color = config.EMBED_SUCCESS
            fields = [[f"", '\u200b']]
            if "*" in extensions:
                extensions = list(self.bot.extensions.keys())
            for extension in extensions:
                self.bot.unload_extension(extension)
                try:
                    self.bot.load_extension(extension)
                    #fields.append([f"+ {extension}", '\u200b'])
                    fields[0][0] += f"+ {extension}\n"
                except:
                    color = config.EMBED_ERROR
                    #fields.append([f"- {extension}", '\u200b'])
                    fields[0][0] += f"- {extension}\n"
            #description += "Done."
            await m.edit(embed=make_embed(
                author_url=[title.replace("ing", "ed"), "", config.THUMBNAIL],
                fields=fields,
                color=color))  #title=title.replace("ing", "ed")
Esempio n. 2
0
    async def restart_bot(self, ctx):
        if (utils.check_perms(ctx.author.id)):
            m = await ctx.send(
                embed=make_embed(title="Restarting...", color=0xff00ff))

            msg_id = m.id
            msg_channel_id = m.channel.id
            msg_guild_id = m.guild.id
            print(m)
            print(m.id)
            print(m.channel.id)
            print(m.guild.id)
            #print(await self.bot.get_guild(msg_guild_id).get_channel(msg_channel_id).fetch_message(msg_id).edit("this"))
            #		channel = self.bot.get_guild(msg_guild_id).get_channel(msg_channel_id)
            #channel = guild.get_channel(msg_channel_id)
            #		msg = await channel.fetch_message(msg_id)
            #		await msg.edit(embed=make_embed(title="Restarted.", color=0xff00ff))
            with open('restart.dat', 'w+') as f:
                f.write(f"True\n{m.guild.id}\n{m.channel.id}\n{m.id}")
                #f.write('\n')
                #f.write(str(m))
                #pickle.dump(m, f, -1)
            #await ctx.send("Hi!")
            await self.bot.logout()
            await self.bot.close()
Esempio n. 3
0
    async def reload(self, ctx, *, extensions: str = "*"):
        """Reload an extension.
		
		Use `reload *` to reload all extensions.
		
		This command is automatically run by `update`.
		"""
        if (utils.check_perms(ctx.author.id)):
            await self.reload_(ctx, *extensions.split())
Esempio n. 4
0
 async def unload(self, ctx, *, module):
     """Unloads a module."""
     if (utils.check_perms(ctx.author.id)):
         try:
             self.bot.unload_extension(module)
         except Exception as e:
             #await ctx.send(f'```py\n{traceback.format_exc()}\n```')
             await ctx.send(
                 embed=make_embed(author_url=["Exception", "", config.WARN],
                                  fields=[[f"{e}", "\u200b"]],
                                  color=config.EMBED_WARN))
         else:
             #await ctx.send('\N{OK HAND SIGN}')
             await ctx.send(embed=make_embed(
                 author_url=["Unloaded Module", "", config.UNLOADED],
                 fields=[[f"- {module}", "\u200b"]],
                 color=config.EMBED_ERROR))
Esempio n. 5
0
 async def load(self, ctx, *, module):
     """Loads a module."""
     if (utils.check_perms(ctx.author.id)):
         try:
             self.bot.load_extension(module)
         except Exception as e:
             #await ctx.send(f'```py\n{e}\n```')
             await ctx.send(
                 embed=make_embed(author_url=["Exception", "", config.WARN],
                                  fields=[[f"{e}", "\u200b"]],
                                  color=config.EMBED_WARN))
         else:
             #await ctx.send('\N{OK HAND SIGN}')
             await ctx.send(embed=make_embed(
                 author_url=["Loaded Module", "", config.LOADED],
                 fields=[[f"+ {module}", "\u200b"]],
                 color=config.EMBED_SUCCESS))
Esempio n. 6
0
async def on_message(message):

    # TODO: Make a call for the server object here

    if message.author.id == client.user.id:
        return

    elif bot.initializing:
        # If people call while the objects are being handled, which might just not happen
        await client.send_message(message.channel, "Currently re-initializing, please try again later.")

    elif message.content.startswith("%team"):

        if message.content[6:] == "":
            # User didn't put in anything. Note that we might want to find a way to clarify the roles that do exist.
            await client.send_message(message.channel,
                                      "Usage is `%team [team name]`.")
            return

            # First things first, determine if it's a PM or not.
            # We need to get the server object that the member wants a role in. If not PM, it's ez.

        if not message.channel.is_private:  # Not a PM.

            server_obj = bot.servers[message.server.id]

            # Run checks to see if the message should go through or not

            whitelist = await server_obj.check_whitelist(message)
            pm_prefs = int(server_obj.pm_config)

            if pm_prefs == 1:  # Server owner has required roles be set by PMs.
                await client.send_message(message.channel, "The server moderators have required that roles be set by PM.")
                return
            elif whitelist is not None:  # The channel was not in the whitelist.
                await client.send_message(message.channel, whitelist)
                return

            member = message.author
            server = message.server

        else:  # Sent in a private message, so things might get tricky.
            servers_shared = []
            for server in client.servers:
                for member in server.members:
                    if member.id == message.author.id:
                        servers_shared.append(member.server)
            if len(servers_shared) == 0:
                await client.send_message(message.channel, "Something is wrong. We don't appear to share any servers.")
                return

            elif len(servers_shared) == 1:
                server = servers_shared[0]
            else:  # Time for issues
                base_message = "Oops, looks like I share more than one server with you. Which server would you like to set your role in? Reply with the digit of the server.\n"
                i = 1

                for svr in servers_shared:
                    base_message += "{0}: {1.name}\n".format(i, svr)
                    i += 1

                await client.send_message(message.channel, base_message)

                server_selection = await utils.get_message(client, message, i, base_message)
                if server_selection > i:
                    await client.send_message(message.channel, "That number was too large, try %team again.")
                    return
                else:
                    try:
                        server = servers_shared[(int(server_selection) - 1)]
                    except IndexError:
                        await client.send_message(message.channel, "That number was too large, try %team again.")
                        return
                    # TODO: Fix possibility of IndexError

            member = discord.utils.get(server.members, id=message.author.id)
            try:
                server_obj = bot.servers[server.id]
            except KeyError:  # Datafile is missing or something, it's not there.
                await client.send_message(message.channel, "The server datafile appears to be nonexistent for some reason.")
                return

        # Now, actually handle and process the roles.

        entered_team = message.content[6:]
        role = discord.utils.get(server.roles, name=entered_team)
        allowed_roles = server_obj.roles  # allowed_roles used to be team_list
        if server_obj.exclusive == "0":
            # Needs to be an exclusive if (not elif) because it has a chance at not returning anything
            for r in member.roles:
                if r.name in allowed_roles:
                    # If a role in the user's list of roles matches one of those we're checking
                    # or if the server has exclusive roles enabled
                    await client.send_message(message.channel,
                                              "You already have a team role. If you want to switch, message a moderator.")
                    return
        if (server_obj.check_role(entered_team)) & (role is None):
            # Role does not exist on the server, but is in the team_list, so the server just isn't configured properly.
            await client.send_message(message.channel,
                                      "The role you're trying to add existed at some point, but does not anymore, or has since been renamed.")
            # TODO: Consider using IDs in self.roles for this very reason

        elif (not server_obj.check_role(entered_team)) or (role is None):
            # If the role wasn't found by discord.utils.get() or is a role that we don't want to add, such as it not
            # being in the roles list:
            await client.send_message(message.channel, "Role isn't addable; " + server_obj.list_roles())

        elif role in member.roles:
            # If they already have the role
            await client.send_message(message.channel, "You already have this role. If you want to change, message a moderator.")

        else:
            try:
                await client.add_roles(member, role)
                await client.send_message(message.channel, "Successfully added role `{0}`.".format(role.name))
            except discord.Forbidden:
                # Something's wrong with permissions or the role hierarchy.
                await client.send_message(message.channel, "I don't have the `Manage Roles` permission, or the role you want me to assign is located above my own.\nRoles need to be located beneath my highest role in order for me to be able to assign them.")
            except discord.HTTPException:
                # Some random HTTP Exception, usually unpredictable.
                await client.send_message(message.channel, "Something went wrong, please try again.")

    # From here on out, don't let commands work in a PM.

    elif message.channel.is_private:
        return

    # Checks the Pokemon GO Wiki for information

    elif message.content.startswith("%wiki "):
        content = message.content.replace('%wiki ', '')
        page = wiki.pages[content].resolve_redirect()

        if not page.exists:
            await client.send_message(message.channel, "{} :warning: Couldn't find a page on **{}**".format(message.author.mention, content))
        else:
            await client.send_message(message.channel, "{} :candy: Found **{}**:\nhttp://{}/wiki/{}_".format(message.author.mention, page.name, wiki_base, page.name.replace(" ", "_")))

    # Bot info message, listing things such as the creator and link to github

    elif message.content.startswith("%botinfo"):
        await client.send_message(message.channel, bot.info_message)

    # List of commands

    elif message.content.startswith("%help") or message.content.startswith("%commands"):
        await client.send_message(message.channel, help_message)

    # Commands to (un)whitelist channels. Can only be run by someone with `Manage Server`.

    elif message.content.startswith("%whitelist"):
        if utils.check_perms(message):

            server_obj = bot.servers[message.server.id]

            if message.channel.id in server_obj.channel_whitelist:
                await client.send_message(message.channel, "This channel is already whitelisted.")
            else:

                with open("server_data/{0}.json".format(message.server.id), "r", encoding="utf-8") as tmp:
                    temp_data = json.load(tmp)
                    temp_data["team_ch_wl"].append(message.channel.id)
                with open("server_data/{0}.json".format(message.server.id), "w", encoding="utf-8") as tmp:
                    json.dump(temp_data, tmp)
                await client.send_message(message.channel, "Channel successfully whitelisted.")

    elif message.content.startswith("%unwhitelist"):
        if utils.check_perms(message):
            server_obj = bot.servers[message.server.id]
            if message.channel.id in server_obj.channel_whitelist:
                with open("server_data/{0}.json".format(message.server.id), "r", encoding="utf-8") as tmp:
                    temp_data = json.load(tmp)
                    temp_data["team_ch_wl"].remove(message.channel.id)
                with open("server_data/{0}.json".format(message.server.id), "w", encoding="utf-8") as tmp:
                    json.dump(temp_data, tmp)
                await client.send_message(message.channel, "Channel successfully removed from the whitelist.")
            else:
                await client.send_message(message.channel, "This channel is not whitelisted.")

    # Adjust server PM preferences

    elif message.content.startswith('%pm'):
        if not utils.check_perms(message):
            await client.send_message(message.channel,
                                      "This command is accessible by users with the `Manage Server` permission.")
            return
        else:
            flag = message.content.split()[1]
            flag_prefs = {
                "optional": "0",
                "required": "1",
            }
            if flag not in flag_prefs:
                await client.send_message(message.channel, "`%pm [required/optional]` (server owner only): Optional is default, allowing role setting in server and PMs; required disables setting roles in the server; disabled disables setting roles in PMs.")
                return
            server_obj = bot.servers[message.server.id]
            server_obj.pm_config = flag_prefs[flag]
            server_obj.export_to_file()
            await client.send_message(message.channel, "Server PM preferences now set to {0}.".format(flag))

    elif message.content.startswith("%role_config"):
        if not utils.check_perms(message):
            await client.send_message(message.channel,
                                      "This command is accessible by users with the `Manage Server` permission.")
            return
        else:
            flag = message.content.split()[1]
            flag_prefs = {
                "exclusive": "0",
                "multiple": "1",
            }
            if flag not in flag_prefs:
                await client.send_message(message.channel,
                                          "%role_\config [exclusive/multiple]__*: Setting to exclusive (default) only allows one role to be set per user. Setting to multiple allows users to set as many roles as they want.")
                return
            server_obj = bot.servers[message.server.id]
            server_obj.exclusive = flag_prefs[flag]
            server_obj.export_to_file()
            await client.send_message(message.channel, "Server role preferences now set to {0}.".format(flag))

    # Small command listing information on the server itself.

    elif message.content.startswith('%server_info'):
        members = (i for i in message.server.members)
        member_count = sum(1 for _ in members)
        await client.send_message(message.channel, server_info_message.format(message.server, member_count))

    elif message.content.startswith("%server_config"):
        server_obj = bot.servers[message.server.id]
        await client.send_message(message.channel, server_obj.generate_config_msg())

    # Generate an oauth link so people can add it to their own servers.

    elif message.content.startswith('%invite'):
        oauth_url = discord.utils.oauth_url(auth["client_id"], utils.required_perms)
        await client.send_message(message.channel, "Add me to a server by clicking this link: {}".format(oauth_url))

    # Create roles in server.

    elif message.content.startswith("%create_roles"):
        if utils.check_perms(message):

            # First, check to make sure the server doesn't already have the roles.

            server_obj = bot.servers[message.server.id]

            for role in message.server.roles:
                if role.name in server_obj.base_roles:
                    await client.send_message(
                        message.channel, "One or more default Pokemon GO team roles that I can use already exist on this server. Role creating aborted.")
                    return

            # That passed, create the roles using blank templates.

            # !!!
            # THESE ROLES HAVE TO BE AT LEAST BELOW THE ROLE THAT THE BOT HAS, OR ELSE IT CAN'T ASSIGN THEM DUE TO
            # ROLE HIERARCHY
            # Hopefully, creating the roles at position 0 and up should fix this.

            try:
                await client.send_message(message.channel, "Creating roles...")
                await client.create_role(message.server, name="Mystic", color=discord.Color.blue(),
                                         permissions=utils.team_perms, position=0)
                sleep(1)  # Rate limiting purposes
                await client.create_role(message.server, name="Instinct", color=discord.Color.gold(),
                                         permissions=utils.team_perms, position=1)
                sleep(1)
                await client.create_role(message.server, name="Valor", color=discord.Color.red(),
                                         permissions=utils.team_perms, position=2)
                await client.send_message(message.channel, "Blank roles successfully added.")
                server_obj.init_default_roles(message)
            except discord.Forbidden:
                await client.send_message(message.channel, "I don't have the `Manage Roles` permission.")
                return

    # Create a custom role for the server.

    elif message.content.startswith("%enable_role"):
        if utils.check_perms(message):
            server_obj = bot.get_server(server=message.server)
            role = server_obj.add_custom_role(message)
            if role is not None:
                await client.send_message(message.channel, "Role `{}` can now be added with %team.".format(role.name))
            else:
                await client.send_message(message.channel, "Couldn't find that role.")

    elif message.content.startswith("%disable_role"):
        if utils.check_perms(message):
            server_obj = bot.get_server(server=message.server)
            if server_obj.remove_custom_role(message):
                await client.send_message(message.channel, "Role `{}` now can *not* be added with %team.".format(message.content[14:]))
            else:
                await client.send_message(message.channel, "Role `{}` was already not assignable.".format(message.content[14:]))

    # Team stats in the server. Only for pokemon go servers

    elif message.content.startswith('%stats'):

        server_obj = bot.servers[message.server.id]
        if not server_obj.exists_default_roles():
            await client.send_message(message.channel, "This command requires Pokemon GO roles, which don't exist on this server.")

        role_stats = {
            "Mystic": 0,
            "Valor": 0,
            "Instinct": 0,
        }
        total = 0

        for member in message.server.members:
            for role in member.roles:
                if role.name in role_stats:
                    role_stats[role.name] += 1
                    total += 1

        msg = messages.stats_message.format(
            message.server.name,
            role_stats["Mystic"],
            role_stats["Valor"],
            role_stats["Instinct"],
            utils.get_percentage(role_stats["Mystic"], total),
            utils.get_percentage(role_stats["Valor"], total),
            utils.get_percentage(role_stats["Instinct"], total)
        )

        await client.send_message(message.channel, msg)

    # Evaluate an input. Only for bot owner.

    elif message.content.startswith('%eval'):
        if bot.sudo(message):
            await client.send_message(message.channel, eval(message.content[6:]))

    # Set bot status, or "game" it's currently playing.
    # A blank message removes the status

    elif message.content.startswith('%status'):
        if bot.sudo(message):
            if message.content[8:] != "":
                game_name = message.content[8:]
            else:
                game_name = None
            await client.change_status(game=discord.Game(name=game_name))

    # !!!!!!!!
    # SENDS A MESSAGE TO EVERY SERVER CONNECTED TO THE BOT. NOT AN ECHO COMMAND.
    # NOT TO BE TAKEN LIGHTLY, AND I RECOMMEND YOU DON'T USE @everyone IN THE MESSAGE UNLESS YOU WANT HATE MAIL

    elif message.content.startswith("%announce"):
        if bot.sudo(message):
            for server_id, server in bot.servers.items():
                default_channel = discord.utils.get(server.obj.channels, is_default=True)
                await client.send_message(default_channel, message.content[10:])
                sleep(0.5)  # To be nice on the api
Esempio n. 7
0
async def on_message(message):

    # TODO: Make a call for the server object here

    if message.author.id == client.user.id:
        return

    elif bot.initializing:
        # If people call while the objects are being handled, which might just not happen
        await client.send_message(
            message.channel, "Currently initializing, please try again later.")

    elif message.content.startswith("%team"):

        if message.content[6:] == "":
            # User didn't put in anything. Note that we might want to find a way to clarify the roles that do exist.
            await client.send_message(message.channel,
                                      "Usage is `%team [team name]`.")
            return

        # First things first, determine if it's a PM or not.
        # We need to get the server object that the member wants a role in. If not PM, it's ez.

        if not message.channel.is_private:  # Not a PM.

            server_obj = bot.servers[message.server.id]

            # Run checks to see if the message should go through or not

            whitelist_message = await server_obj.check_whitelist(message)
            pm_prefs = int(server_obj.pm_config)

            if pm_prefs == 1:  # Server owner has required roles be set by PMs.
                await client.send_message(
                    message.channel,
                    "The server moderators have required that roles be set by PM."
                )
                return
            elif whitelist_message is not None:  # The channel was not in the whitelist.
                await client.send_message(message.channel, whitelist_message)
                return

            member = message.author
            server = message.server

        else:  # Sent in a private message, so things might get tricky.

            server = await bot.get_server_from_pm(message)

            member = discord.utils.get(server.members, id=message.author.id)
            try:
                server_obj = bot.servers[server.id]
            except KeyError:  # Datafile is missing or something, it's not there.
                await client.send_message(
                    message.channel,
                    "The server datafile appears to be nonexistent for some reason."
                )
                return

        # Now, actually handle and process the roles.

        entered_team = message.content[6:]
        role = discord.utils.get(server.roles, name=entered_team)
        allowed_roles = server_obj.roles  # allowed_roles used to be team_list
        if server_obj.exclusive == "0":
            # Needs to be an exclusive if (not elif) because it has a chance at not returning anything
            for r in member.roles:
                if r.name in allowed_roles:
                    # If a role in the user's list of roles matches one of those we're checking
                    # or if the server has exclusive roles enabled
                    await client.send_message(
                        message.channel,
                        "You already have a team role. If you want to switch, message a moderator."
                    )
                    return
        if (server_obj.check_role(entered_team)) & (role is None):
            # Role does not exist on the server, but is in the team_list, so the server just isn't configured properly.
            await client.send_message(
                message.channel,
                "The role you're trying to add existed at some point, but does not anymore, or has since been renamed."
            )
            # TODO: Consider using IDs in self.roles for this very reason

        elif (not server_obj.check_role(entered_team)) or (role is None):
            # If the role wasn't found by discord.utils.get() or is a role that we don't want to add, such as it not
            # being in the roles list:
            await client.send_message(
                message.channel,
                "Role isn't addable; " + server_obj.list_roles())

        elif role in member.roles:
            # If they already have the role
            await client.send_message(
                message.channel,
                "You already have this role. If you want to change, message a moderator."
            )

        else:
            try:
                await client.add_roles(member, role)
                await client.send_message(
                    message.channel,
                    "Successfully added role `{0}`.".format(role.name))
            except discord.Forbidden:
                # Something's wrong with permissions or the role hierarchy.
                await client.send_message(
                    message.channel,
                    "I don't have the `Manage Roles` permission, or the role you want me to assign is located above my own.\nRoles need to be located beneath my highest role in order for me to be able to assign them."
                )
            except discord.HTTPException:
                # Some random HTTP Exception, usually unpredictable.
                await client.send_message(
                    message.channel, "Something went wrong, please try again.")

    elif message.content.startswith("%leaveteam"):
        if message.content[6:] == "":
            # User didn't put in anything. Note that we might want to find a way to clarify the roles that do exist.
            await client.send_message(message.channel,
                                      "Usage is `%leaveteam [team name]`.")
            return
        if not message.channel.is_private:
            server_obj = bot.servers[message.server.id]

            # Run checks to see if the message should go through or not

            whitelist_message = await server_obj.check_whitelist(message)
            pm_prefs = int(server_obj.pm_config)

            if pm_prefs == 1:  # Server owner has required roles be set by PMs.
                await client.send_message(
                    message.channel,
                    "The server moderators have required that roles be managed by PM."
                )
                return
            elif whitelist_message is not None:  # The channel was not in the whitelist.
                await client.send_message(message.channel, whitelist_message)
                return

            member = message.author
            server = message.server

        else:
            server = await bot.get_server_from_pm(message)
            if server is None:
                return
            else:
                member = server.get_member(message.author.id)
                server_obj = bot.servers[server.id]

        entered_team = message.content[11:]
        role = discord.utils.get(server.roles, name=entered_team)

        if server_obj.user_ctrl == "0":
            await client.send_message(
                message.channel,
                "Removing roles with %leaveteam is disabled in this server.")
            return
        elif role is None:
            await client.send_message(message.channel,
                                      "That role does not exist.")
            return
        elif (role.name not in server_obj.roles) & (role in member.roles):
            await client.send_message(
                message.channel,
                "Cannot remove that role, you can only remove roles with %leaveteam that I can add with %team."
            )
        elif role not in member.roles:
            await client.send_message(message.channel,
                                      "You don't have that role.")
        else:
            await client.remove_roles(member, role)
            await client.send_message(
                message.channel,
                "Role {0.name} removed successfully.".format(role))

    # From here on out, don't let commands work in a PM.

    elif message.channel.is_private:
        return

    # Checks the Pokemon GO Wiki for information

    elif message.content.startswith("%wiki "):
        content = message.content.replace('%wiki ', '')
        page = wiki.pages[content].resolve_redirect()

        if not page.exists:
            await client.send_message(
                message.channel,
                "{} :warning: Couldn't find a page on **{}**".format(
                    message.author.mention, content))
        else:
            await client.send_message(
                message.channel,
                "{} :candy: Found **{}**:\nhttp://{}/wiki/{}_".format(
                    message.author.mention, page.name, wiki_base,
                    page.name.replace(" ", "_")))

    # Bot info message, listing things such as the creator and link to github

    elif message.content.startswith("%botinfo"):
        await client.send_message(message.channel, bot.info_message)

    # List of commands

    elif message.content.startswith("%help") or message.content.startswith(
            "%commands"):
        await client.send_message(message.channel, help_message)

    # Commands to (un)whitelist channels. Can only be run by someone with `Manage Server`.

    elif message.content.startswith('%whitelist'):
        if utils.check_perms(message):

            server_obj = bot.servers[message.server.id]

            if message.channel.id in server_obj.channel_whitelist:
                await client.send_message(
                    message.channel, "This channel is already whitelisted.")
            else:
                server_obj.add_to_whitelist(message)
                await client.send_message(message.channel,
                                          "Channel successfully whitelisted.")

    elif message.content.startswith("%unwhitelist"):
        if utils.check_perms(message):
            server_obj = bot.servers[message.server.id]
            if message.channel.id not in server_obj.channel_whitelist:
                await client.send_message(
                    message.channel,
                    "This channel is already not whitelisted.")
            else:
                server_obj.remove_from_whitelist(message)
                await client.send_message(
                    message.channel,
                    "Channel successfully removed from the whitelist.")

    # Adjust server PM preferences

    elif message.content.startswith('%pm'):
        if not utils.check_perms(message):
            await client.send_message(
                message.channel,
                "This command is accessible by users with the `Manage Server` permission."
            )
            return
        else:
            flag = message.content.split()[1]
            if flag not in utils.flags["pm"]:
                await client.send_message(
                    message.channel,
                    "`%pm [required/optional]` (server owner only): Optional is default, allowing role setting in server and PMs; required disables setting roles in the server; disabled disables setting roles in PMs."
                )
                return
            server_obj = bot.servers[message.server.id]
            server_obj.pm_config = utils.flags["pm"][flag]
            server_obj.export_to_file()
            await client.send_message(
                message.channel,
                "Server PM preferences now set to {0}.".format(flag))

    elif message.content.startswith("%role_config"):
        if not utils.check_perms(message):
            await client.send_message(
                message.channel,
                "This command is only accessible by users with the `Manage Server` permission."
            )
            return
        else:
            flag = message.content.split()[1]
            if flag not in utils.flags["role"]:
                await client.send_message(
                    message.channel,
                    "%role_\config [exclusive/multiple]: Setting to exclusive (default) only allows one role to be set per user. Setting to multiple allows users to set as many roles as they want."
                )
                return
            server_obj = bot.servers[message.server.id]
            server_obj.exclusive = utils.flags["role"][flag]
            server_obj.export_to_file()
            await client.send_message(
                message.channel,
                "Server role preferences now set to {0}.".format(flag))

    elif message.content.startswith(
            "%leave_config"):  # Consider changing this name
        if not utils.check_perms(message):
            await client.send_message(
                message.channel,
                "This command is only accessible by users with the `Manage Server` permission."
            )
        else:
            flag = message.content.split()[1]
            if flag not in utils.flags["ctrl"]:
                await client.send_message(
                    message.channel,
                    "%leave_config [enabled/disabled]: Setting to disabled (default) prevents users from removing roles with %leaveteam. Enabled lets users use %leaveteam to remove a %team-assignable role."
                )
                return
            server_obj = bot.servers[message.server.id]
            server_obj.user_ctrl = utils.flags["ctrl"][flag]
            server_obj.export_to_file()
            await client.send_message(message.channel,
                                      "`%leaveteam` is now {0}.".format(flag))

    # Small command listing information on the server itself.

    elif message.content.startswith('%server_info'):
        members = (i for i in message.server.members)
        member_count = sum(1 for _ in members)
        await client.send_message(
            message.channel,
            server_info_message.format(message.server, member_count))

    elif message.content.startswith("%server_config"):
        server_obj = bot.servers[message.server.id]
        await client.send_message(message.channel,
                                  server_obj.generate_config_msg())

    # Generate an oauth link so people can add it to their own servers.

    elif message.content.startswith('%invite'):
        oauth_url = discord.utils.oauth_url(auth["client_id"],
                                            utils.required_perms)
        await client.send_message(
            message.channel,
            "Add me to a server by clicking this link: {}".format(oauth_url))

    # Create roles in server.

    elif message.content.startswith("%create_roles"):
        if utils.check_perms(message):

            # First, check to make sure the server doesn't already have the roles.

            server_obj = bot.servers[message.server.id]

            for role in message.server.roles:
                if role.name in server_obj.base_roles:
                    await client.send_message(
                        message.channel,
                        "One or more default Pokemon GO team roles that I can use already exist on this server. Role creating aborted."
                    )
                    return

            # That passed, create the roles using blank templates.

            # !!!
            # THESE ROLES HAVE TO BE AT LEAST BELOW THE ROLE THAT THE BOT HAS, OR ELSE IT CAN'T ASSIGN THEM DUE TO
            # ROLE HIERARCHY
            # Hopefully, creating the roles at position 0 and up should fix this.

            try:
                await client.send_message(message.channel, "Creating roles...")
                await client.create_role(message.server,
                                         name="Mystic",
                                         color=discord.Color.blue(),
                                         permissions=utils.team_perms,
                                         position=0)
                sleep(1)  # Rate limiting purposes
                await client.create_role(message.server,
                                         name="Instinct",
                                         color=discord.Color.gold(),
                                         permissions=utils.team_perms,
                                         position=1)
                sleep(1)
                await client.create_role(message.server,
                                         name="Valor",
                                         color=discord.Color.red(),
                                         permissions=utils.team_perms,
                                         position=2)
                await client.send_message(message.channel,
                                          "Blank roles successfully added.")
                server_obj.init_default_roles(message)
                log.info("Created default roles in {0.name}".format(
                    message.server))
            except discord.Forbidden:
                await client.send_message(
                    message.channel,
                    "I don't have the `Manage Roles` permission.")
                return

    # Create a custom role for the server.

    elif message.content.startswith("%enable_role"):
        if utils.check_perms(message):
            server_obj = bot.get_server(server=message.server)
            role = server_obj.add_custom_role(message)
            if role is not None:
                await client.send_message(
                    message.channel,
                    "Role `{}` can now be added with %team.".format(role.name))
            else:
                await client.send_message(message.channel,
                                          "Couldn't find that role.")

    elif message.content.startswith("%disable_role"):
        if utils.check_perms(message):
            server_obj = bot.get_server(server=message.server)
            if server_obj.remove_custom_role(message):
                await client.send_message(
                    message.channel,
                    "Role `{}` now can *not* be added with %team.".format(
                        message.content[14:]))
            else:
                await client.send_message(
                    message.channel,
                    "Role `{}` was already not assignable.".format(
                        message.content[14:]))

    # Team stats in the server. Only for pokemon go servers

    elif message.content.startswith('%stats'):

        server_obj = bot.servers[message.server.id]
        if not server_obj.exists_default_roles():
            await client.send_message(
                message.channel,
                "This command requires Pokemon GO roles, which don't exist on this server."
            )

        role_stats = {
            "Mystic": 0,
            "Valor": 0,
            "Instinct": 0,
        }
        total = 0

        for member in message.server.members:
            for role in member.roles:
                if role.name in role_stats:
                    role_stats[role.name] += 1
                    total += 1

        msg = messages.stats_message.format(
            message.server.name, role_stats["Mystic"], role_stats["Valor"],
            role_stats["Instinct"],
            utils.get_percentage(role_stats["Mystic"], total),
            utils.get_percentage(role_stats["Valor"], total),
            utils.get_percentage(role_stats["Instinct"], total))

        await client.send_message(message.channel, msg)

    # Evaluate an input. Only for bot owner.

    elif message.content.startswith('%eval'):
        if bot.sudo(message):
            eval_result = eval(message.content[6:])
            log.info("Eval executed; Command: {}; Result: {}".format(
                message.content, eval_result))
            await client.send_message(message.channel, eval_result)

    # Set bot status, or "game" it's currently playing.
    # A blank message removes the status

    elif message.content.startswith('%status'):
        if bot.sudo(message):
            if message.content[8:] != "":
                game_name = message.content[8:]
            else:
                game_name = None
            await client.change_status(game=discord.Game(name=game_name))

    # !!!!!!!!
    # SENDS A MESSAGE TO EVERY SERVER CONNECTED TO THE BOT. NOT AN ECHO COMMAND.
    # NOT TO BE TAKEN LIGHTLY, AND I RECOMMEND YOU DON'T USE @everyone IN THE MESSAGE UNLESS YOU WANT HATE MAIL

    elif message.content.startswith("%announce"):
        if bot.sudo(message):
            log.warning("%announce was used!")
            for server_id, server in bot.servers.items():
                default_channel = discord.utils.get(server.obj.channels,
                                                    is_default=True)
                await client.send_message(default_channel,
                                          message.content[10:])
                sleep(0.5)  # To be nice on the api
Esempio n. 8
0
async def on_message(message):

    if message.author.id == client.user.id:
        return

    elif message.content.startswith("%team"):

            # First things first, determine if it's a PM or not.
            # We need to get the server object that the member wants a role in. If not PM, it's ez.

        if not message.channel.is_private:  # Not a PM.

            # Is the channel whitelisted?
            with open(r"server_data/{0}.json".format(message.server.id), "r+", encoding="utf-8") as tmp:
                temp_data = json.load(tmp)
                chan_whitelist = temp_data["team_ch_wl"]
                pm_prefs = int(temp_data["pm"])

            if chan_whitelist is None:  # Nothing in the whitelist, needs to come first
                pass
            elif pm_prefs == 1:  # Server owner has required roles be set by PMs.
                await client.send_message(message.channel, "The server owner has required that roles be set by PM.")
            elif message.channel.id not in chan_whitelist:
                if len(chan_whitelist) == 1:
                    await client.send_message(message.channel, "Please put team requests in <#{0}>.".format(chan_whitelist[0]))
                    return
                elif len(chan_whitelist) > 1:  # Grammar for grammar's sake, any more are ignored.
                    await client.send_message(message.channel,
                                              "Please put team requests in <#{0}> or <#{1}>.".format(chan_whitelist[0], chan_whitelist[1]))
                    return
            else:
                pass

            member = message.author
            server = message.server

        else:  # Sent in a private message, so things might get tricky.
            servers_shared = []
            for server in client.servers:
                for member in server.members:
                    if member.id == message.author.id:
                        servers_shared.append(member.server)
            print(servers_shared)
            if len(servers_shared) == 0:
                await client.send_message(message.channel, "Something is wrong. We don't appear to share any servers.")
                return

            elif len(servers_shared) == 1:
                server = servers_shared[0]
            else:  # Wew, time for issues
                base_message = "Oops, looks like I share more than one server with you. Which server would you like to set your role in? Reply with the digit of the server.\n"
                i = 1

                for svr in servers_shared:
                    base_message += "{0}: {1.name}\n".format(i, svr)
                    i += 1

                await client.send_message(message.channel, base_message)

                server_selection = await utils.get_message(client, message, i, base_message)
                if server_selection > i:
                    await client.send_message(message.channel, "That number was too large, try %team again.")
                    return
                else:
                    server = servers_shared[int(server_selection) - 1]


            member = discord.utils.get(server.members, id=message.author.id)

        # TODO: Add ability to possibly set roles to things that aren't V/M/I, but still use default roles
        # if role in full_list and role in message.server.roles:

        # Now, actually handle and process the roles.

        entered_team = message.content[6:]
        role = discord.utils.get(server.roles, name=entered_team)

        for r in member.roles:
            if r.name in team_list:
                # If a role in the user's list of roles matches one of those we're checking
                await client.send_message(message.channel,
                                          "You already have a team role. If you want to switch, message a moderator.")
                return
        if (entered_team not in team_list) or (role is None):
            # If the role wasn't found by discord.utils.get() or is a role that we don't want to add:
            await client.send_message(message.channel, "Team doesn't exist. Teams that do are `Mystic`, `Valor`, and `Instinct`.\nBlue is Mystic, red is Valor, and yellow is Instinct.")

        elif (entered_team in team_list) & (role is None):
            # Role does not exist on the server, but is in the team_list, so the server just isn't configured properly.
            await client.send_message(message.channel, "The server does not appear to have the proper roles configured.\nAnticipated role names are `Mystic`, `Valor`, and `Instinct`.")

        elif role in member.roles:
            # If they already have the role
            await client.send_message(message.channel, "You already have this role. If you want to change, message a moderator.")

        else:
            try:
                await client.add_roles(member, role)
                await client.send_message(message.channel, "Successfully added role `{0}`.".format(role.name))
            except discord.Forbidden:
                await client.send_message(message.channel, "I don't have the `Manage Roles` permission., or the team roles are located above my own.\nTeam roles need to be located beneath my highest role for me to be able to assign roles.")
            except discord.HTTPException:
                await client.send_message(message.channel, "Something went wrong, please try again.")

    # From here on out, don't let commands work in a PM.

    elif message.channel.is_private:
        return

    # Bot info message, listing things such as the creator and link to github

    elif message.content.startswith("%botinfo"):
        await client.send_message(message.channel, bot_info_message)

    # List of commands

    elif message.content.startswith("%help") or message.content.startswith("%commands"):
        await client.send_message(message.channel, help_message)

    # Commands to (un)whitelist channels. Can only be run by someone with `Manage Server`.

    elif message.content.startswith("%whitelist"):
        if utils.check_perms(message):

            with open("server_data/{0}.json".format(message.server.id), "r", encoding="utf-8") as tmp:
                temp_data = json.load(tmp)
                temp_data["team_ch_wl"].append(message.channel.id)
            with open("server_data/{0}.json".format(message.server.id), "w", encoding="utf-8") as tmp:
                json.dump(temp_data, tmp)
            await client.send_message(message.channel, "Channel successfully whitelisted.")

    elif message.content.startswith("%unwhitelist"):
        if utils.check_perms(message):

            with open("server_data/{0}.json".format(message.server.id), "r", encoding="utf-8") as tmp:
                temp_data = json.load(tmp)
                temp_data["team_ch_wl"].remove(message.channel.id)
            with open("server_data/{0}.json".format(message.server.id), "w", encoding="utf-8") as tmp:
                json.dump(temp_data, tmp)
                await client.send_message(message.channel, "Channel successfully removed from the whitelist.")

    # Adjust server PM preferences

    elif message.content.startswith('%pm'):
        flag = message.content.split()[1]
        flag_prefs = {
            "optional": "0",
            "required": "1",
        }

        if message.author is not message.server.owner:
            await client.send_message(message.channel, "This command is accessible by users with the `Manage Server` permission.")
            return

        elif flag not in flag_prefs:
            await client.send_message(message.channel, "`%pm [required/optional/disabled]` (server owner only): Optional is default, allowing role setting in server and PMs; required disables setting roles in the server; disabled disables setting roles in PMs.")
            return
        with open("server_data/{0}.json".format(message.server.id), "r", encoding="utf-8") as tmp:
            temp_data = json.load(tmp)
            temp_data["pm"] = flag_prefs[flag]
        with open("server_data/{0}.json".format(message.server.id), "w", encoding="utf-8") as tmp:
            json.dump(temp_data, tmp)
        await client.send_message(message.channel, "Server PM preferences now set to {0}.".format(flag))

    # Small command listing information on the server itself.

    elif message.content.startswith('%server_info'):
        members = (i for i in message.server.members)
        member_count = sum(1 for _ in members)
        await client.send_message(message.channel, server_info_message.format(message.server, member_count))

    # Generate an oauth link so people can add it to their own servers.

    elif message.content.startswith('%invite'):
        #oauth_url = discord.utils.oauth_url(auth["client_id"], utils.required_perms)
        #await client.send_message(message.channel, "Add me to a server by clicking this link: {}".format(oauth_url))
        await client.send_message(message.channel, "Temporarily disabled.")

    # Create roles in server.

    elif message.content.startswith("%create_roles"):
        if utils.check_perms(message):

            # First, check to make sure the server doesn't already have the roles.

            for role in message.server.roles:
                if role.name in team_list:
                    await client.send_message(
                        message.channel, "One or more roles that I can use already exist on this server. Role creating aborted.")
                    return

            # That passed, create the roles using blank templates.

            # !!!
            # THESE ROLES HAVE TO BE AT LEAST BELOW

            try:
                await client.send_message(message.channel, "Creating roles...")
                await client.create_role(message.server, name="Mystic", color=discord.Color.blue(),
                                         permissions=utils.team_perms, position=0)
                sleep(1)  # Rate limiting purposes
                await client.create_role(message.server, name="Instinct", color=discord.Color.gold(),
                                         permissions=utils.team_perms, position=1)
                sleep(1)
                await client.create_role(message.server, name="Valor", color=discord.Color.red(),
                                         permissions=utils.team_perms, position=2)
                await client.send_message(message.channel, "Blank roles successfully added.")
            except discord.Forbidden:
                await client.send_message(message.channel, "I don't have the `Manage Roles` permission.")
                return

    # Team stats in the server.

    elif message.content.startswith('%stats'):
        role_stats = {
            "Mystic": 0,
            "Valor": 0,
            "Instinct": 0,
            "total": 0
        }
        for member in message.server.members:
            for role in member.roles:
                if role.name in role_stats:
                    role_stats[role.name] += 1
                    role_stats["total"] += 1

        msg = stats_message.format(
            message.server.name,
            role_stats["Mystic"],
            role_stats["Valor"],
            role_stats["Instinct"],
            ((role_stats["Mystic"] / role_stats["total"]) * 100),
            ((role_stats["Valor"] / role_stats["total"]) * 100),
            ((role_stats["Instinct"] / role_stats["total"]) * 100),
        )

        await client.send_message(message.channel, msg)
Esempio n. 9
0
 async def uptime(self, ctx):
     if (utils.check_perms(ctx.author.id)):
         await ctx.send(
             f"📢 Current Uptime: {datetime.timedelta(seconds=int(round(time.time()-self.time)))}"
         )
Esempio n. 10
0
 async def ping(self, ctx):
     if (utils.check_perms(ctx.author.id)):
         #await ctx.send('📢 Pong! {0}ms'.format(round(self.bot.latency*1000)))
         await ctx.send(f"📢 Pong! {self.bot.latency*1000}ms")
Esempio n. 11
0
 async def shutdown(self, ctx):
     if (utils.check_perms(ctx.author.id)):
         await ctx.bot.logout()
         await ctx.bot.close()