Ejemplo n.º 1
0
    async def move(self, ctx: Context, *args: str):
        todo_id, section_id, args = await parse_section_args(ctx, args)

        if not (todo_id and section_id):
            return

        if not args:
            return await ctx.send('Specify second list id')

        todo_id_2 = args[0]

        with Session() as session:
            todo_list_2 = get_todo_list(session, ctx.guild.id, todo_id_2)
            if not todo_list_2:
                return await ctx.send('Invalid second list id')

            sections = get_todo_sects(session, ctx.guild.id, todo_id)
            section = sections[section_id - 1]

            section.todo_id = todo_id_2
            section.timestamp = func.now()

            session.commit()

        await update_list(ctx, todo_id)
        await update_list(ctx, todo_id_2)
Ejemplo n.º 2
0
async def get_specified_champion(ctx: Context, name: tuple[str], champ_func):
    possible_keys, parsedname = parse_possible_champion_keys(name)
    champ = None
    for key in possible_keys:
        try:
            champ = await champ_func(key=key).get()
            break
        except (NotFound, KeyError):
            try:
                champ = await champ_func(name=key).get()
                break
            except (NotFound, KeyError):
                pass

    if champ:
        with Session() as session:
            cache = session.query(LeaguechampsKeyCache).where(
                LeaguechampsKeyCache.parsedname == parsedname).first()
            if not cache:
                new_key = LeaguechampsKeyCache()
                new_key.key = champ.key
                new_key.parsedname = parsedname
                session.add(new_key)
                session.commit()
    else:
        await ctx.send('Champion not found')

    return champ
Ejemplo n.º 3
0
    async def swap(self, ctx: Context, *args: str):
        todo_id, section_id, args = await parse_section_args(ctx, args)

        if not (todo_id and section_id):
            return

        if not args:
            return await ctx.send('Specify second section id')

        try:
            section_id_2 = int(args[0])

            with Session() as session:
                sections = get_todo_sects(session, ctx.guild.id, todo_id)
                if 1 <= section_id_2 <= len(sections):
                    section = sections[section_id - 1]
                    section_2 = sections[section_id_2 - 1]

                    section.timestamp, section_2.timestamp = section_2.timestamp, section.timestamp
                    session.commit()

                    return await update_list(ctx, todo_id)
        except ValueError:
            pass

        await ctx.send('Please input valid second section id')
Ejemplo n.º 4
0
async def parse_section_args(
        ctx: Context,
        args: tuple[str]) -> tuple[Optional[str], Optional[int], tuple[str]]:
    todo_id, args = await parse_todo_args(ctx, args)

    if todo_id:
        if args:
            try:
                section = int(args[0])

                with Session() as session:
                    num_of_sections = session.query(TodoSect).where(
                        TodoSect.server_id == ctx.guild.id,
                        TodoSect.todo_id == todo_id).count()
                if num_of_sections > 0:
                    if 1 <= section <= num_of_sections:
                        return todo_id, section, args[1:]
                else:
                    await ctx.send('Specified list doesnt have any sections')
            except ValueError:
                pass

        await ctx.send('Please input valid section id')

    return todo_id, None, args
Ejemplo n.º 5
0
    async def prefix(self, ctx: Context, new_prefix=None):
        """Set prefix to address bot"""
        if new_prefix is None:
            embed = Embed(description='Set prefix to address bot')
            embed.set_author(name=f'{self.bot.user.name} settings', icon_url=self.bot.user.avatar_url)
            embed.add_field(name='Actual prefix', value=f'`{ctx.prefix}`', inline=False)
            embed.add_field(name='Usage', value=f'`{ctx.prefix}settings prefix [new prefix]`')
            return await ctx.send(embed=embed)
        else:
            if new_prefix in PREFIX_BLACKLIST:
                return await ctx.send('This prefix is blacklisted')
            if len(new_prefix) > 3:
                return await ctx.send('This prefix is too long')

            with Session() as session:
                actual_prefix = session.query(Rules).where(Rules.server == ctx.guild.id, Rules.type == 'prefix').first()

                if actual_prefix:
                    actual_prefix.value = new_prefix
                else:
                    new_rule = Rules()
                    new_rule.server = ctx.guild.id
                    new_rule.type = 'prefix'
                    new_rule.value = new_prefix
                    session.add(new_rule)
                session.commit()

            await ctx.send(f'Changed prefix to `{new_prefix}`')
