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
botConfigServiceBotConfigurationServicediscordAccountServiceIDiscordAccountServicedbContextFactoryNpgsqlDbContextFactoryloggerILogger<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
discordUserIdulong
Returns
HasPendingVerification(ulong)
Checks if a Discord user has a pending verification.
public bool HasPendingVerification(ulong discordUserId)
Parameters
discordUserIdulong
Returns
ImportLegacyLinksAsync()
Moves the links older bots kept in botdata.json into the database, once.
public Task ImportLegacyLinksAsync()
Returns
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
cancellationTokenCancellationToken
Returns
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
discordUserIdulongThe Discord user ID.
discordUsernamestringThe user's Discord username as Discord reports it, recorded on the account when the link is made.
characterNamestringThe 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
cancellationTokenCancellationTokenIndicates that the shutdown process should no longer be graceful.
Returns
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
characterNamestringThe character name that sent the message.
accountNamestringThe game account name.
messagestringThe 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
discordUserIdulong
Returns
- Task<DatabaseResult<bool>>
True in
Datawhen there was a link to remove.