Example #1
0
    async def reset_lotto_entries(self, ctx, member: discord.Member=None):
        """Reset the lotto entries for the server where
        where the commands is being used.

        :member: empty discord.Member object
        """
        member = ctx.message.author
        member_id = ctx.message.author.id
        server_id = ctx.message.server.id

        bot_admin_users = []
        bot_admin_roles = []
        user_roles_list = []

        for user_role in ctx.message.author.roles:
            user_roles_list.append(str(int(user_role.id)))

        try:
            bot_admins_user_list = ConfigLoader().load_server_string_setting(
                ctx.message.server.id,
                'BotAdmins',
                'bot_admin_users'
            )

            bot_admins_role_list = ConfigLoader().load_server_string_setting(
                ctx.message.server.id,
                'BotAdmins',
                'bot_admin_roles'
            )

            for user in bot_admins_user_list.split():
                bot_admin_users.append(user)

            for role in bot_admins_role_list.split():
                bot_admin_roles.append(role)
        except (configparser.NoSectionError, configparser.Error):
            pass

        try:
            if member_id == ctx.message.server.owner_id or \
            int(member_id) == ConfigLoader().load_config_setting_int(
                        'BotSettings', 'owner_id'
            ) or \
            str(member_id) in bot_admin_users or \
            [admin_role for admin_role in user_roles_list if admin_role in bot_admin_roles]:
                args = (str(server_id),)
                DatabaseHandler().update_database_with_args(
                    "DELETE FROM credit_bet WHERE serverID = ?",
                    args
                )
                return await self.bot.say("{0.mention}: lottery table for this server reset.".format(member))
        except configparser.Error as config_error:
            print("Error with resetlotto command.")
    def assign_role(self, ctx, *, guild: str, member: discord.Member = None):
        """
        Assign users to configured roles if requested.  Command is executed via the `guild` command.

        Examples:
            > guild Test
            {user.mention}: You've been successfully added to {guild_name}.

            > guild Test
            {user.mention}: You've been removed from {guild_name}.

        :param ctx: discord.py Context
        :param guild: the requested group name, uses consume rest behavior
        :param member: optional discord.Member object
        :return: Nothing
        """
        server_id = ctx.message.server.id

        if member is None:
            member = ctx.message.author

        plugin_enabled = ConfigLoader().load_server_boolean_setting(
            server_id, 'RoleAssignment', 'enabled')

        if member is not None and plugin_enabled:
            requested_guild = discord.utils.get(ctx.message.server.roles,
                                                name=guild)

            if requested_guild is None:
                # We ran into an issue where a role name was using acute accents
                # This is the attempt to fix that if requested_guild is none
                # If still none after this we'll need to get examples to fix it
                guild = guild.replace("'", "’")
                requested_guild = discord.utils.get(ctx.message.server.roles,
                                                    name=guild)

            role_list = ConfigLoader().load_server_string_setting(
                server_id, 'RoleAssignment', 'role_list')

            assignment_channel_list = ConfigLoader(
            ).load_server_string_setting(server_id, 'RoleAssignment',
                                         'assignment_channel_id')

            if role_list == 'NOT_SET' or assignment_channel_list == 'NOT_SET':
                yield from self.bot.say(
                    "This plugin is not configured for this server.")
                return

            channel_list = []
            for channel in map(int, assignment_channel_list.split()):
                channel_list.append(channel)

            if requested_guild is not None:

                role_list_split = []
                for role in map(int, role_list.split()):
                    role_list_split.append(role)

                if int(ctx.message.channel.id) in channel_list and \
                        int(requested_guild.id) in role_list_split:
                    for role in ctx.message.author.roles:
                        if role.id == requested_guild.id:
                            yield from self.bot.remove_roles(
                                ctx.message.author, requested_guild)
                            yield from self.bot.send_message(
                                ctx.message.channel,
                                "{0.mention}: You've been removed from {1}.".
                                format(member, requested_guild.name))
                            return

                    # So we got this far, add the user to the role
                    yield from self.bot.add_roles(ctx.message.author,
                                                  requested_guild)
                    yield from self.bot.send_message(
                        ctx.message.channel,
                        "{0.mention}: You've been successfully "
                        "added to {1}!".format(member, requested_guild.name))
                    return
        return
