def get_long_field_args(text, title, inline=False, chunk_size=1024): """Returns a list of dicts (to pass as kwargs) given a long text.""" chunks = chunk_text(text, chunk_size) if not chunks: return [] out = [{"name": title, "value": chunks[0].strip(), "inline": inline}] for chunk in chunks[1:]: out.append({"name": "** **", "value": chunk.strip(), "inline": inline}) return out
def set_maybe_long_desc(embed, desc): """ Sets a description that might be longer than 2048 characters but is less than 5000 characters. :param embed: The embed to add the description (and potentially fields) to. :param str desc: The description to add. Will overwrite existing description. """ desc = chunk_text(trim_str(desc, 5000)) embed.description = ''.join(desc[:2]).strip() for piece in desc[2:]: embed.add_field(name="** **", value=piece.strip(), inline=False)
def safe_append(title, desc): if len(desc) < 1024: embed_queue[-1].add_field(name=title, value=desc, inline=False) elif len(desc) < 2048: # noinspection PyTypeChecker # I'm adding an Embed to a list of Embeds, shut up. embed_queue.append(discord.Embed(colour=color, description=desc, title=title)) else: # noinspection PyTypeChecker embed_queue.append(discord.Embed(colour=color, title=title)) trait_all = chunk_text(desc, max_chunk_size=2048) embed_queue[-1].description = trait_all[0] for t in trait_all[1:]: # noinspection PyTypeChecker embed_queue.append(discord.Embed(colour=color, description=t))
def add_field(self, name='', value='', inline=False): """Add a new field to the help embed.""" if len(name) > self.EMBED_TITLE_MAX: raise ValueError("This value is too large to store in an embed field.") if self._current_field: self.close_field() self._current_field_name = name chunks = chunk_text(value, max_chunk_size=self.EMBED_FIELD_MAX) for chunk in chunks: self._field_count += len(value) + 1 self._current_field_inline = inline self._current_field.append(chunk) self.close_field() self._current_field_name = self.CONTINUATION_FIELD_TITLE
def extend_field(self, value): """Add a line of text to the last field in the current embed.""" if not value: return chunks = chunk_text(value, max_chunk_size=self.EMBED_FIELD_MAX - 1) # if the first chunk is too large to fit in the current field, start a new one if self._field_count + len(chunks[0]) + 1 > self.EMBED_FIELD_MAX: self.close_field() self._current_field_name = self.CONTINUATION_FIELD_TITLE # add the rest of the chunks for i, chunk in enumerate(chunks): self._field_count += len(value) + 1 self._current_field.append(chunk) if i < len(chunks) - 1: # if not last chunk, add the chunk in a new field self.close_field() self._current_field_name = self.CONTINUATION_FIELD_TITLE
async def spell(self, ctx, *, name: str): """Looks up a spell.""" choices = await get_spell_choices(ctx, filter_by_license=False) spell = await self._lookup_search3(ctx, {'spell': choices}, name) embed = EmbedWithAuthor(ctx) embed.url = spell.url color = embed.colour embed.title = spell.name school_level = f"{spell.get_level()} {spell.get_school().lower()}" if spell.level > 0 \ else f"{spell.get_school().lower()} cantrip" embed.description = f"*{school_level}. " \ f"({', '.join(itertools.chain(spell.classes, spell.subclasses))})*" if spell.ritual: time = f"{spell.time} (ritual)" else: time = spell.time meta = f"**Casting Time**: {time}\n" \ f"**Range**: {spell.range}\n" \ f"**Components**: {spell.components}\n" \ f"**Duration**: {spell.duration}" embed.add_field(name="Meta", value=meta) higher_levels = spell.higherlevels pieces = chunk_text(spell.description) embed.add_field(name="Description", value=pieces[0], inline=False) embed_queue = [embed] if len(pieces) > 1: for i, piece in enumerate(pieces[1::2]): temp_embed = discord.Embed() temp_embed.colour = color if (next_idx := (i + 1) * 2) < len( pieces ): # this is chunked into 1024 pieces, and descs can handle 2 temp_embed.description = piece + pieces[next_idx] else: temp_embed.description = piece embed_queue.append(temp_embed)