Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task DeleteMessageAsync(IMessage message, string reason)
        {
            AuthorizationService.RequireAuthenticatedUser();
            AuthorizationService.RequireClaims(AuthorizationClaim.ModerationDeleteMessage);

            if (!(message.Channel is IGuildChannel guildChannel))
            {
                throw new InvalidOperationException($"Cannot delete message {message.Id} because it is not a guild message");
            }

            await UserService.TrackUserAsync(message.Author as IGuildUser);

            await ChannelService.TrackChannelAsync(guildChannel);

            using (var transaction = await DeletedMessageRepository.BeginCreateTransactionAsync())
            {
                await DeletedMessageRepository.CreateAsync(new DeletedMessageCreationData()
                {
                    GuildId     = guildChannel.GuildId,
                    ChannelId   = guildChannel.Id,
                    MessageId   = message.Id,
                    AuthorId    = message.Author.Id,
                    Content     = message.Content,
                    Reason      = reason,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                });

                await message.DeleteAsync();

                transaction.Commit();
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task <ServiceResult> DeleteMessageAsync(IMessage message, string reason)
        {
            var authResult = AuthorizationService.CheckClaims(AuthorizationClaim.ModerationDeleteInfraction);

            if (authResult.IsFailure)
            {
                return(authResult);
            }

            var rankResult = await RequireSubjectRankLowerThanModeratorRankAsync(AuthorizationService.CurrentGuildId.Value, message.Author.Id);

            if (rankResult.IsFailure)
            {
                return(rankResult);
            }

            if (!(message.Channel is IGuildChannel guildChannel))
            {
                return(ServiceResult.FromError($"Cannot delete message {message.Id} because it is not a guild message"));
            }

            await UserService.TrackUserAsync(message.Author as IGuildUser);

            await ChannelService.TrackChannelAsync(guildChannel);

            using (var transaction = await DeletedMessageRepository.BeginCreateTransactionAsync())
            {
                await DeletedMessageRepository.CreateAsync(new DeletedMessageCreationData()
                {
                    GuildId     = guildChannel.GuildId,
                    ChannelId   = guildChannel.Id,
                    MessageId   = message.Id,
                    AuthorId    = message.Author.Id,
                    Content     = message.Content,
                    Reason      = reason,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                });

                await message.DeleteAsync();

                transaction.Commit();
            }

            return(ServiceResult.FromSuccess());
        }