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

            ScheduledMessage message = new();

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

            if (message.ScheduledFor < DateTime.UtcNow.AddMinutes(1))
            {
                throw new InvalidDateForScheduledMessageException();
            }

            message = await ScheduledMessageRepository.CreateDefault(_serviceProvider, await GetIdentity()).CreateMessage(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)
                          )));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DeleteMessage([FromRoute] ulong guildId, [FromRoute] int id)
        {
            await RequirePermission(guildId, DiscordPermission.Moderator);
            await GetRegisteredGuild(guildId);

            Identity identity = await GetIdentity();

            ScheduledMessageRepository repo = ScheduledMessageRepository.CreateDefault(_serviceProvider, identity);

            ScheduledMessage message = await repo.GetMessage(id);

            if (message.GuildId != guildId)
            {
                throw new UnauthorizedException();
            }

            // handled messages should only be deletable by an admin to prevent abuse
            if (message.Status != ScheduledMessageStatus.Pending && !(await identity.HasAdminRoleOnGuild(guildId)))
            {
                throw new ProtectedScheduledMessageException();
            }

            await repo.DeleteMessage(message.Id);

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetMessages([FromRoute] ulong guildId, [FromQuery][Range(0, int.MaxValue)] int page = 0)
        {
            await RequirePermission(guildId, DiscordPermission.Moderator);

            List <ScheduledMessage> messages = await ScheduledMessageRepository.CreateDefault(_serviceProvider, await GetIdentity()).GetAllMessages(guildId, page);

            List <ScheduledMessageView> results = new();

            foreach (ScheduledMessage message in messages)
            {
                results.Add(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)));
            }

            return(Ok(results));
        }