Пример #1
0
    /// <inheritdoc />
    public async Task <Result> RespondAsync(IMessageCreate gatewayEvent, CancellationToken ct = default)
    {
        var isBot    = gatewayEvent.Author.IsBot.HasValue && gatewayEvent.Author.IsBot.Value;
        var isSystem = gatewayEvent.Author.IsSystem.HasValue && gatewayEvent.Author.IsSystem.Value;

        if (isBot || isSystem)
        {
            return(Result.FromSuccess());
        }

        if (!gatewayEvent.GuildID.IsDefined(out var guildID))
        {
            return(Result.FromSuccess());
        }

        return(await _roleplays.ConsumeMessageAsync(gatewayEvent, guildID));
    }
        public async Task <RuntimeResult> IncludePreviousMessagesAsync
        (
            [RequireEntityOwnerOrPermission(typeof(EditRoleplay), PermissionTarget.Other)]
            Roleplay roleplay,
            [OverrideTypeReader(typeof(UncachedMessageTypeReader <IMessage>))]
            IMessage startMessage,
            [OverrideTypeReader(typeof(UncachedMessageTypeReader <IMessage>))]
            IMessage?finalMessage = null
        )
        {
            finalMessage ??= this.Context.Message;

            if (startMessage.Channel != finalMessage.Channel)
            {
                return(RuntimeCommandResult.FromError("The messages are not in the same channel."));
            }

            var addedOrUpdatedMessageCount = 0;

            var latestMessage = startMessage;

            while (latestMessage.Timestamp < finalMessage.Timestamp)
            {
                var messages = (await this.Context.Channel.GetMessagesAsync
                                (
                                    latestMessage, Direction.After
                                ).FlattenAsync()).OrderBy(m => m.Timestamp).ToList();

                latestMessage = messages.Last();

                foreach (var message in messages)
                {
                    // Jump out if we've passed the final message
                    if (message.Timestamp > finalMessage.Timestamp)
                    {
                        break;
                    }

                    if (!(message is IUserMessage userMessage))
                    {
                        continue;
                    }

                    var modifyResult = await _discordRoleplays.ConsumeMessageAsync(userMessage);

                    if (modifyResult.IsSuccess)
                    {
                        ++addedOrUpdatedMessageCount;
                    }
                }
            }

            return(RuntimeCommandResult.FromSuccess
                   (
                       $"{addedOrUpdatedMessageCount} messages added to \"{roleplay.Name}\"."
                   ));
        }
        private async Task <OperationResult> ArchiveRoleplayAsync
        (
            SocketGuild guild,
            ServerService serverService,
            RoleplayDiscordService roleplayService,
            DedicatedChannelService dedicatedChannels,
            RoleplayServerSettingsService serverSettings,
            Roleplay roleplay
        )
        {
            if (roleplay.DedicatedChannelID is null)
            {
                return(OperationResult.FromError("The roleplay doesn't have a dedicated channel."));
            }

            if (roleplay.IsPublic)
            {
                var postResult = await PostArchivedRoleplayAsync(guild, serverService, serverSettings, roleplay);

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

            var dedicatedChannel = guild.GetTextChannel((ulong)roleplay.DedicatedChannelID);

            if (dedicatedChannel is null)
            {
                // Something's gone wrong in the database. Who the f**k knows why. We'll do an extra delete to be
                // on the safe side.
                await dedicatedChannels.DeleteChannelAsync(guild, roleplay);

                return(OperationResult.FromSuccess());
            }

            // Ensure the messages are all caught up
            foreach (var message in await dedicatedChannel.GetMessagesAsync().FlattenAsync())
            {
                if (!(message is IUserMessage userMessage))
                {
                    continue;
                }

                // We don't care about the results here.
                await roleplayService.ConsumeMessageAsync(userMessage);
            }

            await dedicatedChannels.DeleteChannelAsync(guild, roleplay);

            return(OperationResult.FromSuccess());
        }