Skip to content
Classplugin-linked-rolesv0.1.0

LinkedRoles

Sets up Discord's Linked Roles for your bot. Attach what with returns.

It serves linked-roles/verify and linked-roles/callback under your bot's server.url. Add the callback's address under Redirects on your application's OAuth2 page. The plugin logs the exact address at startup while it's missing.

It reads the client secret from DISCORD_CLIENT_SECRET.

abstract class LinkedRoles<
    TRecords extends LinkedRoleRecords,
    TScopes extends OAuthScope = never,
    TKeep extends boolean = false
> extends Plugin

Member visibility

Filter class members by access level.

constructor

Constructs a new instance of the Plugin class

Plugin(
    host: CoreBase,
    [spec],
    input: PluginSpecArgs<Opts, Fixed>
)

Inherited from:Plugin

TRecords

The records from the options. Leave it out.

TRecords extends LinkedRoleRecords

TScopes

The scopes from the options. Leave it out.

TScopes extends OAuthScope = never

TKeep

The keepTokens from the options. Leave it out.

TKeep extends boolean = false

core

The host, typed to the transport whose Plugin base this class extends.

protected core: TCore

Inherited from:Plugin

dispatchMembers

Adds values that every handler can read. Each key becomes a member, like this.tag, and its function returns the value. Gates and Notice.render read the same values on ctx.

Seedcord calls each function once per interaction, and once per event that has a handler.

attach() throws if seedcord already uses one of these keys, like reply, if another plugin on the bot uses it, or if the bot attaches this plugin twice.

readonly dispatchMembers?: DispatchMemberFunctions
public override readonly dispatchMembers = {
    tag: (origin: DispatchOrigin) => `${origin.guildId}:${origin.userId}`
};

Inherited from:Plugin

kv

This plugin's records in the bot's config.kv, stored under its attach key. Two attachments of one plugin class each get their own records. Renaming the attach key leaves the records under the old key behind.

protected kv: Kv
// works in a field initializer, the constructor, and every method
private readonly jobs = this.kv.collection<Job>('job');

Inherited from:Plugin

Throws:

  • A SeedcordTypeError when something built the plugin with new outside attach().

logger

Logs under the plugin's class name, on the channel its attach key sets.

protected readonly logger: Logger

Inherited from:Plugin

clear()

Empties the values that Discord has for a user. Use it for a user who shouldn't keep the roles that depend on your records. The plugin keeps their tokens, and a later push writes their values again.

A cleared user can open verifyUrl again, and the plugin then writes what your values returns for them. To keep them out, you have to refuse them in values.

public async clear(
    userId: SavedTokenUserId<TKeep, "clear">
): Promise<void>

Throws:

A SeedcordError with the code PluginOAuthLoginNeeded when the user never connected, or took your application off their account since.

Throws:

A SeedcordTypeError with the code PluginLinkedRolesTokensNotKept when the plugin doesn't have keepTokens: true.

codegen()

Returns data that seedcord codegen writes into the bot's seedcord-gen.ts as a type. Read it back with Generated<typeof YourPlugin>. Return plain data, or classes and objects that a project file or your plugin's package exports. The class needs a static readonly id.

seedcord codegen calls this without running init(). Read only what the constructor stored and the bot's files.

async codegen?(): Promise<unknown>

Inherited from:Plugin

dispose()

Runs during teardown, in ShutdownPhase.Disconnect by default. seedcord skips it when init() has thrown.

When init() outlasts its timeout, seedcord calls this once that init() resolves, outside any shutdown phase. The process must still be alive for that call to happen.

dispose?(): Promisable<void>

Inherited from:Plugin

init()

public init(): void

Throws:

A SeedcordError when server.url or DISCORD_CLIENT_SECRET isn't set.

Throws:

A SeedcordTypeError when the records break one of Discord's limits.

Throws:

A SeedcordTypeError when keepTokens is on and the bot's kv keeps everything in memory.

prepareCommands()

Changes commands before seedcord sends them to Discord. Write your changes onto these objects, like setting name_localizations. Each command appears once, even when it deploys to several guilds.

If it throws, the deploy stops, and so do seedcord codegen, a Workers build, seedcord commands deploy, and seedcord commands list.

The CLI calls this without running init(). Read only what the constructor stored and the bot's files.

prepareCommands?(
    commands: readonly RESTPostAPIApplicationCommandsJSONBody[]
): Promisable<void>

Inherited from:Plugin

push()

Runs your values again for a user who connected, and writes what it returns. Call it when something a role depends on changes, like after a match in your game.

It resolves to a ConnectionResult. Its kind is 'connected' after a write. When your values refused this user, its kind is 'refused' and it has the message.

public async push(
    userId: SavedTokenUserId<TKeep, "push">
): Promise<ConnectionResult>

Throws:

Whatever your values throws.

Throws:

A SeedcordError with the code PluginOAuthLoginNeeded when the user never connected, or took your application off their account since. They have to open verifyUrl again.

Throws:

A SeedcordTypeError with the code PluginLinkedRolesValueRejected when your values returns a value that doesn't fit its record, or a key that isn't in records.

Throws:

A SeedcordRangeError with the code PluginLinkedRolesValueRejected when platformName or platformUsername is longer than Discord takes.

Throws:

A SeedcordTypeError with the code PluginLinkedRolesTokensNotKept when the plugin doesn't have keepTokens: true.

ready()

Registers your records with Discord and sets the application's Linked Roles Verification URL to verifyUrl. A later start skips those calls unless your records or server.url changed, or the last time it made them was more than a day ago.

If the callback address isn't under Redirects on your application's OAuth2 page, it logs the address. Only you can add it there.

public override async ready(): Promise<void>

registerCommandFiles()

Tells seedcord dev which files your prepareCommands reads. Saving one of them in seedcord dev shows the command refresh prompt.

protected registerCommandFiles(patterns: string[]): void
Parameter: patterns

Glob patterns. A relative one resolves against root from seedcord.config.ts. A pattern that starts with ! excludes the files it matches.

Inherited from:Plugin

registerCriticalFiles()

Registers critical file patterns that should trigger a full restart when changed in Dev HMR.

protected registerCriticalFiles(
    patterns: string[]
): void
Parameter: patterns

Glob patterns. A relative one resolves against root from seedcord.config.ts. A pattern that starts with ! excludes the files it matches from the other patterns in this call.

Inherited from:Plugin

rejectOptions()

Rejects the options this plugin got.

protected rejectOptions(reason: string): never
Parameter: reason

Why the options are invalid.

Throws:

A SeedcordTypeError every time, with the plugin class name and reason in its message.

Inherited from:Plugin

verifyUrl()

The address that starts the flow for a user. Discord opens it from a server's Linked Roles menu. Also reply with it from a command of your own because that menu doesn't open it again for someone who is already connected.

public verifyUrl(): string

with()

Returns the plugin class to attach, with your options in it. Attaching LinkedRoles directly doesn't compile because it's abstract.

public static with<TRecords, TScopes, TKeep>(options: LinkedRolesOptions<TRecords, TScopes, TKeep>): new (host: CoreBase) => LinkedRoles<TRecords, TScopes, TKeep>
const Ranked = LinkedRoles.with({
    records: {
        matches_won: {
            type: RecordType.IntegerGreaterThanOrEqual,
            name: 'Matches won',
            description: 'or more matches won'
        }
    },
    // `matches` is your own code, with what your game has for each Discord user
    values: async ({ user }) => ({ metadata: { matches_won: await matches.wonBy(user.id) } })
});

seedcord.attach('linkedRoles', Ranked);