Ejemplo n.º 6
0
def parse_possible_champion_keys(name: tuple[str]) -> tuple[set[str], str]:
    parsed = [
        x.capitalize() for segment in name if segment != '\''
        for x in segment.split('\'')
    ]

    res = set()

    if not parsed:
        return res, ''

    capitalized_segments = ''.join(parsed)
    capitalized = capitalized_segments.capitalize()

    with Session() as session:
        parsedname = parse_champion_name(capitalized)

        champ = session.query(LeaguechampsKeyCache).where(
            LeaguechampsKeyCache.parsedname == parsedname).first()
        if champ:
            res.add(champ.key)

        res.update([capitalized, capitalized_segments])

        champ = session.query(Leaguechamps).where(
            Leaguechamps.parsedname == parsedname).first()
        if champ:
            res.add(champ.name)

    return res, parsedname
Ejemplo n.º 7
0
    async def cleardb(self, ctx: Context):
        with Session() as session:
            for model in Base.registry.mappers:
                session.query(model.class_).delete()
            session.commit()

        print('Cleared database')
        await ctx.send('Cleared database')
Ejemplo n.º 8
0
def get_prefix(bot: Bot, message: Message) -> list[str]:
    if message.guild is None:
        prefix = DEFAULT_PREFIX
    else:
        with Session() as session:
            rule = session.query(Rules).where(Rules.server == message.guild.id, Rules.type == 'prefix').first()
            prefix = rule.value if rule else DEFAULT_PREFIX

    return commands.when_mentioned_or(prefix)(bot, message)
Ejemplo n.º 9
0
def generate_unique_id(server_id: int, length: int = 7) -> str:
    allowed_chars = list(string.ascii_uppercase) + list(range(10))

    new_id = ''
    check_if_exists = True
    while check_if_exists:
        new_id = ''.join(
            [str(random.choice(allowed_chars)) for _ in range(length)])

        with Session() as session:
            check_if_exists = get_todo_list(session, server_id, new_id)
    return new_id
Ejemplo n.º 10
0
    async def done(self, ctx: Context, *args: str):
        todo_id, section_id, args = await parse_section_args(ctx, args)

        if not (todo_id and section_id):
            return

        with Session() as session:
            sections = get_todo_sects(session, ctx.guild.id, todo_id)
            section = sections[section_id - 1]

            section.done = not section.done
            session.commit()

        await update_list(ctx, todo_id)
Ejemplo n.º 11
0
    async def show(self, ctx: Context):
        with Session() as session:
            lists = get_all_todo_lists(session, ctx.guild.id)

        embed = Embed(color=random_color())

        if not lists:
            embed.add_field(name=EMBED_EMPTY_VAL, value='empty', inline=False)

        embed.set_author(name='Todo Lists')
        for item in lists:
            embed.add_field(name=item.title, value=item.todo_id, inline=False)

        await ctx.send(embed=embed)
Ejemplo n.º 12
0
async def update_list(ctx: Context, todo_id: str) -> None:
    with Session() as session:
        todo_list = get_todo_list(session, ctx.guild.id, todo_id)
        sections = get_todo_sects(session, ctx.guild.id, todo_id)

    if not todo_list:
        return

    msg = await fetch_message(ctx, todo_list.msg_id)

    if not msg:
        return

    embed = create_todo_embed(todo_list, sections)
    await msg.edit(embed=embed)
Ejemplo n.º 13
0
    async def delete(self, ctx: Context, *args: str):
        todo_id, sink = await parse_todo_args(ctx, args)

        if not todo_id:
            return

        with Session() as session:
            to_delete = get_todo_list(session, ctx.guild.id, todo_id)

            msg = await fetch_message(ctx, to_delete.msg_id)
            if msg:
                await msg.delete()

            session.query(TodoSect).where(TodoSect.server_id == ctx.guild.id, TodoSect.todo_id == todo_id).delete()
            session.delete(to_delete)

            session.commit()
