async Task Link(IUser discordUser, TownUser user, int altaId, string linkToken) { if (user.AltaInfo == null) { user.AltaInfo = new UserAltaInfo(); } if (user.AltaInfo.Identifier == altaId) { await ReplyAsync(discordUser.Mention + ", " + "Already connected!"); await Context.Message.DeleteAsync(); await AccountService.UpdateAsync(user, (SocketGuildUser)discordUser); return; } if (user.AltaInfo.Identifier != 0) { await ReplyAsync(discordUser.Mention + ", " + $"Unlinking your Discord from {user.AltaInfo.Username}..."); await Context.Message.DeleteAsync(); user.Unlink(); Database.Users.Update(user); } TownUser existing = Database.Users.FindByIndex(altaId, "alta_id-index", "AltaId"); if (existing != null && existing.UserId != discordUser.Id) { var olddiscorduser = Context.Client.GetUser(existing.UserId); await ReplyAsync(discordUser.Mention + ", " + $"Unlinking your Alta account from {olddiscorduser?.Mention}..."); await Context.Message.DeleteAsync(); existing.Unlink(); Database.Users.Update(existing); } var userInfo = await AltaApi.ApiClient.UserClient.GetUserInfoAsync(altaId); user.AltaId = altaId; user.AltaInfo.Identifier = altaId; user.AltaInfo.Username = userInfo.Username; Database.Users.Update(user); try { if (linkToken != null) { await AltaApi.ApiClient.Account.LinkDiscordAccount(linkToken, discordUser.Id); } } catch (Exception e) { Console.WriteLine("Failed linking to discord in the ATT Database"); } await ReplyAsync(Context.User.Mention + ", " + $"Successfully linked to your Alta account! Hey there {user.AltaInfo.Username}!"); await Context.Message.DeleteAsync(); //await AccountService.UpdateAsync(user, (SocketGuildUser)discordUser); }