Beispiel #1
0
        /// <inheritdoc />
        protected override async Task AfterCommandAsync(SocketCommandContext context, int commandStart, IResult result)
        {
            switch (result.Error)
            {
            case CommandError.UnknownCommand:
            {
                break;
            }

            case CommandError.ObjectNotFound:
            case CommandError.MultipleMatches:
            case CommandError.Unsuccessful:
            case CommandError.UnmetPrecondition:
            case CommandError.ParseFailed:
            case CommandError.BadArgCount:
            {
                var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

                try
                {
                    var errorEmbed = _feedback.CreateFeedbackEmbed
                                     (
                        context.User,
                        Color.Red,
                        result.ErrorReason
                                     );

                    await userDMChannel.SendMessageAsync(string.Empty, false, errorEmbed);

                    var searchResult = this.Commands.Search(context, commandStart);
                    if (searchResult.Commands.Any())
                    {
                        await userDMChannel.SendMessageAsync
                        (
                            string.Empty,
                            false,
                            _help.CreateCommandUsageEmbed(searchResult.Commands)
                        );
                    }
                }
                catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
                {
                }
                finally
                {
                    await userDMChannel.CloseAsync();
                }

                break;
            }
            }
        }
        public async Task <RuntimeResult> ContactUserAsync(IUser discordUser)
        {
            if (this.Context.User is IGuildUser guildUser && !guildUser.GuildPermissions.MentionEveryone)
            {
                return(RuntimeCommandResult.FromError("You need to be able to mention everyone to do that."));
            }

            if (discordUser.Id == this.Context.Client.CurrentUser.Id)
            {
                return(RuntimeCommandResult.FromError
                       (
                           "That's a splendid idea - at least then, I'd get an intelligent reply."
                       ));
            }

            if (discordUser.IsBot)
            {
                return(RuntimeCommandResult.FromError("I could do that, but I doubt I'd get a reply."));
            }

            var contactMessage = $"Hello there, {discordUser.Mention}. I've been instructed to initiate... " +
                                 $"negotiations... with you. \nA good place to start would be the \"!help <topic>\" " +
                                 $"command.";

            var eb = _feedback.CreateFeedbackEmbed
                     (
                discordUser,
                Color.DarkPurple,
                contactMessage
                     );

            var userDMChannel = await discordUser.GetOrCreateDMChannelAsync();

            try
            {
                await userDMChannel.SendMessageAsync(string.Empty, false, eb);
            }
            catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
            {
                return(RuntimeCommandResult.FromError(hex.Message));
            }
            finally
            {
                await userDMChannel.CloseAsync();
            }

            return(RuntimeCommandResult.FromSuccess("User contacted."));
        }
Beispiel #3
0
        public async Task ContactUserAsync([NotNull] IUser discordUser)
        {
            if (discordUser.Id == this.Context.Client.CurrentUser.Id)
            {
                await _feedback.SendErrorAsync(this.Context, "That's a splendid idea - at least then, I'd get an intelligent reply.");

                return;
            }

            if (discordUser.IsBot)
            {
                await _feedback.SendErrorAsync(this.Context, "I could do that, but I doubt I'd get a reply.");

                return;
            }

            var eb = _feedback.CreateFeedbackEmbed
                     (
                discordUser,
                Color.DarkPurple,
                $"Hello there, {discordUser.Mention}. I've been instructed to initiate... negotiations... with you. \nA good place to start would be the \"!help <topic>\" command."
                     );

            var userDMChannel = await discordUser.GetOrCreateDMChannelAsync();

            try
            {
                await userDMChannel.SendMessageAsync(string.Empty, false, eb);
            }
            catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
            {
                return;
            }
            finally
            {
                await userDMChannel.CloseAsync();
            }

            await _feedback.SendConfirmationAsync(this.Context, "User contacted.");
        }