Example #3
0
    async def get_config_information(self, ctx, member: discord.Member=None):
        """Get the server configuration settings and send in a private message

        :param ctx: discord.py Context
        :param member: empty discord.Member object
        :return:
        """
        member = ctx.message.author
        member_id = ctx.message.author.id
        server_id = ctx.message.server.id

        bot_admin_users = []
        bot_admin_roles = []
        user_roles_list = []

        for user_role in ctx.message.author.roles:
            user_roles_list.append(str(int(user_role.id)))

        try:
            bot_admins_user_list = ConfigLoader().load_server_string_setting(
                ctx.message.server.id,
                'BotAdmins',
                'bot_admin_users'
            )

            bot_admins_role_list = ConfigLoader().load_server_string_setting(
                ctx.message.server.id,
                'BotAdmins',
                'bot_admin_roles'
            )

            for user in bot_admins_user_list.split():
                bot_admin_users.append(user)

            for role in bot_admins_role_list.split():
                bot_admin_roles.append(role)

        except (configparser.NoSectionError, configparser.Error):
            pass

        try:
            if member_id == ctx.message.server.owner_id or \
                int(member_id) == ConfigLoader().load_config_setting_int(
                      'BotSettings', 'owner_id'
                ) or \
                str(member_id) in bot_admin_users or \
                    [admin_role for admin_role in user_roles_list if admin_role in bot_admin_roles]:
                return_string = "```Settings for {0}:\n\n".format(ctx.message.server.name)

                parser = configparser.ConfigParser()
                loaded_file = BotResources().load_config(
                    '%s.ini' % (
                        os.path.join(
                            self.server_settings_path,
                            str(server_id)
                        )
                    ),
                )
                parser.read(loaded_file)

                for section in parser.sections():
                    return_string = return_string + section + ":\n"
                    for name, value in parser.items(section):
                        return_string = return_string + "{0}: {1}".format(name, value) + "\n"
                        return_string = return_string + "\n"

                return_string = return_string + "```"

                await self.bot.send_message(member, return_string)
                return await self.bot.delete_message(ctx.message)
        except discord.Forbidden:
            print("There was a discord.Forbidden error.")
            bot_message = await self.bot.say(
                "I am unable to message you. You may have me blocked, "
                "or personal messages disabled."
            )
            await asyncio.sleep(5)
            await self.bot.delete_message(ctx.message)
            return await self.bot.delete_message(bot_message)
        except configparser.Error as config_error:
            print("Error with the configuration file: \n{0}".format(config_error))
Example #4
0
# load the database name from the core ini
DATABASE_NAME = ConfigLoader().load_config_setting('BotSettings', 'sqlite')

# Create the plugin list, which is built from the core ini file
EXTENSIONS = ConfigLoader().load_config_setting('BotSettings', 'enabled_plugins')

# Set the message that will display if a user hasn't accepted the terms of service
NOT_ACCEPTED_MESSAGE = ConfigLoader().load_config_setting_string('BotSettings', 'not_accepted_message')

# Should we show debug strings or not
SHOW_DEBUG = str(ConfigLoader().load_config_setting_boolean('Debugging', 'error_handle_debugger'))

# Command whitelist - these commands can be run without having to accept any terms
COMMAND_WHITELIST = ConfigLoader().load_config_setting('BotSettings', 'command_whitelist')
whitelist = []
for command in COMMAND_WHITELIST.split():
    whitelist.append('{0}'.format(COMMAND_PREFIX) + command)