Ejemplo n.º 14
0
    async def title(self, ctx: Context, *args: str):
        todo_id, args = await parse_todo_args(ctx, args)

        if not todo_id:
            return

        new_title = ' '.join(args)
        if new_title == '':
            new_title = 'Title'

        with Session() as session:
            todo_list = get_todo_list(session, ctx.guild.id, todo_id)

            todo_list.title = new_title
            session.commit()

        await update_list(ctx, todo_id)
Ejemplo n.º 15
0
    async def content(self, ctx: Context, *args: str):
        todo_id, section_id, args = await parse_section_args(ctx, args)

        if not (todo_id and section_id):
            return

        value = ' '.join(args).replace('\\n', '\n')
        if value == '':
            value = 'Title'

        with Session() as session:
            sections = get_todo_sects(session, ctx.guild.id, todo_id)
            section = sections[section_id - 1]

            section.content = value
            session.commit()

        await update_list(ctx, todo_id)
Ejemplo n.º 16
0
    async def title(self, ctx: Context, *args: str):
        todo_id, section_id, args = await parse_section_args(ctx, args)

        if not (todo_id and section_id):
            return

        title = ' '.join(args)
        if title == '':
            title = 'Title'

        with Session() as session:
            sections = get_todo_sects(session, ctx.guild.id, todo_id)
            section = sections[section_id - 1]

            section.title = title

            session.commit()

        await update_list(ctx, todo_id)
Ejemplo n.º 17
0
    async def add(self, ctx: Context, *args: str):
        todo_id, title = await parse_todo_args(ctx, args)
        if not todo_id:
            return

        title = ' '.join(title)
        if title == '':
            title = 'Section'

        with Session() as session:
            section = TodoSect()
            section.todo_id = todo_id
            section.server_id = ctx.guild.id
            section.title = title

            session.add(section)
            session.commit()

        await update_list(ctx, todo_id)
Ejemplo n.º 18
0
async def parse_todo_args(
        ctx: Context, args: tuple[str]) -> tuple[Optional[str], tuple[str]]:
    """Gets command context and arguments then returns todo_id and rest of args"""
    with Session() as session:
        # check if valid todo_list was referenced
        if ctx.message.reference:
            todo_list = get_todo_list_by_msg(session, ctx.guild.id,
                                             ctx.message.reference.message_id)
            if todo_list:
                return todo_list.todo_id, args

        # else check if first argument was walid todo_id
        if len(args) > 0:
            todo_list = get_todo_list(session, ctx.guild.id, args[0])

            if todo_list:
                return args[0], args[1:]

        await ctx.send('Cant find specified list')
        return None, args
Ejemplo n.º 19
0
    async def create(self, ctx: Context, *title: str):
        title = ' '.join(title)
        if title == '':
            title = 'Title'

        color = random_color()

        new_id = generate_unique_id(ctx.guild.id)

        with Session() as session:
            new_list = Todolist()
            new_list.todo_id = new_id
            new_list.server_id = ctx.guild.id
            new_list.title = title
            new_list.color = color

            session.add(new_list)
            session.commit()

        await ctx.invoke(self.recreate, new_id)
Ejemplo n.º 20
0
    async def recreate(self, ctx: Context, *args: str):
        todo_id, sink = await parse_todo_args(ctx, args)

        if not todo_id:
            return

        with Session() as session:
            res = get_todo_list(session, ctx.guild.id, todo_id)

            msg = await fetch_message(ctx, res.msg_id)
            if msg:
                await msg.delete()

            sections = get_todo_sects(session, ctx.guild.id, todo_id)

            embed = create_todo_embed(res, sections)
            new_msg = await ctx.send(embed=embed)

            res.msg_id = new_msg.id
            session.commit()
Ejemplo n.º 21
0
    async def color(self, ctx: Context, *args: str):
        todo_id, args = await parse_todo_args(ctx, args)

        if not todo_id:
            return

        if not args:
            return await ctx.send('Please input color')

        try:
            color = int(args[0].strip('#'), 16)
        except ValueError:
            return await ctx.send('Please input valid hex color')

        color = max(0, min(color, 0xFFFFFF))

        with Session() as session:
            todo_list = get_todo_list(session, ctx.guild.id, todo_id)

            todo_list.color = color
            session.commit()

        await update_list(ctx, todo_id)