mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 00:40:42 +09:00
* Add exp_discord plugin for Discord login and account linking Players log in to the web interface with Discord through OAuth2 with the identify scope, so no bot token is needed. After logging in they get a code from the login page and type /link-discord <code> in game to link that Factorio account. One Discord account can hold up to exp_discord.max_accounts Factorio accounts (default 3), and linking a name moves it off any other Discord account. Sessions and link codes live in memory on the controller plugin, the links are stored in database/exp_discord/accounts.json. The user page shows the linked Discord account and lets the user, or anyone with exp_discord.link.delete, unlink it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Link Discord from the user page and document the setup The user page gets a Link Discord button. It asks the controller for a one-time ticket bound to the logged in user, then goes through the authorize route with it. The callback links the Discord account to that user directly, without the in-game code, and goes back to the user page. The link flow always shows the Discord consent screen. The README covers registering the redirect URL and the config. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: bbassie <17990055+bbassie@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
4.8 KiB
TypeScript
195 lines
4.8 KiB
TypeScript
import * as lib from "@clusterio/lib";
|
|
import { Type, Static } from "@sinclair/typebox";
|
|
|
|
/*
|
|
Data records
|
|
*/
|
|
|
|
/** A Discord account and the Factorio accounts linked to it. */
|
|
export class DiscordAccountRecord {
|
|
constructor(
|
|
/** Discord user id */
|
|
public id: string,
|
|
/** Discord display name at the time of the last login */
|
|
public name: string,
|
|
public factorioNames: string[],
|
|
public updatedAtMs: number = 0,
|
|
public isDeleted: boolean = false,
|
|
) {}
|
|
|
|
static jsonSchema = Type.Object({
|
|
id: Type.String(),
|
|
name: Type.String(),
|
|
factorio_names: Type.Array(Type.String()),
|
|
updated_at_ms: Type.Optional(Type.Number()),
|
|
is_deleted: Type.Optional(Type.Boolean()),
|
|
});
|
|
|
|
toJSON() {
|
|
const json: Static<typeof DiscordAccountRecord.jsonSchema> = {
|
|
id: this.id,
|
|
name: this.name,
|
|
factorio_names: this.factorioNames,
|
|
};
|
|
|
|
if (this.updatedAtMs) {
|
|
json.updated_at_ms = this.updatedAtMs;
|
|
}
|
|
|
|
if (this.isDeleted) {
|
|
json.is_deleted = true;
|
|
}
|
|
|
|
return json;
|
|
}
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(
|
|
json.id,
|
|
json.name,
|
|
json.factorio_names,
|
|
json.updated_at_ms ?? 0,
|
|
json.is_deleted ?? false,
|
|
);
|
|
}
|
|
}
|
|
|
|
/** Users may always act on their own link, otherwise the permission is needed. */
|
|
function selfOrPermission(permission: lib.PermissionName) {
|
|
return (user: lib.IUser, message: lib.MessageRequest) => {
|
|
const playerName = (message.data as { player_name: string }).player_name;
|
|
if (user.name.toLowerCase() !== playerName.toLowerCase()) {
|
|
user.checkPermission(permission);
|
|
}
|
|
};
|
|
}
|
|
|
|
/** Only the user themselves, no permission lets someone link another user to their Discord. */
|
|
function selfOnly(user: lib.IUser, message: lib.MessageRequest) {
|
|
const playerName = (message.data as { player_name: string }).player_name;
|
|
if (user.name.toLowerCase() !== playerName.toLowerCase()) {
|
|
throw new lib.PermissionError("You can only link your own account");
|
|
}
|
|
}
|
|
|
|
const LinkResponse = lib.plainJson(Type.Union([
|
|
Type.Null(),
|
|
Type.Object({
|
|
id: Type.String(),
|
|
name: Type.String(),
|
|
}),
|
|
]));
|
|
|
|
/*
|
|
Link requests
|
|
*/
|
|
|
|
/** Link a player to the Discord account which generated the code, sent when the player uses the code in game. */
|
|
export class DiscordLinkRequest {
|
|
declare ["constructor"]: typeof DiscordLinkRequest;
|
|
static plugin = "exp_discord" as const;
|
|
static type = "request" as const;
|
|
static src = "instance" as const;
|
|
static dst = "controller" as const;
|
|
static Response = LinkResponse;
|
|
|
|
constructor(
|
|
public playerName: string,
|
|
public code: string,
|
|
) {}
|
|
|
|
static jsonSchema = Type.Object({
|
|
player_name: Type.String(),
|
|
code: Type.String(),
|
|
});
|
|
|
|
toJSON() {
|
|
return { player_name: this.playerName, code: this.code };
|
|
}
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(json.player_name, json.code);
|
|
}
|
|
}
|
|
|
|
/** Remove the link of a player, responds with the account it was linked to. */
|
|
export class DiscordUnlinkRequest {
|
|
declare ["constructor"]: typeof DiscordUnlinkRequest;
|
|
static plugin = "exp_discord" as const;
|
|
static type = "request" as const;
|
|
static src = ["control", "instance"] as const;
|
|
static dst = "controller" as const;
|
|
static permission = selfOrPermission("exp_discord.link.delete");
|
|
static Response = LinkResponse;
|
|
|
|
constructor(
|
|
public playerName: string,
|
|
) {}
|
|
|
|
static jsonSchema = Type.Object({
|
|
player_name: Type.String(),
|
|
});
|
|
|
|
toJSON() {
|
|
return { player_name: this.playerName };
|
|
}
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(json.player_name);
|
|
}
|
|
}
|
|
|
|
/** Get the Discord account a player is linked to. */
|
|
export class DiscordLinkGetRequest {
|
|
declare ["constructor"]: typeof DiscordLinkGetRequest;
|
|
static plugin = "exp_discord" as const;
|
|
static type = "request" as const;
|
|
static src = ["control", "instance"] as const;
|
|
static dst = "controller" as const;
|
|
static permission = selfOrPermission("exp_discord.link.get");
|
|
static Response = LinkResponse;
|
|
|
|
constructor(
|
|
public playerName: string,
|
|
) {}
|
|
|
|
static jsonSchema = Type.Object({
|
|
player_name: Type.String(),
|
|
});
|
|
|
|
toJSON() {
|
|
return { player_name: this.playerName };
|
|
}
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(json.player_name);
|
|
}
|
|
}
|
|
|
|
/** Start linking the Discord account from the user page, responds with a ticket for the authorize route. */
|
|
export class DiscordLinkStartRequest {
|
|
declare ["constructor"]: typeof DiscordLinkStartRequest;
|
|
static plugin = "exp_discord" as const;
|
|
static type = "request" as const;
|
|
static src = "control" as const;
|
|
static dst = "controller" as const;
|
|
static permission = selfOnly;
|
|
static Response = lib.plainJson(Type.String());
|
|
|
|
constructor(
|
|
public playerName: string,
|
|
) {}
|
|
|
|
static jsonSchema = Type.Object({
|
|
player_name: Type.String(),
|
|
});
|
|
|
|
toJSON() {
|
|
return { player_name: this.playerName };
|
|
}
|
|
|
|
static fromJSON(json: Static<typeof this.jsonSchema>) {
|
|
return new this(json.player_name);
|
|
}
|
|
}
|