# Finally, last thing to set is the information required to do bot stuff
CLIENT = commands.Bot(command_prefix=COMMAND_PREFIX, description=DESCRIPTION)


@CLIENT.event
async def on_message(message):
    """
    discord.py on_message

    Processes messages, and if the message is a command, will execute it.
    We do check if the command is in the whitelist - these commands do not require bot terms acceptance to run, as they
    are typically general use commands (e.g. accept).
Example #5
0
    async def update_config(self, ctx, update_section: str, update_key: str, *, update_value: str):
        """
        Update the configuration file

        :param ctx: discord.py Context
        :param update_section: section to be updated in the config file
        :param update_key: the key value to be updated in the passed in section
        :param update_value: the value that matches the key; this uses consume rest behavior
        """
        if update_section == 'ServerSettings':
            bot_message = await self.bot.say("This is a protected section.")
            await asyncio.sleep(5)
            await self.bot.delete_message(ctx.message)
            return await self.bot.delete_message(bot_message)
        else:
            try:
                member_id = ctx.message.author.id

                # @TODO : can we go back to using the regex option at some point? This is ugly...
                # This allows us to use #channel_name, @person_name
                update_value = update_value.replace('<@&', '')
                update_value = update_value.replace('<@!', '')
                update_value = update_value.replace('<#', '')
                update_value = update_value.replace('>', '')
                update_value = update_value.rstrip().lstrip()  # Strip out leading and trailing whitespace

                # Use regex to replace the characters added if they add via pinging; this causes whitespace issues
                # update_value = re.sub('[^\w]', '', update_value)

                bot_admin_users = []
                bot_admin_roles = []
                user_roles_list = []

                for user_role in ctx.message.author.roles:
                    user_roles_list.append(str(int(user_role.id)))

                try:
                    bot_admins_user_list = ConfigLoader().load_server_string_setting(
                        ctx.message.server.id,
                        'BotAdmins',
                        'bot_admin_users'
                    )

                    bot_admins_role_list = ConfigLoader().load_server_string_setting(
                        ctx.message.server.id,
                        'BotAdmins',
                        'bot_admin_roles'
                    )

                    if len(bot_admins_user_list) != 0:
                        for user in bot_admins_user_list.split():
                            bot_admin_users.append(user)

                    if len(bot_admins_role_list) != 0:
                        for role in bot_admins_role_list.split():
                            bot_admin_roles.append(role)

                except (configparser.NoSectionError, configparser.Error):
                    await self.bot.say("There was an error.  Please confirm the server configuration file exists "
                                       "via the genconfig command (only usable by server owner).")
                    pass

                # PEP8 formatting is amusing
                if update_section != 'BotAdmins':
                    if member_id == ctx.message.server.owner_id or \
                        int(member_id) == ConfigLoader().load_config_setting_int(
                            'BotSettings', 'owner_id'
                        ) or \
                        str(member_id) in bot_admin_users or \
                            [admin_role for admin_role in user_roles_list if admin_role in bot_admin_roles]:
                        filename = ctx.message.server.id
                        await self.update_config_file(
                            filename,
                            update_section,
                            update_key,
                            update_value,
                            ctx.message
                        )
                    else:
                        bot_message = await self.bot.say(
                            "Only the server owner can "
                            "configure different plugins."
                        )
                        await asyncio.sleep(5)
                        await self.bot.delete_message(ctx.message)
                        return await self.bot.delete_message(bot_message)
                elif update_section == 'BotAdmins':
                    if member_id == ctx.message.server.owner_id or \
                        int(member_id) == ConfigLoader().load_config_setting_int(
                            'BotSettings', 'owner_id'
                            ):
                        await self.bot.say(
                            "Please use the botadmin command to update this section."
                        )
            except (configparser.NoSectionError, configparser.NoOptionError) as config_error:
                print("Error with updating the configuration file: \n{0}".format(config_error))