<commit_before>from cogs.utils.dataIO import dataIO
from cogs.utils import checks
from discord.ext import commands
from discord.ext.commands import errors as commands_errors
import importlib
import traceback
import inspect
import os
import datetime
import aiohttp
import discord.errors
import asyncio
import random


class Core:
    def __init__(self, liara):
        self.liara = liara
        self.settings = dataIO.load_json('settings')
        self.ignore_db = False
        self.liara.loop.create_task(self.post())

    def __unload(self):
        self.settings.die = True
        self.loop.cancel()

    async def post(self):
        """Power-on self test. Beep boop."""
        if 'prefixes' in self.settings:
            self.liara.command_prefix = self.settings['prefixes']
        else:
            prefix = random.randint(1, 2**8)
            self.liara.command_prefix = self.settings['prefixes'] = [str(prefix)]
            print('Liara hasn\'t been started before, so her prefix has been set to "{0}".'.format(prefix))

        if 'cogs' in self.settings:
            for cog in self.settings['cogs']:
                if cog not in list(self.liara.extensions):
                    try:
                        self.liara.load_extension(cog)
                    except ImportError:
                        self.settings['cogs'].remove(cog)
                        print('{0} could not be loaded. This message will not be shown again.'.format(cog))
        else:
            self.settings['cogs'] = ['cogs.core']
        if 'roles' not in self.settings:
            self.settings['roles'] = {}
        # noinspection PyAttributeOutsideInit
        self.loop = self.liara.loop.create_task(self.maintenance_loop())  # starts the loop

    async def maintenance_loop(self):
        while True:
            if not self.ignore_db:  # if you wanna use something else for database management, just set this to false
                # Loading cogs
                for cog in self.settings['cogs']:
                    if cog not in list(self.liara.extensions):
                        try:
                            self.liara.load_extension(cog)
                        except ImportError:
                            self.settings['cogs'].remove(cog)  # something went wrong here
                            print('{0} could not be loaded. This message will not be shown again.'.format(cog))
                # Unloading cogs
                for cog in list(self.liara.extensions):
                    if cog not in self.settings['cogs']:
                        self.liara.unload_extension(cog)
                # Prefix changing
                self.liara.command_prefix = self.settings['prefixes']
                # Setting owner
                if 'owners' not in self.settings:
                    self.settings['owners'] = []
                try:
                    if self.liara.owner.id not in self.settings['owners']:
                        self.settings['owners'].append(self.liara.owner.id)
                except AttributeError:
                    pass
                self.liara.owners = self.settings['owners']
            await asyncio.sleep(1)

    async def on_message(self, message):
        if not self.liara.lockdown:
            if message.author.id == self.liara.owner.id:  # *always* process owner and server owner commands
                await self.liara.process_commands(message)
                return
            if message.server is not None:
                if message.server.owner == message.author:
                    await self.liara.process_commands(message)
                    return
            await self.liara.process_commands(message)

    async def on_command_error(self, exception, context):
        if isinstance(exception, commands_errors.MissingRequiredArgument):
            await self.liara.send_cmd_help(context)
        elif isinstance(exception, commands_errors.CommandInvokeError):
            exception = exception.original
            _traceback = traceback.format_tb(exception.__traceback__)
            _traceback = ''.join(_traceback)
            error = '`{0}` in command `{1}`: ```py\n{2}```'\
                .format(type(exception).__name__, context.command.qualified_name, _traceback)
            await self.liara.send_message(context.message.channel, error)
        elif isinstance(exception, commands_errors.CommandNotFound):
            pass

    @commands.group(name='set', pass_context=True, invoke_without_command=True)
    @checks.admin_or_permissions()
    async def set_cmd(self, ctx):
        """Sets Liara's settings."""
        await self.liara.send_cmd_help(ctx)

    @set_cmd.command(pass_context=True)
    @checks.is_owner()
    async def prefix(self, ctx, *prefixes: str):
        """Sets Liara's prefixes."""
        prefixes = list(prefixes)

        if not prefixes:
            await self.liara.send_cmd_help(ctx)
            return

        self.liara.command_prefix = prefixes
        self.settings['prefixes'] = prefixes
        await self.liara.say('Prefix(es) set.')

    @set_cmd.command()
    @checks.is_owner()
    async def name(self, username: str):
        """Changes Liara's username."""
        await self.liara.edit_profile(username=username)
        await self.liara.say('Username changed. Please call me {0} from now on.'.format(username))

    @set_cmd.command()
    @checks.is_owner()
    async def avatar(self, url: str):
        """Changes Liara's avatar."""
        session = aiohttp.ClientSession()
        response = await session.get(url)
        avatar = await response.read()
        response.close()
        await session.close()
        try:
            await self.liara.edit_profile(avatar=avatar)
            await self.liara.say('Avatar changed.')
        except discord.errors.InvalidArgument:
            await self.liara.say('That image type is unsupported.')

    @set_cmd.command()
    @checks.is_owner()
    async def owner(self, *owners: discord.User):
        """Sets Liara's owners."""
        self.settings['owners'] = [x.id for x in list(owners)]
        if len(list(owners)) == 1:
            await self.liara.say('Owner set.')
        else:
            await self.liara.say('Owners set.')

    @set_cmd.command(pass_context=True, no_pm=True)
    @checks.admin_or_permissions()
    async def admin(self, ctx, role: str=None):
        """Sets Liara's admin role.
        Roles are non-case sensitive."""
        server = ctx.message.server.id
        if server not in self.settings['roles']:
            self.settings['roles'][server] = {}
        if role is not None:
            self.settings['roles'][server]['admin_role'] = role
            await self.liara.say('Admin role set to `{0}` successfully.'.format(role))
        else:
            if 'admin_role' in self.settings['roles'][server]:
                self.settings['roles'][server].pop('admin_role')
            await self.liara.say('Admin role cleared.\n'
                                 'If you didn\'t intend to do this, use `{0}help set admin` for help.'
                                 .format(ctx.prefix))

    @set_cmd.command(pass_context=True, no_pm=True)
    @checks.admin_or_permissions()
    async def moderator(self, ctx, role: str=None):
        """Sets Liara's moderator role.
        Roles are non-case sensitive."""
        server = ctx.message.server.id
        if server not in self.settings['roles']:
            self.settings['roles'][server] = {}
        if role is not None:
            self.settings['roles'][server]['mod_role'] = role
            await self.liara.say('Moderator role set to `{0}` successfully.'.format(role))
        else:
            if 'mod_role' in self.settings['roles'][server]:
                self.settings['roles'][server].pop('mod_role')
            await self.liara.say('Moderator role cleared.\n'
                                 'If you didn\'t intend to do this, use `{0}help set moderator` for help.'
                                 .format(ctx.prefix))

    @commands.command()
    @checks.is_owner()
    async def shutdown(self):
        """Shuts Liara down."""
        await self.liara.say(':wave:')
        self.liara.stopped = True
        await self.liara.logout()

    @commands.command()
    @checks.is_owner()
    async def load(self, name: str):
        """Loads a cog."""
        cog_name = 'cogs.{0}'.format(name)
        if cog_name not in list(self.liara.extensions):
            try:
                cog = importlib.import_module(cog_name)<commit_msg>Change shutdown command to halt Add alias for halt<commit_after>from cogs.utils.dataIO import dataIO