Beispiel #4
0
        /// <summary>
        /// Sends a paginated message to the context user's direct messaging channel, alerting them if they are
        /// not already in it, and deletes it after a certain timeout. Defaults to 5 minutes.
        /// </summary>
        /// <param name="this">The interactive service.</param>
        /// <param name="context">The command context.</param>
        /// <param name="feedback">The feedback service to use.</param>
        /// <param name="message">The message to send.</param>
        /// <param name="timeout">The timeout after which the embed will be deleted. Defaults to 5 minutes.</param>
        /// <returns>The message that was sent.</returns>
        public static async Task SendPrivateInteractiveMessageAndDeleteAsync
        (
            [NotNull] this InteractivityService @this,
            [NotNull] ICommandContext context,
            [NotNull] UserFeedbackService feedback,
            [NotNull] InteractiveMessage message,
            [CanBeNull] TimeSpan?timeout = null
        )
        {
            timeout = timeout ?? TimeSpan.FromMinutes(5.0);

            var userChannel = await context.User.GetOrCreateDMChannelAsync();

            try
            {
                var eb = feedback.CreateFeedbackEmbed(context.User, Color.DarkPurple, "Loading...");
                await feedback.SendEmbedAndDeleteAsync(userChannel, eb, TimeSpan.FromSeconds(1));

                if (!(context.Channel is IDMChannel))
                {
                    await feedback.SendConfirmationAsync(context, "Please check your private messages.");
                }

                await @this.SendInteractiveMessageAndDeleteAsync(userChannel, message, timeout);
            }
            catch (HttpException hex)
            {
                if (hex.WasCausedByDMsNotAccepted())
                {
                    await feedback.SendWarningAsync
                    (
                        context,
                        "You don't accept DMs from non-friends on this server, so I'm unable to do that."
                    );
                }

                throw;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Sends a private message to the user in the given context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="contents">The contents of the message to send.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task SendPrivateMessageAsync(SocketCommandContext context, string contents)
        {
            var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

            try
            {
                var errorEmbed = _feedback.CreateFeedbackEmbed
                                 (
                    context.User,
                    Color.Red,
                    contents
                                 );

                await userDMChannel.SendMessageAsync(string.Empty, false, errorEmbed);
            }
            catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
            {
            }
            finally
            {
                await userDMChannel.CloseAsync();
            }
        }
        /// <inheritdoc />
        protected override async Task UserJoined(SocketGuildUser user)
        {
            var getServerResult = await _servers.GetOrRegisterServerAsync(user.Guild);

            if (!getServerResult.IsSuccess)
            {
                return;
            }

            var server = getServerResult.Entity;

            if (!server.SendJoinMessage)
            {
                return;
            }

            var getJoinMessageResult = _servers.GetJoinMessage(server);

            if (!getJoinMessageResult.IsSuccess)
            {
                return;
            }

            var userChannel = await user.GetOrCreateDMChannelAsync();

            try
            {
                var eb = _feedback.CreateEmbedBase();
                eb.WithDescription($"Welcome, {user.Mention}!");
                eb.WithDescription(getJoinMessageResult.Entity);

                await _feedback.SendEmbedAsync(userChannel, eb.Build());
            }
            catch (HttpException hex)
            {
                if (!hex.WasCausedByDMsNotAccepted())
                {
                    throw;
                }

                var content = $"Welcome, {user.Mention}! You have DMs disabled, so I couldn't send you the " +
                              "first-join message. To see it, type \"!server join-message\".";

                var welcomeMessage = _feedback.CreateFeedbackEmbed
                                     (
                    user,
                    Color.Orange,
                    content
                                     );

                try
                {
                    await _feedback.SendEmbedAsync(user.Guild.DefaultChannel, welcomeMessage);
                }
                catch (HttpException pex)
                {
                    if (!pex.WasCausedByMissingPermission())
                    {
                        throw;
                    }
                }
            }
        }
        public async Task <RuntimeResult> ReplayRoleplayAsync
        (
            [RequireEntityOwnerOrPermission(typeof(ExportRoleplay), PermissionTarget.Other)]
            Roleplay roleplay,
            DateTimeOffset from = default,
            DateTimeOffset to   = default
        )
        {
            if (from == default)
            {
                from = DateTimeOffset.MinValue;
            }

            if (to == default)
            {
                to = DateTimeOffset.Now;
            }

            var userDMChannel = await this.Context.Message.Author.GetOrCreateDMChannelAsync();

            var eb = await CreateRoleplayInfoEmbedAsync(roleplay);

            try
            {
                await userDMChannel.SendMessageAsync(string.Empty, false, eb);

                var messages = roleplay.Messages.Where
                               (
                    m =>
                    m.Timestamp > from && m.Timestamp < to
                               )
                               .OrderBy(msg => msg.Timestamp).ToList();

                var timestampEmbed = _feedback.CreateFeedbackEmbed
                                     (
                    this.Context.User,
                    Color.DarkPurple,
                    $"Roleplay began at {messages.First().Timestamp.ToUniversalTime()}"
                                     );

                await userDMChannel.SendMessageAsync(string.Empty, false, timestampEmbed);

                if (messages.Count <= 0)
                {
                    await userDMChannel.SendMessageAsync("No messages found in the specified timeframe.");

                    return(RuntimeCommandResult.FromSuccess());
                }

                await _feedback.SendConfirmationAsync
                (
                    this.Context,
                    $"Replaying \"{roleplay.Name}\". Please check your private messages."
                );

                const int messageCharacterLimit = 2000;
                var       sb = new StringBuilder(messageCharacterLimit);

                foreach (var message in messages)
                {
                    var newContent = $"**{message.AuthorNickname}** {message.Contents}\n";

                    if (sb.Length + newContent.Length >= messageCharacterLimit)
                    {
                        await userDMChannel.SendMessageAsync(sb.ToString());

                        await Task.Delay(TimeSpan.FromSeconds(2));

                        sb.Clear();
                        sb.AppendLine();
                    }

                    sb.Append(newContent);

                    if (message.ID == messages.Last().ID)
                    {
                        await userDMChannel.SendMessageAsync(sb.ToString());
                    }
                }
            }
            catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
            {
                await _feedback.SendWarningAsync
                (
                    this.Context,
                    "I can't do that, since you don't accept DMs from non-friends on this server."
                );
            }
            finally
            {
                await userDMChannel.CloseAsync();
            }

            return(RuntimeCommandResult.FromSuccess());
        }
        /// <summary>
        /// Handles the result of a command, alerting the user if errors occurred.
        /// </summary>
        /// <param name="context">The context of the command.</param>
        /// <param name="result">The result of the command.</param>
        /// <param name="argumentPos">The position in the message string where the command starts.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        private async Task HandleCommandResultAsync
        (
            [NotNull] ICommandContext context,
            [NotNull] IResult result,
            int argumentPos
        )
        {
            if (result.IsSuccess)
            {
                return;
            }

            switch (result.Error)
            {
            case CommandError.Exception:
            {
                await HandleInternalErrorAsync(context, result);

                break;
            }

            case CommandError.UnknownCommand:
            {
                break;
            }

            case CommandError.ObjectNotFound:
            case CommandError.MultipleMatches:
            case CommandError.Unsuccessful:
            case CommandError.UnmetPrecondition:
            case CommandError.ParseFailed:
            case CommandError.BadArgCount:
            {
                var userDMChannel = await context.Message.Author.GetOrCreateDMChannelAsync();

                try
                {
                    var errorEmbed = _feedback.CreateFeedbackEmbed
                                     (
                        context.User,
                        Color.Red,
                        result.ErrorReason
                                     );

                    await userDMChannel.SendMessageAsync(string.Empty, false, errorEmbed);

                    var searchResult = _commands.Search(context, argumentPos);
                    if (searchResult.Commands.Any())
                    {
                        await userDMChannel.SendMessageAsync
                        (
                            string.Empty,
                            false,
                            _help.CreateCommandUsageEmbed(searchResult.Commands)
                        );
                    }
                }
                catch (HttpException hex) when(hex.WasCausedByDMsNotAccepted())
                {
                }
                finally
                {
                    await userDMChannel.CloseAsync();
                }

                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }
Beispiel #9
0
        /// <inheritdoc />
        protected override async Task <OperationResult> UserJoinedAsync(SocketGuildUser user)
        {
            using var scope = this.Services.CreateScope();
            var serverService = scope.ServiceProvider.GetRequiredService <ServerService>();

            var getServerResult = await serverService.GetOrRegisterServerAsync(user.Guild);

            if (!getServerResult.IsSuccess)
            {
                return(OperationResult.FromError(getServerResult));
            }

            var server = getServerResult.Entity;

            if (!server.SendJoinMessage)
            {
                return(OperationResult.FromSuccess());
            }

            var getJoinMessageResult = serverService.GetJoinMessage(server);

            if (!getJoinMessageResult.IsSuccess)
            {
                return(OperationResult.FromError(getJoinMessageResult));
            }

            var userChannel = await user.GetOrCreateDMChannelAsync();

            try
            {
                var eb = _feedback.CreateEmbedBase();
                eb.WithDescription($"Welcome, {user.Mention}!");
                eb.WithDescription(getJoinMessageResult.Entity);

                await _feedback.SendEmbedAsync(userChannel, eb.Build());
            }
            catch (HttpException hex)
            {
                if (!hex.WasCausedByDMsNotAccepted())
                {
                    throw;
                }

                var content = $"Welcome, {user.Mention}! You have DMs disabled, so I couldn't send you the " +
                              "first-join message. To see it, type \"!server join-message\".";

                var welcomeMessage = _feedback.CreateFeedbackEmbed
                                     (
                    user,
                    Color.Orange,
                    content
                                     );

                try
                {
                    await _feedback.SendEmbedAsync(user.Guild.SystemChannel, welcomeMessage);
                }
                catch (HttpException pex)
                {
                    if (!pex.WasCausedByMissingPermission())
                    {
                        throw;
                    }
                }
            }

            return(OperationResult.FromSuccess());
        }