Source code for sidequest_bot.lib.embeds

 1"""Shared embed styling so every plugin's output looks like one bot."""
 2
 3from typing import TYPE_CHECKING, Final
 4
 5import discord
 6
 7if TYPE_CHECKING:
 8    from collections.abc import Sequence
 9
10DESCRIPTION_LIMIT: Final = 4096
11"""Discord's hard cap on an embed's ``description``."""
12
13
[docs] 14class Colors: 15 """Palette, matching the guild's rank role colors.""" 16 17 GOLD = discord.Colour(0xF1C40F) # Quest Master / level-ups / highlights 18 PURPLE = discord.Colour(0x9B59B6) # Quest Guide / moderation 19 ORANGE = discord.Colour(0xE67E22) # warnings 20 BLUE = discord.Colour(0x3498DB) # info 21 GREEN = discord.Colour(0x2ECC71) # success / joins / role added 22 RED = discord.Colour(0xE74C3C) # destructive: deletes, bans, leaves 23 SLATE = discord.Colour(0x607D8B) # neutral / edits 24 SITE_GOLD = discord.Colour(0xD8AB5C) # sidequest-guild.com's gold: public panels such as role menus 25 BLIZZARD_BLUE = discord.Colour(0x148EFF) # Blizzard blue posts in the WoW: Forever feed 26 WOWHEAD_RED = discord.Colour(0xC41F1F) # Wowhead articles in the WoW: Forever feed
27 28
[docs] 29def pack_lines(lines: Sequence[str], limit: int = DESCRIPTION_LIMIT) -> list[str]: 30 """Greedily pack ``lines`` into newline-joined chunks no longer than ``limit`` characters.""" 31 chunks: list[str] = [] 32 current: list[str] = [] 33 current_len = 0 34 for line in lines: 35 added = len(line) + (1 if current else 0) 36 if current and current_len + added > limit: 37 chunks.append("\n".join(current)) 38 current = [line] 39 current_len = len(line) 40 else: 41 current.append(line) 42 current_len += added 43 if current: 44 chunks.append("\n".join(current)) 45 return chunks or [""]
46 47
[docs] 48def make_embed( 49 title: str | None = None, 50 description: str | None = None, 51 *, 52 color: discord.Colour = Colors.BLUE, 53 timestamp: bool = True, 54) -> discord.Embed: 55 """Embed with the house color and, by default, a timestamp.""" 56 return discord.Embed( 57 title=title, 58 description=description, 59 colour=color, 60 timestamp=discord.utils.utcnow() if timestamp else None, 61 )
62 63
[docs] 64def user_line(user: discord.Member | discord.User) -> str: 65 """``@mention (name, ID 123)``; renders even if the user has left.""" 66 return f"{user.mention} (`{user}`, ID `{user.id}`)"
67 68
[docs] 69def truncate(text: str, limit: int = 1024) -> str: 70 """Trim ``text`` to fit an embed field (default 1024 chars).""" 71 return text if len(text) <= limit else text[: limit - 1] + "…"
72 73 77 78