Exemplo 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)
            }
        });
Exemplo n.º 2
0
    /// <summary>
    /// Starting the role editing assistant
    /// </summary>
    /// <param name="commandContextContainer">Current command context</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    private async Task RunEditAssistantAsync(CommandContextContainer commandContextContainer)
    {
        var roleId = await SelectRoleAsync(commandContextContainer, null).ConfigureAwait(false);

        if (roleId != null)
        {
            var builder = new DiscordEmbedBuilder();
            builder.WithTitle(LocalizationGroup.GetText("RoleEditTitle", "Raid role configuration"));

            var areRolesAvailable = false;

            using (var dbFactory = RepositoryFactory.CreateInstance())
            {
                var rolesFieldText = new StringBuilder();
                var roles          = dbFactory.GetRepository <RaidRoleRepository>()
                                     .GetQuery()
                                     .Where(obj => obj.Id == roleId.Value &&
                                            obj.IsDeleted == false)
                                     .Select(obj => new
                {
                    obj.DiscordEmojiId,
                    obj.Description,
                    SubRoles = obj.SubRaidRoles
                               .Where(obj2 => obj2.IsDeleted == false)
                               .Select(obj2 => new
                    {
                        obj2.DiscordEmojiId,
                        obj2.Description
                    })
                })
                                     .OrderBy(obj => obj.Description)
                                     .First();

                builder.WithDescription($"{DiscordEmoji.FromGuildEmote(commandContextContainer.Client, roles.DiscordEmojiId)} - {roles.Description}");

                if (roles.SubRoles.Any())
                {
                    areRolesAvailable = true;

                    foreach (var role in roles.SubRoles)
                    {
                        rolesFieldText.Append(DiscordEmoji.FromGuildEmote(commandContextContainer.Client, role.DiscordEmojiId));
                        rolesFieldText.Append(" - ");
                        rolesFieldText.Append(role.Description);
                        rolesFieldText.Append('\n');
                    }

                    builder.AddField(LocalizationGroup.GetText("AssistantRolesField", "Roles"), rolesFieldText.ToString());
                }
            }

            var addSubRoleEmoji    = DiscordEmojiService.GetAddEmoji(commandContextContainer.Client);
            var descriptionEmoji   = DiscordEmojiService.GetEditEmoji(commandContextContainer.Client);
            var editSubRoleEmoji   = DiscordEmojiService.GetEdit2Emoji(commandContextContainer.Client);
            var emojiEmoji         = DiscordEmojiService.GetEmojiEmoji(commandContextContainer.Client);
            var deleteSubRoleEmoji = DiscordEmojiService.GetTrashCanEmoji(commandContextContainer.Client);
            var cancelEmoji        = DiscordEmojiService.GetCrossEmoji(commandContextContainer.Client);

            var commands = new StringBuilder();
            commands.AppendLine(LocalizationGroup.GetFormattedText("RoleEditEditDescriptionCommand", "{0} Edit description", descriptionEmoji));
            commands.AppendLine(LocalizationGroup.GetFormattedText("RoleEditEditEmojiCommand", "{0} Edit emoji", emojiEmoji));
            commands.AppendLine(LocalizationGroup.GetFormattedText("RoleEditAddSubRoleCommand", "{0} Add sub role", addSubRoleEmoji));

            if (areRolesAvailable)
            {
                commands.AppendLine(LocalizationGroup.GetFormattedText("RoleEditEditSubRoleCommand", "{0} Edit sub role", editSubRoleEmoji));
                commands.AppendLine(LocalizationGroup.GetFormattedText("RoleEditDeleteSubRoleCommand", "{0} Delete sub role", deleteSubRoleEmoji));
            }

            commands.AppendLine(LocalizationGroup.GetFormattedText("AssistantCancelCommand", "{0} Cancel", cancelEmoji));

            builder.AddField(LocalizationGroup.GetText("AssistantCommandsField", "Commands"), commands.ToString());

            var message = await commandContextContainer.Channel
                          .SendMessageAsync(builder)
                          .ConfigureAwait(false);

            var userReactionTask = commandContextContainer.Client
                                   .GetInteractivity()
                                   .WaitForReactionAsync(message, commandContextContainer.User);

            await message.CreateReactionAsync(descriptionEmoji).ConfigureAwait(false);

            await message.CreateReactionAsync(emojiEmoji).ConfigureAwait(false);

            await message.CreateReactionAsync(addSubRoleEmoji).ConfigureAwait(false);

            if (areRolesAvailable)
            {
                await message.CreateReactionAsync(editSubRoleEmoji).ConfigureAwait(false);

                await message.CreateReactionAsync(deleteSubRoleEmoji).ConfigureAwait(false);
            }

            await message.CreateReactionAsync(cancelEmoji).ConfigureAwait(false);

            var userReaction = await userReactionTask.ConfigureAwait(false);

            if (userReaction.TimedOut == false)
            {
                if (userReaction.Result.Emoji.Id == addSubRoleEmoji.Id)
                {
                    await RunAddSubRoleAssistantAsync(commandContextContainer, roleId.Value).ConfigureAwait(false);
                }
                else if (userReaction.Result.Emoji.Id == descriptionEmoji.Id)
                {
                    await RunEditDescriptionAssistantAsync(commandContextContainer, roleId.Value).ConfigureAwait(false);
                }
                else if (userReaction.Result.Emoji.Id == editSubRoleEmoji.Id)
                {
                    await RunEditSubRoleAssistantAsync(commandContextContainer, roleId.Value).ConfigureAwait(false);
                }
                else if (userReaction.Result.Emoji.Id == emojiEmoji.Id)
                {
                    await RunEditEmojiAssistantAsync(commandContextContainer, roleId.Value).ConfigureAwait(false);
                }
                else if (userReaction.Result.Emoji.Id == deleteSubRoleEmoji.Id)
                {
                    await RunDeleteSubRoleAssistantAsync(commandContextContainer, roleId.Value).ConfigureAwait(false);
                }
            }
        }
    }