Skip to content

Writing a plugin

A Cicord plugin is a small TypeScript (or JavaScript) module that runs inside the runtime and reacts to Discord: gateway events, slash commands (with subcommands and autocomplete), buttons and select menus, modals, schedules, background tasks, events from other plugins and HTTP routes for the community portal. There are no Node.js APIs: every side effect goes through the ctx object, and each call is checked against the capabilities bot.yaml grants.

  1. Scaffold the plugin:

    Terminal window
    cicord plugin new plugins/hello

    It prints the snippet to add to your bot:

    bots/bot/bot.yaml
    spec:
    plugins:
    - name: hello
    source: ../../plugins/hello
    grants: [discord.messages.send]
  2. Run it on the simulator. Every save hot-reloads the plugin:

    Terminal window
    cicord dev --sim

    Open http://127.0.0.1:7420/admin/, sign in as a simulated user, and run /hello from the Simulator panel.

  3. Type the config whenever config.schema.json changes:

    Terminal window
    cicord plugin typegen plugins/hello
  4. Check it like the runtime does, then smoke-test it on the simulator:

    Terminal window
    cicord plugin build plugins/hello
    cicord plugin test plugins/hello
  5. Unit-test it with @cicord/sdk/testing (see Testing and limits):

    Terminal window
    cd plugins/hello && npm install && npm test
  • Directoryplugins/hello/
    • plugin.yaml the manifest: static, reviewed in pull requests
    • config.schema.json JSON Schema of the instance config
    • tsconfig.json maps @cicord/sdk to .cicord/cicord-sdk.d.ts
    • Directory.cicord/
      • cicord-sdk.d.ts the SDK types (cicord plugin sdk-types rewrites them)
    • Directorysrc/
      • index.ts export default definePlugin({...})
      • cicord-env.d.ts typed ctx.config, generated by cicord plugin typegen
    • package.json unit tests only: node --test
    • Directorytest/
      • hello.test.ts an example unit test
    • Directoryvendor/
      • cicord-sdk-0.1.0.tgz @cicord/sdk, embedded in the cicord binary
    • .gitignore

The runtime ignores package.json, test/ and vendor/: it bundles src/ itself. Commit vendor/ so the tests work without npm publication of @cicord/sdk. Once the SDK is on npm, cicord plugin new --from-npm depends on the registry instead. A cicord built without the portal kit (make build only) scaffolds the plugin without the test setup and says so.

The scaffolded plugin is a single command:

plugins/hello/src/index.ts
import { command, definePlugin } from "@cicord/sdk";
export default definePlugin({
commands: {
hello: command({
description: "Say hello",
async run(ctx, interaction) {
await interaction.reply(ctx.config.greeting);
},
}),
},
});

The manifest is static and reviewable. Everything dynamic (commands, handlers, routes) comes from the code, and the builder checks that both agree. Schema: spec/schemas/plugin.schema.json.

plugins/welcome/plugin.yaml
apiVersion: cicord.dev/v1alpha1
kind: Plugin
metadata:
name: welcome # key: lowercase, digits, '-'
version: 0.1.0 # semver
displayName: Welcome # ≤ 64 characters
description: Greets new members… # ≤ 280 characters
license: Apache-2.0 # SPDX identifier
spec:
engine: ">=0.1.0" # runtime versions this plugin supports
entry: ./src/index.ts
capabilities:
[
discord.messages.send,
discord.channels.read,
discord.members.read,
discord.roles.assign,
discord.dm.send,
portal.links,
storage.kv,
]
intents: [ServerMembers]
config:
schema: ./config.schema.json
# network: # required when http.fetch is requested
# allow: [api.example.com, "*.example.org"]
Field Description
metadata.name, metadata.version Required. The plugin key and its semver version.
metadata.displayName, description, authors, license, homepage, keywords (≤ 10) Shown in the Store.
spec.engine Required. Semver range of supported Cicord versions.
spec.entry Required. Relative path of the module that default-exports definePlugin(...).
spec.capabilities What the plugin may do (below).
spec.intents Gateway intents its event handlers need (below).
spec.network.allow Hosts reachable with http.fetch. Required when http.fetch is requested.
spec.config.schema Relative path of the config JSON Schema.
spec.routes.types Relative path of the route contract for typed portal clients.
spec.client.package Optional npm package with React components for portals.

Every capability in spec.capabilities must also be listed in the instance’s grants in bot.yaml. Otherwise cicord validate fails (“requests capabilities that are not granted”), so a plugin update that asks for more shows up in review. At run time, a call that is not granted throws a CapabilityError whose capability field names the missing one.

