Esempio n. 1
0
    /// <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)
            }
        });
Esempio n. 2
0
    /// <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);
            }
        }
    }