Ejemplo n.º 1
0
        public async Task <IActionResult> EditMessage([FromRoute] ulong guildId, [FromRoute] int id, [FromBody] ScheduledMessageForPutDto dto)
        {
            await RequirePermission(guildId, DiscordPermission.Moderator);
            await GetRegisteredGuild(guildId);

            ScheduledMessageRepository repo = ScheduledMessageRepository.CreateDefault(_serviceProvider, await GetIdentity());

            ScheduledMessage message = await repo.GetMessage(id);

            if (message.GuildId != guildId)
            {
                throw new UnauthorizedException();
            }
            if (message.Status != ScheduledMessageStatus.Pending)
            {
                throw new ProtectedScheduledMessageException();
            }
            if (dto.ScheduledFor < DateTime.UtcNow.AddMinutes(1))
            {
                throw new InvalidDateForScheduledMessageException();
            }

            message.Name         = dto.Name;
            message.Content      = dto.Content;
            message.ScheduledFor = dto.ScheduledFor;
            message.ChannelId    = dto.ChannelId;

            message = await repo.UpdateMessage(message);

            return(Ok(new ScheduledMessageView(
                          message,
                          await _discordAPI.FetchUserInfo(message.CreatorId, CacheBehavior.OnlyCache),
                          await _discordAPI.FetchUserInfo(message.LastEditedById, CacheBehavior.OnlyCache),
                          _discordAPI.FetchGuildChannels(guildId, CacheBehavior.OnlyCache).FirstOrDefault(x => x.Id == message.ChannelId)
                          )));
        }