Capability Allows
discord.messages.read channel.messages() (history)
discord.messages.send channel.send(), message.reply()
discord.messages.manage Edit or delete other authors’ messages
discord.reactions.manage message.react()
discord.members.read ctx.server.member()
discord.members.manage member.setNickname()
discord.members.moderate member.timeout(), kick(), ban()
discord.roles.assign member.addRole(), removeRole()
discord.channels.read ctx.server.channel()
discord.channels.manage ctx.server.createChannel() / deleteChannel() (plugin-owned channels)
discord.threads.manage channel.createThread()
discord.dm.send member.send()
storage.kv ctx.kv
scheduler schedules, tasks, ctx.schedule
http.fetch ctx.http.fetch() to the spec.network.allow hosts
portal.routes routes
portal.links ctx.portal.link()
events.publish / events.subscribe ctx.events.publish() / subscriptions

Answering the plugin’s own interactions (reply, defer, followUp, update, showModal) needs no capability.

Isolation. Each instance only reaches its own server: channel IDs must belong to the instance’s server, or to a DM channel the instance opened or received. Storage, tasks and events are scoped to the instance or to its (bot, server).

spec.network.allow takes public DNS host names with an optional port. *.example.com matches subdomains, not example.com itself. IP literals, localhost, single-label names, reserved suffixes (.local, .internal, .arpa…) and wildcards on a bare TLD are rejected. On every request the runtime also checks that every resolved address is public, connects to the address it checked (no DNS rebinding), re-checks every redirect hop (at most 5), and allows only https. cicord dev relaxes the address checks and allows plain http, so a plugin can call a local service during development; cicord serve never does.

Event handlers need gateway intents, which the manifest must declare. A handler without its intent is a build error.

Handler Intent
memberJoin, memberLeave, memberUpdate ServerMembers (privileged)
messageCreate, messageUpdate, messageDelete ServerMessages; add MessageContent (privileged) to read message.content
reactionAdd, reactionRemove ServerMessageReactions

Message content is gated per plugin. A bot connects with the union of its plugins’ intents, but a plugin that does not declare MessageContent itself still gets an empty message.content (and no attachments), in events and in channel.messages(), unless the message is the bot’s own, mentions the bot or is a DM. Declare it only when the plugin reads what people write.

config.schema.json is JSON Schema 2020-12. The runtime checks each instance’s config against it, per server, after merging overrides.<server>.config. Schema defaults are applied before the plugin sees ctx.config: at the top level, in nested objects, in every object element of arrays and through local $refs (not inside additionalProperties maps nor oneOf/anyOf alternatives). Cicord adds three keywords:

Keyword Values Effect
x-cicord-ref channel, category, role The value is a resource key of the server (a string, or an array of strings). The admin console renders a picker; cicord validate checks the key exists on every server.
x-cicord-widget textarea, markdown, color, duration Rendering hint for the admin console form.
x-cicord-access admin (default), moderator Who may edit the top-level field in the admin console.
plugins/welcome/config.schema.json
{
"type": "object",
"required": ["channel"],
"properties": {
"channel": { "type": "string", "x-cicord-ref": "channel" },
"message": {
"type": "string",
"default": "Welcome {member} to {server}!",
"x-cicord-widget": "textarea",
"x-cicord-access": "moderator"
},
"autoRole": { "type": "string", "x-cicord-ref": "role" },
"dmOnboarding": { "type": "boolean", "default": true }
},
"additionalProperties": false
}

cicord plugin typegen turns the schema into a module augmentation, so ctx.config is typed. A field that is required, or has a default the runtime applies, is non-optional:

src/cicord-env.d.ts (generated, do not edit)
declare module "@cicord/sdk" {
interface PluginConfig {
channel: string;
message: string;
autoRole?: string;
dmOnboarding: boolean;
}
}
export {};

Run cicord plugin typegen --check in CI to catch a stale file.

The runtime bundles plugins itself, with esbuild used as a Go library (no Node.js):

  • The entry is spec.entry, which must export default definePlugin({...}).
  • Every imported module is bundled in. Pure-JS npm libraries work; Node.js built-ins (fs, path, node:*…) are refused at build time.
  • @cicord/sdk is the only module resolved at run time (the host provides it).
  • Target ES2022 (async generators lowered), with an inline source map, so stack traces point at src/index.ts:12:5.
  • At load time the definition is checked against the manifest: a handler without its intent, schedules/tasks without scheduler, routes without portal.routes, subscriptions without events.subscribe, an unknown key in definePlugin(), an invalid command or cron expression all reject the revision, and the previous one keeps running.

cicord plugin build <dir> does exactly this and prints the definition (commands, events, routes); --out also writes the bundle, -o json prints JSON.

Type-check with the scaffolded tsconfig.json, which maps @cicord/sdk to .cicord/cicord-sdk.d.ts and uses "lib": ["ES2023"] (the engine provides the ES2023 built-ins) and "types": [] so no DOM or Node types leak in:

Terminal window
tsc --noEmit -p plugins/hello

The normative SDK contract is spec/sdk/cicord-sdk.d.ts; cicord plugin sdk-types <dir> rewrites the local copy.

Next: the plugin API.