Package
plugin-linked-roles v0.1.0

About
@seedcord/plugin-linked-roles sets up Discord's Linked Roles for your bot. A server puts a requirement on a role, like "100 matches won or more" in your game, and your bot answers it for each member. The plugin registers your records with Discord, serves the verification and callback routes, and checks the values you return against the records you declared.
It works on gateway, on http, and on Cloudflare Workers.
seedcord is in beta, so a minor version can break your bot. Each release's changelog says what changed and how to update.
Installation
pnpm add @seedcord/plugin-linked-rolesUsage
The plugin needs two things from your bot: its public address in server.url, and DISCORD_CLIENT_SECRET in the environment. LinkedRoles.with() returns a plugin class with your options in it. Attach that class:
// src/bot.ts
import { Seedcord } from '@seedcord/http';
import { LinkedRoles, RecordType } from '@seedcord/plugin-linked-roles';
// your own code. it looks up your game's player by their Discord id
import { players } from './players';
const Ranked = LinkedRoles.with({
records: {
matches_won: {
type: RecordType.IntegerGreaterThanOrEqual,
name: 'Matches won',
description: 'or more matches won'
}
},
values: async ({ user }) => {
const player = await players.byDiscordId(user.id);
if (!player) return { refuse: 'Link Discord in the game first.' };
return { platformUsername: player.name, metadata: { matches_won: player.wins } };
}
});
export default new Seedcord({
server: { url: 'https://bot.example.com' },
bot: {
interactions: { path: './handlers' },
commands: { path: './commands' }
},
subscribers: { path: null }
}).attach('linkedRoles', Ranked);Add https://bot.example.com/linked-roles/callback under Redirects on your application's OAuth2 page in the Developer Portal. The plugin logs the exact address at startup if it isn't there yet.
records lists what a server can set a requirement on. The plugin runs values for each member who connects. metadata takes its types from your records, so matches_won has to be a number here. Return { refuse: 'your message' } to turn someone away without writing anything.
A server admin adds the requirement under Server Settings, Roles, then Links on a role. A member then has to claim the role from the server's Linked Roles menu.
Updating a member later
With keepTokens: true, the plugin saves each member's tokens in your bot's kv, so you can write their values again after they change. Your kv then needs a driver that saves to a file or a database.
// in a handler, after the member finished a match
const result = await this.core.linkedRoles.push(this.user.id);
if (result.kind === 'refused') await this.reply(result.message);push() runs your values for that member again and writes what it returns.
// for a member who shouldn't keep the roles that depend on your records
await this.core.linkedRoles.clear(this.user.id);clear() empties the values that Discord has for them. They can open the verify link again, so you have to refuse them in values to keep them out. Both calls only compile with keepTokens: true.
Check out the Linked Roles guide for more details on how to use the plugin.