feat: add dicebear support

This commit is contained in:
Teesh 2025-03-17 13:41:34 +02:00
parent 698a12b693
commit a96b3bf858
4 changed files with 90 additions and 1 deletions

46
src/apis/dicebear.py Normal file
View file

@ -0,0 +1,46 @@
from enum import Enum
class Style(Enum):
Adventurer = "adventurer"
AdventurerNeutral = "adventurer-neutral"
Avataaars = "avataaars"
AvataaarsNeutral = "avataaars-neutral"
BigEars = "big-ears"
BigEarsNeutral = "big-ears-neutral"
BigSmile = "big-smile"
Bottts = "bottts"
BotttsNeutral = "bottts-neutral"
Croodles = "croodles"
CroodlesNeutral = "croodles-neutral"
Dylan = "dylan"
FunEmoji = "fun-emoji"
Glass = "glass"
Icons = "icons"
Identicon = "identicon"
Initials = "initials"
Lorelei = "lorelei"
LoreleiNeutral = "lorelei-neutral"
Micah = "micah"
Miniavs = "miniavs"
Notionists = "notionists"
NotionistsNeutral = "notionists-neutral"
OpenPeeps = "open-peeps"
Personas = "personas"
PixelArt = "pixel-art"
PixelArtNeutral = "pixel-art-neutral"
Rings = "rings"
Shapes = "shapes"
Thumbs = "thumbs"
ALL_STYLES = [style.value for style in Style]
def get_avatar_url(style: Style, seed: str, **kwargs) -> str:
url = f"https://api.dicebear.com/9.x/{style.value}/png?seed={seed}"
for k, v in kwargs.items():
url += f"&{k}={v}"
return url

View file

@ -1,7 +1,6 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import app_commands from discord import app_commands
import random
import pollinations as ai import pollinations as ai
SYSTEM_PROMPT = """ SYSTEM_PROMPT = """

View file

@ -0,0 +1,44 @@
import discord
from discord.ext import commands
from discord import app_commands
import apis.dicebear as db
async def style_autocomplete(interaction: discord.Interaction, current: str):
try:
current = current or "" # Ensure current is a string
return [
app_commands.Choice(name=style, value=style)
for style in db.ALL_STYLES
if current in style
]
except Exception as e:
print(f"Autocomplete error: {e}")
return [] # Return empty list to avoid breaking autocomplete
class Dicebear(commands.GroupCog, name="dicebear"):
def __init__(self, bot):
super().__init__()
self.bot = bot
@app_commands.command(name="generate", description="Generate an avatar")
@app_commands.autocomplete(style=style_autocomplete)
async def generate(self, interaction: discord.Interaction, seed: str, style: str):
url = db.get_avatar_url(db.Style(style), seed)
embed = discord.Embed(title=f"{style} on {seed}:", color=discord.Color.yellow())
embed.set_image(url=url)
await interaction.response.send_message(embed=embed)
@app_commands.command(name="get-styles", description="Get all available styles")
async def get_styles(self, interaction: discord.Interaction):
embed = discord.Embed(
title="All available styles:",
description="\n".join(db.ALL_STYLES),
color=discord.Color.yellow(),
)
await interaction.response.send_message(embed=embed)
async def setup(bot: commands.Bot):
await bot.add_cog(Dicebear(bot))