Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
4.2 kB · 119 lines
Python
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120from __future__ import annotations
import builtinsfrom collections.abc import Sequenceimport osfrom pathlib import Pathfrom typing import castfrom albedo_api import Host, PythonApi, Record
host: Host
MAX_FILES = 10MAX_FILE_BYTES = 10 * 1024 * 1024
def written(value: str | int | None) -> str | None: return None if value is None else str(value)
def attachments( files: str | os.PathLike[str] | Sequence[str | os.PathLike[str]] | None,) -> builtins.list[str]: if files is None: return [] items = [files] if isinstance(files, (str, os.PathLike)) else list(files) if len(items) > MAX_FILES: raise ValueError(f"Discord accepts at most {MAX_FILES} files per message") resolved: builtins.list[str] = [] for item in items: path = Path(item).expanduser().resolve() if not path.exists(): raise FileNotFoundError(str(item)) if not path.is_file(): raise IsADirectoryError(f"{item!s} is not a file") if path.stat().st_size > MAX_FILE_BYTES: raise ValueError(f"{path.name} exceeds Discord's 10 MiB limit") resolved.append(str(path)) return resolved
class Discord: async def send( self, channel: str, text: str = "", reply_to: str | int | None = None, files: str | os.PathLike[str] | Sequence[str | os.PathLike[str]] | None = None, ) -> Record: """Post in a channel by handle ("c3") or name, optionally replying to a message number or attaching files (a path or list of up to 10 paths, each at most 10 MiB). Text past 2000 characters is split; returns Record(messages, channel): the numbers posted and the handle.""" paths = attachments(files) if not text and not paths: raise ValueError("send needs text or files") value = cast( dict, await host( "discord.send", { "channel": channel, "text": text, "reply_to": written(reply_to), "files": paths, }, ), ) return Record(value)
async def dm( self, user: str, text: str = "", files: str | os.PathLike[str] | Sequence[str | os.PathLike[str]] | None = None, ) -> Record: """DM the bot's owner or an allowed person, optionally attaching files; returns Record(messages, channel).""" paths = attachments(files) if not text and not paths: raise ValueError("dm needs text or files") return Record( cast( dict, await host("discord.dm", {"user": user, "text": text, "files": paths}), ) )
async def history( self, channel: str, limit: int = 20, before: str | int | None = None ) -> builtins.list[Record]: """Recent messages, oldest first: Record(number, author, username, bot, content, timestamp, reply_to, attachments). `before` is a number.""" items = await host( "discord.history", {"channel": channel, "limit": limit, "before": written(before)}, ) return [Record(item) for item in cast(builtins.list[dict], items)]
async def react(self, channel: str, message: str | int, emoji: str) -> None: """React to a message number with a unicode emoji, or name:id for a custom one.""" await host( "discord.react", {"channel": channel, "message": str(message), "emoji": emoji}, )
async def channels(self) -> builtins.list[Record]: """Channels the bot sees: handle, name, guild, delivery, mentions_only.""" items = await host("discord.channels", {}) return [Record(item) for item in cast(builtins.list[dict], items)]
async def status(self) -> Record: """The bot's name, state, detail, user, owner and invite_url.""" return Record(cast(dict, await host("discord.status", {})))
def setup(api: PythonApi) -> dict[str, object]: global host host = api.host return {"discord": Discord(), "DiscordError": api.HostError}