<commit_before>import urllib
from plugin_manager import BasePlugin
from utils import Command, respond
from discord import InvalidArgument

class BotManagement(BasePlugin):
    name = "bot_management"

    @Command("shutdown",
             doc="Shuts down the bot.",
             syntax="N/A",
             category="bot_management",
             perms={"manage_guild"})
    async def _shutdown(self, msg):
        await respond(msg, "**AFFIRMATIVE. SHUTTING DOWN.**")
        await self.client.stop_bot()

    @Command("update_avatar",
             doc="Updates the bot's avatar.",
             syntax="(URL)",
             category="bot_management",
             perms={"manage_guild"})
    async def _update_avatar(self, msg):
        url = " ".join(msg.content.split()[1:])
        if url:
            try:
                img = urllib.request.urlopen(url).read()
                await self.client.user.edit(avatar=img)
                await respond(msg, "**AVATAR UPDATED.**")
            except (urllib.request.URLError, ValueError) as e:
                await respond(msg, "**WARNING: Invalid URL provided.**")
            except InvalidArgument:
                await respond(msg, "**NEGATIVE. Image must be a PNG or JPG.**")
        else:
            raise SyntaxError("No URL provided.")

    @Command("activate",
             doc="Activates an inactive plugin.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _activate(self, msg):
        plgname = " ".join(msg.content.split()[1:]).lower()
        all_plugins = self.plugin_manager.plugins
        if plgname in all_plugins:
            if plgname not in self.plugins:
                await self.plugin_manager.activate(plgname)
                await respond(msg, f"**ANALYSIS: Plugin {plgname} was activated successfully.**")
            else:
                await respond(msg, f"**ANALYSIS: Plugin {plgname} is already activated.**")
        else:
            await respond(msg, f"**WARNING: Could not find plugin {plgname}.**")

    @Command("deactivate",
             doc="Deactivates an active plugin.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _deactivate(self, msg):
        plgname = " ".join(msg.content.split()[1:]).lower()
        if plgname == self.name:
            await respond(msg, f"**WARNING: Cannot deactivate {self.name}.**")
        elif plgname in self.plugins:
            await self.plugin_manager.deactivate(plgname)
            await respond(msg, f"**ANALYSIS: Plugin {plgname} was deactivated successfully.**")
        else:
            await respond(msg, f"**ANALYSIS: Plugin {plgname} is not active.**")

    @Command("list_plugins",
             doc="Lists all plugins and their activation status.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _list_plugins(self, msg):
        active_plgs = ", ".join(self.plugins.keys())
        if not active_plgs:
            active_plgs = "None."
        all_plgs = list(self.plugin_manager.plugins.keys())
        inactive_plgs = ", ".join([x for x in all_plgs if x not in self.plugins])
        if not inactive_plgs:
            inactive_plgs = "None."
        await respond(msg, f"**ANALYSIS: Plugins are as follows:**```\nActive: {active_plgs}\n"
                           f"Inactive: {inactive_plgs}\n```")

    @Command("get_config",
             doc="Gets the config value at the specified path. Use <server> to fill in the server ID.",
             syntax="(path/to/value)",
             category="bot_management",
             perms={"manage_guild"})
    async def _get_config(self, msg):
        conf = self.config_manager.config
        args = msg.clean_content.split()[1:]
        path = args[0]
        path = path.replace("<server>", str(msg.guild.id))
        if path.startswith("/"):
            path = path[1:]
        path = path.split("/")
        val = conf
        for k in path:
            try:
                val = val[k]
            except TypeError:
                try:
                    i = int(k)
                    val = val[i]
                except ValueError:
                    raise SyntaxError(f"{k} is not a valid integer.")
                except IndexError:<commit_msg>Catch IndexError in get_config<commit_after>import urllib
from plugin_manager import BasePlugin
from utils import Command, respond
from discord import InvalidArgument

class BotManagement(BasePlugin):
    name = "bot_management"

    @Command("shutdown",
             doc="Shuts down the bot.",
             syntax="N/A",
             category="bot_management",
             perms={"manage_guild"})
    async def _shutdown(self, msg):
        await respond(msg, "**AFFIRMATIVE. SHUTTING DOWN.**")
        await self.client.stop_bot()

    @Command("update_avatar",
             doc="Updates the bot's avatar.",
             syntax="(URL)",
             category="bot_management",
             perms={"manage_guild"})
    async def _update_avatar(self, msg):
        url = " ".join(msg.content.split()[1:])
        if url:
            try:
                img = urllib.request.urlopen(url).read()
                await self.client.user.edit(avatar=img)
                await respond(msg, "**AVATAR UPDATED.**")
            except (urllib.request.URLError, ValueError) as e:
                await respond(msg, "**WARNING: Invalid URL provided.**")
            except InvalidArgument:
                await respond(msg, "**NEGATIVE. Image must be a PNG or JPG.**")
        else:
            raise SyntaxError("No URL provided.")

    @Command("activate",
             doc="Activates an inactive plugin.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _activate(self, msg):
        plgname = " ".join(msg.content.split()[1:]).lower()
        all_plugins = self.plugin_manager.plugins
        if plgname in all_plugins:
            if plgname not in self.plugins:
                await self.plugin_manager.activate(plgname)
                await respond(msg, f"**ANALYSIS: Plugin {plgname} was activated successfully.**")
            else:
                await respond(msg, f"**ANALYSIS: Plugin {plgname} is already activated.**")
        else:
            await respond(msg, f"**WARNING: Could not find plugin {plgname}.**")

    @Command("deactivate",
             doc="Deactivates an active plugin.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _deactivate(self, msg):
        plgname = " ".join(msg.content.split()[1:]).lower()
        if plgname == self.name:
            await respond(msg, f"**WARNING: Cannot deactivate {self.name}.**")
        elif plgname in self.plugins:
            await self.plugin_manager.deactivate(plgname)
            await respond(msg, f"**ANALYSIS: Plugin {plgname} was deactivated successfully.**")
        else:
            await respond(msg, f"**ANALYSIS: Plugin {plgname} is not active.**")

    @Command("list_plugins",
             doc="Lists all plugins and their activation status.",
             syntax="(plugin)",
             category="bot_management",
             perms={"manage_guild"})
    async def _list_plugins(self, msg):
        active_plgs = ", ".join(self.plugins.keys())
        if not active_plgs:
            active_plgs = "None."
        all_plgs = list(self.plugin_manager.plugins.keys())
        inactive_plgs = ", ".join([x for x in all_plgs if x not in self.plugins])
        if not inactive_plgs:
            inactive_plgs = "None."
        await respond(msg, f"**ANALYSIS: Plugins are as follows:**```\nActive: {active_plgs}\n"
                           f"Inactive: {inactive_plgs}\n```")

    @Command("get_config",
             doc="Gets the config value at the specified path. Use <server> to fill in the server ID.",
             syntax="(path/to/value)",
             category="bot_management",
             perms={"manage_guild"})
    async def _get_config(self, msg):
        conf = self.config_manager.config
        args = msg.clean_content.split()[1:]
        try:
            path = args[0]
        except IndexError:
            raise SyntaxError("Missing path to config value.")
        path = path.replace("<server>", str(msg.guild.id))
        if path.startswith("/"):
            path = path[1:]
        path = path.split("/")
        val = conf
        for k in path:
            try:
                val = val[k]
            except TypeError:
                try:
                    i = int(k)
                    val = val[i]
                except ValueError:
                    raise SyntaxError(f"{k} is not a valid integer.")
                except IndexError:
{'fraction_non_alphanumeric': 0.12522502250225023, 'fraction_numerical': 0.0013501350135013501, 'mean_word_length': 1.978887399463807, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6782852', 'n_tokens_mistral': 2567, 'n_tokens_neox': 2486, 'n_words': 625}