Exemplo n.º 1
0
    public async Task <Result> MoveRoleplayIntoChannelAsync(string newName, params IUser[] participants)
    {
        var createRoleplayAsync = await _discordRoleplays.CreateRoleplayAsync
                                  (
            _context.GuildID.Value,
            _context.User.ID,
            newName,
            "No summary set.",
            false,
            true
                                  );

        if (!createRoleplayAsync.IsSuccess)
        {
            return(Result.FromError(createRoleplayAsync));
        }

        var roleplay = createRoleplayAsync.Entity;

        foreach (var participant in participants)
        {
            if (participant.ID == _context.User.ID)
            {
                // Already added
                continue;
            }

            var addParticipantAsync = await _discordRoleplays.AddUserToRoleplayAsync(roleplay, participant.ID);

            if (addParticipantAsync.IsSuccess)
            {
                continue;
            }

            var message =
                $"I couldn't add <@{participant.ID}> to the roleplay ({addParticipantAsync.Error.Message}. " +
                "Please try to invite them manually.";

            var sendWarning = await _feedback.SendContextualWarningAsync
                              (
                message,
                _context.User.ID
                              );

            if (!sendWarning.IsSuccess)
            {
                return(Result.FromError(sendWarning));
            }
        }

        // Copy the last messages from the participants
        var before = _context switch
        {
            MessageContext messageContext => messageContext.MessageID,
            InteractionContext interactionContext => interactionContext.ID,
            _ => throw new ArgumentOutOfRangeException(nameof(_context))
        };

        var getMessageBatch = await _channelAPI.GetChannelMessagesAsync(_context.ChannelID, before : before);

        if (!getMessageBatch.IsSuccess)
        {
            return(Result.FromError(getMessageBatch));
        }

        var messageBatch = getMessageBatch.Entity;

        var participantMessages = participants
                                  .Select(participant => messageBatch.FirstOrDefault(m => m.Author.ID == participant.ID))
                                  .Where(message => message is not null)
                                  .Select(m => m !)
                                  .ToList();

        var getDedicatedChannel = DedicatedChannelService.GetDedicatedChannel(roleplay);

        if (!getDedicatedChannel.IsSuccess)
        {
            return(Result.FromError(getDedicatedChannel));
        }

        var dedicatedChannel = getDedicatedChannel.Entity;

        foreach (var participantMessage in participantMessages.OrderByDescending(m => m.Timestamp))
        {
            var messageLink = "https://discord.com/channels/" +
                              $"{_context.GuildID.Value}/{_context.ChannelID}/{participantMessage.ID}";

            var send = await _channelAPI.CreateMessageAsync(dedicatedChannel, messageLink);

            if (!send.IsSuccess)
            {
                return(Result.FromError(send));
            }
        }

        var start = await StartRoleplayAsync(roleplay);

        return(start.IsSuccess
            ? Result.FromSuccess()
            : Result.FromError(start));
    }
}