mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-20 07:23:13 +09:00
Expose Discord links to other controller plugins (#525)
exp_discord_auth gains a static get(controller) like exp_roles and exp_groups, getLink/getAccount/getAccounts lookups that return copies, and linked/unlinked AsyncHooks. Moving a player to another Discord account fires unlinked for the old account, then linked for the new one. This is what exp_discord_sync needs to give linked players their Discord roles. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cade0cc4d4
commit
b513b7a637
3 files changed
+132
-2
No files matched your search
@@ -53,3 +53,21 @@ ExpDiscordAuth.link(player, code)
|
||||
ExpDiscordAuth.unlink(player)
|
||||
ExpDiscordAuth.get_linked(player) -- Prints the Discord account the player is linked to
|
||||
```
|
||||
|
||||
## Other plugins
|
||||
|
||||
Controller plugins can read links and react to them. Import the plugin class from the controller entrypoint and look it up with the controller.
|
||||
|
||||
```ts
|
||||
import { ControllerPlugin as DiscordAuth } from "@expcluster/discord-auth/dist/node/controller.js";
|
||||
|
||||
const discordAuth = DiscordAuth.get(controller); // undefined if exp_discord_auth is not loaded
|
||||
discordAuth.getLink("Player1"); // { id, name } of the Discord account, or undefined
|
||||
discordAuth.getAccount(discordId); // { id, name, playerNames }, or undefined
|
||||
discordAuth.getAccounts(); // every linked Discord account
|
||||
|
||||
discordAuth.hooks.linked.attach("my_plugin", (playerName, discordUser) => { /* ... */ });
|
||||
discordAuth.hooks.unlinked.attach("my_plugin", (playerName, discordUser) => { /* ... */ });
|
||||
```
|
||||
|
||||
Moving a player to another Discord account calls `unlinked` for the old account and then `linked` for the new one. A Factorio account is linked to at most one Discord account. Add `@expcluster/discord-auth` as a dependency and a `references` entry for its `tsconfig.node.json`.
|
||||
@@ -14,6 +14,8 @@ const codeLength = 6;
|
||||
const codeLetters = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
|
||||
type DiscordUser = { id: string, name: string };
|
||||
/** A Discord account and the players linked to it, as given to other plugins. */
|
||||
export type DiscordAccount = DiscordUser & { playerNames: string[] };
|
||||
type Session = DiscordUser & { expiresMs: number, code?: string };
|
||||
type LinkCode = { sessionId: string, expiresMs: number };
|
||||
type WebLink = { playerName: string, expiresMs: number };
|
||||
@@ -41,7 +43,22 @@ function randomCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
export class DiscordAuthHooks extends lib.AsyncHookCollection {
|
||||
/** A player was linked to a Discord account. */
|
||||
readonly linked = this.newHook<[playerName: string, discordUser: DiscordUser]>();
|
||||
/** A player was unlinked from a Discord account, also when the link moves to another account. */
|
||||
readonly unlinked = this.newHook<[playerName: string, discordUser: DiscordUser]>();
|
||||
}
|
||||
|
||||
const loaded = new WeakMap<Controller, ControllerPlugin>();
|
||||
|
||||
export class ControllerPlugin {
|
||||
/** The plugin loaded on a controller, for other plugins to read links from. */
|
||||
static get(controller: Controller) {
|
||||
return loaded.get(controller);
|
||||
}
|
||||
|
||||
hooks: DiscordAuthHooks;
|
||||
/** Logins which have been through Discord, by session id */
|
||||
sessions = new Map<string, Session>();
|
||||
/** Codes waiting to be used in game, by code */
|
||||
@@ -58,6 +75,7 @@ export class ControllerPlugin {
|
||||
public name: string,
|
||||
public accounts: lib.SubscribableDatastore<messages.DiscordAccountRecord>,
|
||||
) {
|
||||
this.hooks = new DiscordAuthHooks(logger);
|
||||
controller.handle(messages.DiscordLinkRequest, this.handleLinkRequest.bind(this));
|
||||
controller.handle(messages.DiscordUnlinkRequest, this.handleUnlinkRequest.bind(this));
|
||||
controller.handle(messages.DiscordLinkGetRequest, this.handleLinkGetRequest.bind(this));
|
||||
@@ -100,7 +118,9 @@ export class ControllerPlugin {
|
||||
messages.DiscordAccountRecord.fromJSON.bind(messages.DiscordAccountRecord),
|
||||
));
|
||||
|
||||
return new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
||||
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
||||
loaded.set(controller, plugin);
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async onSave() {
|
||||
@@ -352,6 +372,23 @@ export class ControllerPlugin {
|
||||
Links
|
||||
*/
|
||||
|
||||
/** The Discord account a player is linked to. */
|
||||
getLink(playerName: string): DiscordUser | undefined {
|
||||
const account = this.findAccount(playerName);
|
||||
return account ? { id: account.id, name: account.name } : undefined;
|
||||
}
|
||||
|
||||
getAccount(discordId: string): DiscordAccount | undefined {
|
||||
const account = this.accounts.get(discordId);
|
||||
return account ? { id: account.id, name: account.name, playerNames: [...account.factorioNames] } : undefined;
|
||||
}
|
||||
|
||||
getAccounts(): DiscordAccount[] {
|
||||
return [...this.accounts.values()].map(
|
||||
account => ({ id: account.id, name: account.name, playerNames: [...account.factorioNames] })
|
||||
);
|
||||
}
|
||||
|
||||
findAccount(playerName: string) {
|
||||
const name = playerName.toLowerCase();
|
||||
for (const account of this.accounts.values()) {
|
||||
@@ -371,13 +408,16 @@ export class ControllerPlugin {
|
||||
|
||||
const account = this.accounts.getMutable(found.id)!;
|
||||
const name = playerName.toLowerCase();
|
||||
const removed = account.factorioNames.find(linked => linked.toLowerCase() === name)!;
|
||||
account.factorioNames = account.factorioNames.filter(linked => linked.toLowerCase() !== name);
|
||||
if (account.factorioNames.length) {
|
||||
this.accounts.set(account);
|
||||
} else {
|
||||
this.accounts.delete(account);
|
||||
}
|
||||
return { id: account.id, name: account.name };
|
||||
const discordUser = { id: account.id, name: account.name };
|
||||
void this.hooks.unlinked.invoke(removed, { ...discordUser });
|
||||
return discordUser;
|
||||
}
|
||||
|
||||
async handleLinkRequest(request: messages.DiscordLinkRequest) {
|
||||
@@ -415,6 +455,7 @@ export class ControllerPlugin {
|
||||
account.name = discordUser.name;
|
||||
account.factorioNames = [...account.factorioNames, playerName];
|
||||
this.accounts.set(account);
|
||||
void this.hooks.linked.invoke(playerName, { id: discordUser.id, name: discordUser.name });
|
||||
}
|
||||
return { id: discordUser.id, name: discordUser.name };
|
||||
}
|
||||
|
||||
@@ -282,6 +282,77 @@ t.test("linking from the user page", t2 => {
|
||||
t2.end();
|
||||
});
|
||||
|
||||
t.test("api for other plugins", t2 => {
|
||||
/** Record every linked and unlinked hook call. */
|
||||
function recordHooks(plugin) {
|
||||
const calls = [];
|
||||
plugin.hooks.linked.attach("test", (playerName, discordUser) => {
|
||||
calls.push(["linked", playerName, discordUser]);
|
||||
});
|
||||
plugin.hooks.unlinked.attach("test", (playerName, discordUser) => {
|
||||
calls.push(["unlinked", playerName, discordUser]);
|
||||
});
|
||||
return calls;
|
||||
}
|
||||
|
||||
t2.test("static get() returns the plugin loaded on a controller", async t3 => {
|
||||
const { plugin, controller } = await startPlugin(t3);
|
||||
t3.equal(ControllerPlugin.get(controller), plugin);
|
||||
const other = new Controller(logger, [], new lib.ControllerConfig("controller", {
|
||||
"controller.database_directory": t3.testdir(),
|
||||
}));
|
||||
t3.equal(ControllerPlugin.get(other), undefined);
|
||||
});
|
||||
|
||||
t2.test(".getLink(), .getAccount() and .getAccounts() read links", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const session = await discordLogin(plugin, alice);
|
||||
await link(plugin, session, "Player1");
|
||||
await link(plugin, session, "Player2");
|
||||
await link(plugin, await discordLogin(plugin, bob), "Player3");
|
||||
|
||||
t3.same(plugin.getLink("player1"), alice);
|
||||
t3.equal(plugin.getLink("Player4"), undefined);
|
||||
t3.same(plugin.getAccount(alice.id), { ...alice, playerNames: ["Player1", "Player2"] });
|
||||
t3.equal(plugin.getAccount("300"), undefined);
|
||||
t3.same(plugin.getAccounts(), [
|
||||
{ ...alice, playerNames: ["Player1", "Player2"] },
|
||||
{ ...bob, playerNames: ["Player3"] },
|
||||
]);
|
||||
|
||||
plugin.getAccount(alice.id).playerNames.push("Player4");
|
||||
t3.same(plugin.getAccount(alice.id).playerNames, ["Player1", "Player2"], "returns copies");
|
||||
});
|
||||
|
||||
t2.test("hooks fire on link, move and unlink", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const calls = recordHooks(plugin);
|
||||
const aliceSession = await discordLogin(plugin, alice);
|
||||
await link(plugin, aliceSession, "Player1");
|
||||
await link(plugin, aliceSession, "player1");
|
||||
await link(plugin, await discordLogin(plugin, bob), "Player1");
|
||||
await plugin.handleUnlinkRequest(new messages.DiscordUnlinkRequest("player1"));
|
||||
await plugin.handleUnlinkRequest(new messages.DiscordUnlinkRequest("Player1"));
|
||||
t3.same(calls, [
|
||||
["linked", "Player1", alice],
|
||||
["unlinked", "Player1", alice],
|
||||
["linked", "Player1", bob],
|
||||
["unlinked", "Player1", bob],
|
||||
]);
|
||||
});
|
||||
|
||||
t2.test("hooks fire when linking from the user page", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const calls = recordHooks(plugin);
|
||||
const ticket = await plugin.handleLinkStartRequest(new messages.DiscordLinkStartRequest("Player1"));
|
||||
const state = new URL((await plugin.authorize(ticket)).redirect).searchParams.get("state");
|
||||
plugin.fetchDiscordUser = async () => alice;
|
||||
await plugin.callback({ code: "c", state }, state);
|
||||
t3.same(calls, [["linked", "Player1", alice]]);
|
||||
});
|
||||
t2.end();
|
||||
});
|
||||
|
||||
t.test("permissions", t2 => {
|
||||
const user = (name, granted = []) => ({
|
||||
name,
|
||||
|
||||
Reference in new issue
Block a user