/// <summary> /// Executes the job /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public override async Task ExecuteAsync() { var date = DateTime.Today.AddDays(-1); using (var dbFactory = RepositoryFactory.CreateInstance()) { foreach (var account in dbFactory.GetRepository <AccountRepository>() .GetQuery() .Select(obj => new { obj.Name, obj.ApiKey, obj.LastAge, WordId = obj.WorldId }) .ToList()) { try { var connector = new GuidWars2ApiConnector(account.ApiKey); await using (connector.ConfigureAwait(false)) { var accountInformation = await connector.GetAccountInformationAsync() .ConfigureAwait(false); if (accountInformation.Age != account.LastAge) { dbFactory.GetRepository <AccountDailyLoginCheckRepository>() .Add(new GuildWarsAccountDailyLoginCheckEntity { Name = account.Name, Date = date }); dbFactory.GetRepository <AccountRepository>() .Refresh(obj => obj.Name == account.Name, obj => { obj.LastAge = accountInformation.Age; obj.WorldId = accountInformation.World; obj.DailyAchievementPoints = accountInformation.DailyAchievementPoints; obj.MonthlyAchievementPoints = accountInformation.MonthlyAchievementPoints; }); } } } catch (Exception ex) { LoggingService.AddJobLogEntry(LogEntryLevel.CriticalError, nameof(AccountLoginCheckJob), account.Name, ex.Message, ex.ToString()); } } } }
/// <summary> /// Return the message of element /// </summary> /// <returns>Message</returns> public override DiscordEmbedBuilder GetMessage() { var builder = new DiscordEmbedBuilder(); builder.WithTitle(LocalizationGroup.GetText("ChooseGuildTitle", "Guild selection")); builder.WithDescription(LocalizationGroup.GetText("ChooseGuildDescription", "Please choose one of the following guilds:")); _guilds = new Dictionary <int, string>(); using (var connector = new GuidWars2ApiConnector(DialogContext.GetValue <string>("ApiKey"))) { var fieldText = new StringBuilder(); var i = 1; // TODO GetMessage -> GetMessageAsync var accountInformation = connector.GetAccountInformationAsync().Result; foreach (var guildId in accountInformation.GuildLeader) { var guildInformation = connector.GetGuildInformation(guildId).Result; fieldText.Append('`'); fieldText.Append(i); fieldText.Append("` - "); fieldText.Append(' '); fieldText.Append(guildInformation.Name); fieldText.Append('['); fieldText.Append(guildInformation.Tag); fieldText.Append(']'); fieldText.Append('\n'); _guilds[i] = guildId; i++; } builder.AddField(LocalizationGroup.GetText("GuildsField", "Guilds"), fieldText.ToString()); } return(builder); }
/// <summary> /// Returns the reactions which should be added to the message /// </summary> /// <returns>Reactions</returns> public override IReadOnlyList <ReactionData <bool> > GetReactions() { return(_reactions ??= new List <ReactionData <bool> > { new () { Emoji = DiscordEmojiService.GetEditEmoji(CommandContext.Client), CommandText = LocalizationGroup.GetFormattedText("EditApiKeyCommand", "{0} Edit api key", DiscordEmojiService.GetEditEmoji(CommandContext.Client)), Func = async() => { var success = false; var apiKey = await RunSubElement <AccountApiKeyDialogElement, string>().ConfigureAwait(false); apiKey = apiKey?.Trim(); if (string.IsNullOrWhiteSpace(apiKey) == false) { try { var connector = new GuidWars2ApiConnector(apiKey); await using (connector.ConfigureAwait(false)) { var tokenInformation = await connector.GetTokenInformationAsync() .ConfigureAwait(false); if (tokenInformation?.Permissions != null && tokenInformation.Permissions.Contains(TokenInformation.Permission.Account) && tokenInformation.Permissions.Contains(TokenInformation.Permission.Characters) && tokenInformation.Permissions.Contains(TokenInformation.Permission.Progression)) { var accountInformation = await connector.GetAccountInformationAsync() .ConfigureAwait(false); if (accountInformation.Name == DialogContext.GetValue <string>("AccountName")) { using (var dbFactory = RepositoryFactory.CreateInstance()) { var user = await CommandContext.GetCurrentUser() .ConfigureAwait(false); if (dbFactory.GetRepository <AccountRepository>() .Refresh(obj => obj.UserId == user.Id && obj.Name == accountInformation.Name, obj => { obj.ApiKey = apiKey; obj.Permissions = GuildWars2ApiPermissionConverter.ToPermission(tokenInformation.Permissions); })) { success = true; } } } else { await CommandContext.Channel .SendMessageAsync(LocalizationGroup.GetText("AccountNameMismatch", "The provided api key doesn't match the current account name.")) .ConfigureAwait(false); } } else { await CommandContext.Channel .SendMessageAsync(LocalizationGroup.GetText("InvalidToken", "The provided token is invalid or doesn't have the required permissions.")) .ConfigureAwait(false); } } } catch (HttpRequestException ex) when(ex.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) { await CommandContext.Channel .SendMessageAsync(LocalizationGroup.GetText("InvalidToken", "The provided token is invalid or doesn't have the required permissions.")) .ConfigureAwait(false); } } return success; } }, new () { Emoji = DiscordEmojiService.GetEdit2Emoji(CommandContext.Client), CommandText = LocalizationGroup.GetFormattedText("EditDpsReportUserTokenCommand", "{0} Edit dps report user token", DiscordEmojiService.GetEdit2Emoji(CommandContext.Client)), Func = async() => { var token = await RunSubElement <AccountDpsReportUserTokenDialogElement, string>() .ConfigureAwait(false); using (var dbFactory = RepositoryFactory.CreateInstance()) { var accountName = DialogContext.GetValue <string>("AccountName"); var user = await CommandContext.GetCurrentUser() .ConfigureAwait(false); dbFactory.GetRepository <AccountRepository>() .Refresh(obj => obj.UserId == user.Id && obj.Name == accountName, obj => obj.DpsReportUserToken = token); } return true; } }, new () { Emoji = DiscordEmojiService.GetCrossEmoji(CommandContext.Client), CommandText = LocalizationGroup.GetFormattedText("CancelCommand", "{0} Cancel", DiscordEmojiService.GetCrossEmoji(CommandContext.Client)), Func = () => Task.FromResult(false) } });
/// <summary> /// Adding a new account /// </summary> /// <param name="commandContextContainer">Command context</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> public async Task Add(CommandContextContainer commandContextContainer) { if (commandContextContainer.Message.Channel.IsPrivate == false) { await commandContextContainer.Message .RespondAsync(LocalizationGroup.GetText("SwitchToPrivate", "I answered your command as a private message.")) .ConfigureAwait(false); } await commandContextContainer.SwitchToDirectMessageContext() .ConfigureAwait(false); var apiKey = await DialogHandler.Run <AccountApiKeyDialogElement, string>(commandContextContainer) .ConfigureAwait(false); apiKey = apiKey?.Trim(); if (string.IsNullOrWhiteSpace(apiKey) == false) { try { var connector = new GuidWars2ApiConnector(apiKey); await using (connector.ConfigureAwait(false)) { var tokenInformation = await connector.GetTokenInformationAsync() .ConfigureAwait(false); if (tokenInformation?.Permissions != null && tokenInformation.Permissions.Contains(TokenInformation.Permission.Account) && tokenInformation.Permissions.Contains(TokenInformation.Permission.Characters) && tokenInformation.Permissions.Contains(TokenInformation.Permission.Progression)) { var accountInformation = await connector.GetAccountInformationAsync() .ConfigureAwait(false); using (var dbFactory = RepositoryFactory.CreateInstance()) { var userId = dbFactory.GetRepository <DiscordAccountRepository>() .GetQuery() .Where(obj => obj.Id == commandContextContainer.User.Id) .Select(obj => obj.UserId) .First(); if (dbFactory.GetRepository <AccountRepository>() .AddOrRefresh(obj => obj.UserId == userId && obj.Name == accountInformation.Name, obj => { obj.UserId = userId; obj.Name = accountInformation.Name; obj.ApiKey = apiKey; obj.Permissions = GuildWars2ApiPermissionConverter.ToPermission(tokenInformation.Permissions); })) { await commandContextContainer.Channel .SendMessageAsync(LocalizationGroup.GetText("ApiKeyAdded", "Your API-Key has been added successfully.")) .ConfigureAwait(false); } else { throw dbFactory.LastError; } } } else { await commandContextContainer.Channel .SendMessageAsync(LocalizationGroup.GetText("InvalidToken", "The provided token is invalid or doesn't have the required permissions.")) .ConfigureAwait(false); } } } catch (HttpRequestException ex) when(ex.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) { await commandContextContainer.Channel .SendMessageAsync(LocalizationGroup.GetText("InvalidToken", "The provided token is invalid or doesn't have the required permissions.")) .ConfigureAwait(false); } } }