from cogs.utils import checks
from discord.ext import commands
from discord.ext.commands import errors as commands_errors
import importlib
import traceback
import inspect
import os
import datetime
import aiohttp
import discord.errors
import asyncio
import random


class Core:
    def __init__(self, liara):
        self.liara = liara
        self.settings = dataIO.load_json('settings')
        self.ignore_db = False
        self.liara.loop.create_task(self.post())

    def __unload(self):
        self.settings.die = True
        self.loop.cancel()

    async def post(self):
        """Power-on self test. Beep boop."""
        if 'prefixes' in self.settings:
            self.liara.command_prefix = self.settings['prefixes']
        else:
            prefix = random.randint(1, 2**8)
            self.liara.command_prefix = self.settings['prefixes'] = [str(prefix)]
            print('Liara hasn\'t been started before, so her prefix has been set to "{0}".'.format(prefix))

        if 'cogs' in self.settings:
            for cog in self.settings['cogs']:
                if cog not in list(self.liara.extensions):
                    try:
                        self.liara.load_extension(cog)
                    except ImportError:
                        self.settings['cogs'].remove(cog)
                        print('{0} could not be loaded. This message will not be shown again.'.format(cog))
        else:
            self.settings['cogs'] = ['cogs.core']
        if 'roles' not in self.settings:
            self.settings['roles'] = {}
        # noinspection PyAttributeOutsideInit
        self.loop = self.liara.loop.create_task(self.maintenance_loop())  # starts the loop

    async def maintenance_loop(self):
        while True:
            if not self.ignore_db:  # if you wanna use something else for database management, just set this to false
                # Loading cogs
                for cog in self.settings['cogs']:
                    if cog not in list(self.liara.extensions):
                        try:
                            self.liara.load_extension(cog)
                        except ImportError:
                            self.settings['cogs'].remove(cog)  # something went wrong here
                            print('{0} could not be loaded. This message will not be shown again.'.format(cog))
                # Unloading cogs
                for cog in list(self.liara.extensions):
                    if cog not in self.settings['cogs']:
                        self.liara.unload_extension(cog)
                # Prefix changing
                self.liara.command_prefix = self.settings['prefixes']
                # Setting owner
                if 'owners' not in self.settings:
                    self.settings['owners'] = []
                try:
                    if self.liara.owner.id not in self.settings['owners']:
                        self.settings['owners'].append(self.liara.owner.id)
                except AttributeError:
                    pass
                self.liara.owners = self.settings['owners']
            await asyncio.sleep(1)

    async def on_message(self, message):
        if not self.liara.lockdown:
            if message.author.id == self.liara.owner.id:  # *always* process owner and server owner commands
                await self.liara.process_commands(message)
                return
            if message.server is not None:
                if message.server.owner == message.author:
                    await self.liara.process_commands(message)
                    return
            await self.liara.process_commands(message)

    async def on_command_error(self, exception, context):
        if isinstance(exception, commands_errors.MissingRequiredArgument):
            await self.liara.send_cmd_help(context)
        elif isinstance(exception, commands_errors.CommandInvokeError):
            exception = exception.original
            _traceback = traceback.format_tb(exception.__traceback__)
            _traceback = ''.join(_traceback)
            error = '`{0}` in command `{1}`: ```py\n{2}```'\
                .format(type(exception).__name__, context.command.qualified_name, _traceback)
            await self.liara.send_message(context.message.channel, error)
        elif isinstance(exception, commands_errors.CommandNotFound):
            pass

    @commands.group(name='set', pass_context=True, invoke_without_command=True)
    @checks.admin_or_permissions()
    async def set_cmd(self, ctx):
        """Sets Liara's settings."""
        await self.liara.send_cmd_help(ctx)

    @set_cmd.command(pass_context=True)
    @checks.is_owner()
    async def prefix(self, ctx, *prefixes: str):
        """Sets Liara's prefixes."""
        prefixes = list(prefixes)

        if not prefixes:
            await self.liara.send_cmd_help(ctx)
            return

        self.liara.command_prefix = prefixes
        self.settings['prefixes'] = prefixes
        await self.liara.say('Prefix(es) set.')

    @set_cmd.command()
    @checks.is_owner()
    async def name(self, username: str):
        """Changes Liara's username."""
        await self.liara.edit_profile(username=username)
        await self.liara.say('Username changed. Please call me {0} from now on.'.format(username))

    @set_cmd.command()
    @checks.is_owner()
    async def avatar(self, url: str):
        """Changes Liara's avatar."""
        session = aiohttp.ClientSession()
        response = await session.get(url)
        avatar = await response.read()
        response.close()
        await session.close()
        try:
            await self.liara.edit_profile(avatar=avatar)
            await self.liara.say('Avatar changed.')
        except discord.errors.InvalidArgument:
            await self.liara.say('That image type is unsupported.')

    @set_cmd.command()
    @checks.is_owner()
    async def owner(self, *owners: discord.User):
        """Sets Liara's owners."""
        self.settings['owners'] = [x.id for x in list(owners)]
        if len(list(owners)) == 1:
            await self.liara.say('Owner set.')
        else:
            await self.liara.say('Owners set.')

    @set_cmd.command(pass_context=True, no_pm=True)
    @checks.admin_or_permissions()
    async def admin(self, ctx, role: str=None):
        """Sets Liara's admin role.
        Roles are non-case sensitive."""
        server = ctx.message.server.id
        if server not in self.settings['roles']:
            self.settings['roles'][server] = {}
        if role is not None:
            self.settings['roles'][server]['admin_role'] = role
            await self.liara.say('Admin role set to `{0}` successfully.'.format(role))
        else:
            if 'admin_role' in self.settings['roles'][server]:
                self.settings['roles'][server].pop('admin_role')
            await self.liara.say('Admin role cleared.\n'
                                 'If you didn\'t intend to do this, use `{0}help set admin` for help.'
                                 .format(ctx.prefix))

    @set_cmd.command(pass_context=True, no_pm=True)
    @checks.admin_or_permissions()
    async def moderator(self, ctx, role: str=None):
        """Sets Liara's moderator role.
        Roles are non-case sensitive."""
        server = ctx.message.server.id
        if server not in self.settings['roles']:
            self.settings['roles'][server] = {}
        if role is not None:
            self.settings['roles'][server]['mod_role'] = role
            await self.liara.say('Moderator role set to `{0}` successfully.'.format(role))
        else:
            if 'mod_role' in self.settings['roles'][server]:
                self.settings['roles'][server].pop('mod_role')
            await self.liara.say('Moderator role cleared.\n'
                                 'If you didn\'t intend to do this, use `{0}help set moderator` for help.'
                                 .format(ctx.prefix))

    @commands.command(aliases=['shutdown'])
    @checks.is_owner()
    async def halt(self):
        """Shuts Liara down."""
        await self.liara.say(':wave:')
        self.liara.stopped = True
        await self.liara.logout()

    @commands.command()
    @checks.is_owner()
    async def load(self, name: str):
        """Loads a cog."""
        cog_name = 'cogs.{0}'.format(name)
        if cog_name not in list(self.liara.extensions):
            try:
                cog = importlib.import_module(cog_name)
{'fraction_non_alphanumeric': 0.11274940026914751, 'fraction_numerical': 0.0019893511204727636, 'mean_word_length': 2.018188239449055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1913420', 'n_tokens_mistral': 4850, 'n_tokens_neox': 4746, 'n_words': 1159}