Exemplo n.º 1
0
 def add_field(self, *, name, value, inline=True):
     if len(super().fields) < _max_fields:
         name = neko.ellipses(name, _max_field_name)
         value = neko.ellipses(value, _max_field_cont)
         super().add_field(name=name, value=value, inline=inline)
     else:
         raise FullEmbedError
Exemplo n.º 2
0
    def __init__(
            self,
            *,
            title: str = EmptyEmbedField,
            description: str = EmptyEmbedField,
            color: int = EmptyEmbedField,
            colour: int = EmptyEmbedField,
            url: str = EmptyEmbedField,
            # TODO: find out the data type for this.
            timestamp=EmptyEmbedField,
            **kwargs):

        if colour is EmptyEmbedField and color is not EmptyEmbedField:
            colour = color

        if title != EmptyEmbedField:
            title = neko.ellipses(title, _max_title)
        if description != EmptyEmbedField:
            description = neko.ellipses(description, _max_desc)
        super().__init__(title=title,
                         description=description,
                         url=url,
                         colour=colour,
                         timestamp=timestamp,
                         **kwargs)
Exemplo n.º 3
0
 def set_field_at(self, index, *, name, value, inline=True):
     name = neko.ellipses(name, _max_field_name)
     value = neko.ellipses(value, _max_field_cont)
     super().set_field_at(index=index,
                          name=name,
                          value=value,
                          inline=inline)
Exemplo n.º 4
0
 def __getattribute__(self, item):
     # Unless we do this in this way
     if item == 'title':
         title = super().title
         if title != EmptyEmbedField:
             return neko.ellipses(title, _max_title)
         else:
             return title
     elif item == 'description':
         desc = super().description
         if desc != EmptyEmbedField:
             return neko.ellipses(desc, _max_desc)
         else:
             return desc
     else:
         return getattr(super(), item)
Exemplo n.º 5
0
 def set_author(self,
                *,
                name,
                url=EmptyEmbedField,
                icon_url=EmptyEmbedField):
     name = neko.ellipses(name, _max_auth_name)
     super().set_author(name=name, url=url, icon_url=icon_url)
Exemplo n.º 6
0
 def set_footer(self, *, text=EmptyEmbedField, icon_url=EmptyEmbedField):
     text = neko.ellipses(text, _max_footer)
     super().set_footer(text=text, icon_url=icon_url)
Exemplo n.º 7
0
def make_palette(bytes_io, *strings):
    """
    Generates a palette of the given hex string colours
    """
    if len(strings) == 0:
        raise ValueError('No input...')

    # Dict mapping hex to rgb tuples.
    colours = []

    for colour in strings:
        colour = colour.upper()
        # Don't display more than 12 chars
        display = neko.ellipses(colour, 12)
        if all(c.isspace() or c.isalpha() for c in colour):
            # Colour name.
            colours.append((display, HtmlNames[colour]))
        elif colour.count(' ') == 0:
            colours.append((display, from_hex(colour)))
        else:
            chans = colour.split(' ')
            if len(chans) != 3:
                raise ValueError(
                    'Expected colour name, hex or RGB/RGBA bytes.')
            else:
                r, g, b = (ensure_int_in_0_255(chan) for chan in chans)
                colours.append((display, (r, g, b)))

    rows = math.ceil(len(colours) / _pal_colours_per_row)
    cols = min(_pal_colours_per_row, len(colours))

    im_width = cols * _pal_width_per_colour
    im_height = (rows - 1) * _pal_height_per_colour + _pal_height_per_colour

    print(im_height, im_height, rows, cols)

    image = pil_image.new('RGBA', (im_width, im_height), _pal_backing_colour)

    pen = pil_pen.Draw(image)

    for i, (name, (r, g, b)) in enumerate(colours):
        col = i % cols
        row = int(i / cols)

        # Get the start coordinates
        xs, ys = col * _pal_width_per_colour, row * _pal_height_per_colour

        pen.rectangle(
            (xs, ys, xs + _pal_width_per_colour, ys + _pal_height_per_colour),
            (r, g, b, 255))

        text_xs, text_ys = _pal_rel_text_location
        text_xs += xs
        text_ys += ys

        # Adds text in two colours.
        inverted = invert(r, g, b)
        pen.text((text_xs, text_ys), name, fill=inverted, font=font)

    image.save(bytes_io, 'PNG')
    bytes_io.seek(0)
Exemplo n.º 8
0
    async def get_word(self, ctx, *, word: str):
        """
        Gets a definition of a given word or phrase from WordNik
        """
        def _define():
            # Much complex. Very definition. Such API! Wow!
            api = wordapi.WordApi(self.client)

            # *prays to god this isn't lazy iterative.
            return api.getDefinitions(word,
                                      sourceDictionaries=wordnik_dictionaries,
                                      includeRelated=True)

        with ctx.typing():
            words: typing.List[
                wordnik_definition.Definition] = await ctx.bot.do_job_in_pool(
                    _define)

        # Attempt to favour gcide and wordnet, as they have better definitions
        # imho.
        # Fixes #9
        if not words:
            await ctx.send('I couldn\'t find a definition for that.',
                           delete_after=10)
        else:

            front = []
            back = []

            for word in words:
                if word.sourceDictionary in ('gcide', 'wordnet'):
                    front.append(word)
                else:
                    back.append(word)

            # Re-join.
            words = [*front, *back]

            words: typing.List[wordnik_definition.Definition] = [
                word for word in words
                if not word.sourceDictionary.startswith('ahd')
            ]

            # Max results to get is 100.
            max_count = min(100, len(words))

            book = neko.Book(ctx)

            for i in range(0, max_count):
                word = words[i]

                text = ''
                if word.partOfSpeech:
                    text += f'**{word.partOfSpeech}** '

                if word.text:
                    text += word.text

                if word.extendedText:
                    text += '\n\n'
                    text += word.extendedText

                page = neko.Page(title=neko.capitalize(word.word),
                                 description=text,
                                 color=neko.random_colour())

                if word.exampleUses:
                    example = word.exampleUses[0]
                    ex_text = neko.ellipses(example.text, 800)

                    page.add_field(name='Example', value=ex_text, inline=False)

                if word.relatedWords:

                    related = ', '.join(
                        [', '.join(rw.words) for rw in word.relatedWords])

                    page.add_field(name='Synonyms', value=related)

                if word.textProns:
                    pron = '\n'.join([tp.raw for tp in word.textProns])
                    pron = neko.ellipses(pron, 400)

                    page.add_field(
                        name='Pronunciations',
                        value=pron,
                    )

                if word.score:
                    page.add_field(name='Scrabble score',
                                   value=word.score.value)

                if word.labels:
                    labels = ', '.join(label.text for label in word.labels)
                    labels = neko.ellipses(labels, 300)

                    page.add_field(name='Labels', value=labels)

                if word.notes:
                    notes = []
                    for j, note in enumerate(word.notes):
                        notes.append(f'[{j+1}] {note.value}')

                    notes = neko.ellipses('\n\n'.join(notes), 300)

                    page.add_field(name='Notes', value=notes)

                if word.attributionText:
                    attr = word.attributionText
                else:
                    attr = ('Extracted from '
                            f'{neko.capitalise(word.sourceDictionary)}')

                page.set_footer(text=attr)

                book += page

            await book.send()