# LinkedRoles

`class` in `plugin-linked-roles` · v0.1.0

<https://seedcord.org/docs/packages/plugin-linked-roles/0.1.0/classes/linked-roles>

Sets up Discord's Linked Roles for your bot. Attach what [`with`](/docs/packages/plugin-linked-roles/0.1.0/classes/linked-roles#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`.

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

## Constructors

### constructor

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

Constructs a new instance of the `Plugin` class

## Properties

### core

```ts
protected core: TCore
```

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

### dispatchMembers

```ts
readonly dispatchMembers?: DispatchMemberFunctions
```

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.

### kv

```ts
protected kv: 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.

### logger

```ts
protected readonly logger: Logger
```

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

### options

```ts
protected abstract readonly options: LinkedRolesOptions<TRecords, TScopes, TKeep>
```

## Methods

### clear

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

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`](/docs/packages/plugin-linked-roles/0.1.0/classes/linked-roles#push) writes
their values again.

A cleared user can open [`verifyUrl`](/docs/packages/plugin-linked-roles/0.1.0/classes/linked-roles#verify-url) again, and the plugin then writes what your
`values` returns for them. To keep them out, you have to refuse them in `values`.

### codegen

```ts
async codegen?(): Promise<unknown>
```

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.

### dispose

```ts
dispose?(): Promisable<void>
```

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.

### fetch

```ts
public override async fetch(
    request: Request
): Promise<Response | undefined>
```

### init

```ts
public init(): void
```

### onHmr

```ts
onHmr(_event: HmrUpdateEvent): Promisable<void>
```

Override to reload plugin state on an HMR update.

### prepareCommands

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

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.

### push

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

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`](/docs/packages/plugin-linked-roles/0.1.0/types/connection-result). Its `kind` is `'connected'` after a write. When your
`values` refused this user, its `kind` is `'refused'` and it has the `message`.

### ready

```ts
public override async ready(): Promise<void>
```

Registers your records with Discord and sets the application's Linked Roles Verification URL to
[`verifyUrl`](/docs/packages/plugin-linked-roles/0.1.0/classes/linked-roles#verify-url). 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.

### registerCommandFiles

```ts
protected registerCommandFiles(patterns: string[]): void
```

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

### registerCriticalFiles

```ts
protected registerCriticalFiles(
    patterns: string[]
): void
```

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

### rejectOptions

```ts
protected rejectOptions(reason: string): never
```

Rejects the options this plugin got.

### verifyUrl

```ts
public verifyUrl(): string
```

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.

### with

```ts
public static with<TRecords, TScopes, TKeep>(options: LinkedRolesOptions<TRecords, TScopes, TKeep>): new (host: CoreBase) => LinkedRoles<TRecords, TScopes, TKeep>
```

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