Table of Contents

Class AccountLinkingService

Namespace
FishMMO.DiscordBot.Services
Assembly
FishMMO-DiscordBot.dll

Manages Discord-to-game account linking. The link itself lives in the database: the account's linked Discord user is a unique column, so one Discord account links one game account, and a verified Discord code and a /link are the same link.

public sealed class AccountLinkingService : IHostedService, IDisposable
Inheritance
AccountLinkingService
Implements
Inherited Members

Remarks

The /link flow is unchanged for the player: they name a character, the bot DMs them a short-lived code, and they type it into game chat, where ChatPollingService sees it. Only the pending code is kept in memory; the confirmed link is written to the database.

Lookups are cached briefly because GameChatBridgeService checks the link on every bridged message. Links made or removed by this bot invalidate their entry at once; a link made elsewhere (a Discord code redeemed in the game or Control Panel) is seen within FishMMO.DiscordBot.Services.AccountLinkingService.LinkCacheLifetime.

On start it imports the links older bots kept in botdata.json; see ImportLegacyLinksAsync().

Constructors

AccountLinkingService(BotConfigurationService, IDiscordAccountService, NpgsqlDbContextFactory, ILogger<AccountLinkingService>)

Initializes a new instance of the AccountLinkingService class.

public AccountLinkingService(BotConfigurationService botConfigService, IDiscordAccountService discordAccountService, NpgsqlDbContextFactory dbContextFactory, ILogger<AccountLinkingService> logger)

Parameters

botConfigService BotConfigurationService
discordAccountService IDiscordAccountService
dbContextFactory NpgsqlDbContextFactory
logger ILogger<AccountLinkingService>

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetLinkedAccountAsync(ulong)

Gets the account a Discord user is linked to, or null in Data when there is none. Successful lookups are cached for FishMMO.DiscordBot.Services.AccountLinkingService.LinkCacheLifetime; failures are not.

public Task<DatabaseResult<DiscordAccountLink?>> GetLinkedAccountAsync(ulong discordUserId)

Parameters

discordUserId ulong

Returns

Task<DatabaseResult<DiscordAccountLink?>>

HasPendingVerification(ulong)

Checks if a Discord user has a pending verification.

public bool HasPendingVerification(ulong discordUserId)

Parameters

discordUserId ulong

Returns

bool

ImportLegacyLinksAsync()

Moves the links older bots kept in botdata.json into the database, once.

public Task ImportLegacyLinksAsync()

Returns

Task

Remarks

Each entry is written with the account's Discord user. An entry is removed from the file when it was imported, or when the database refused it for good — that Discord user is already linked to another account, the account no longer exists, or the name is not a valid account name — and each refusal is logged. An entry that failed transiently stays for the next start.

An account that already has a DIFFERENT Discord user in the database is not overwritten, because the database link is newer than anything in the file: the entry is refused and removed.

StartAsync(CancellationToken)

Starts the one-time botdata.json import in the background.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task

Remarks

Registered after DynamicChannelManagerService, whose start loads botdata.json, so the file is in memory by now. Not awaited: a slow or unreachable database must not hold the bot's start, and an entry that fails transiently simply stays in the file for the next start.

StartLinkRequestAsync(ulong, string?, string)

Starts a link request for the given Discord user and character name.

public Task<(LinkRequestStatus Status, string? Code)> StartLinkRequestAsync(ulong discordUserId, string? discordUsername, string characterName)

Parameters

discordUserId ulong

The Discord user ID.

discordUsername string

The user's Discord username as Discord reports it, recorded on the account when the link is made.

characterName string

The character name claimed by the user.

Returns

Task<(LinkRequestStatus Status, string Code)>

The status, and the verification code the user must type in-game when it started.

StopAsync(CancellationToken)

Triggered when the application host is performing a graceful shutdown.

public Task StopAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Indicates that the shutdown process should no longer be graceful.

Returns

Task

A Task that represents the asynchronous Stop operation.

TryVerifyFromChatAsync(string, string, string)

Checks whether a chat message from the game contains a pending verification code. If it matches, writes the link to the database.

public Task<ChatLinkVerification?> TryVerifyFromChatAsync(string characterName, string accountName, string message)

Parameters

characterName string

The character name that sent the message.

accountName string

The game account name.

message string

The chat message content.

Returns

Task<ChatLinkVerification>

How the link ended, or null when the message carried no pending code.

UnlinkAsync(ulong)

Removes the link for the specified Discord user.

public Task<DatabaseResult<bool>> UnlinkAsync(ulong discordUserId)

Parameters

discordUserId ulong

Returns

Task<DatabaseResult<bool>>

True in Data when there was a link to remove.