Ejemplo n.º 1
0
        private async Task TryPurgeInviteLinkAsync(IMessage message)
        {
            if
            (
                !(message.Author is IGuildUser author) || (author.Guild is null) ||
                !(message.Channel is IGuildChannel channel) || (channel.Guild is null)
            )
            {
                Log.Debug("Message {MessageId} was not in an IGuildChannel & IMessageChannel, or Author {Author} was not an IGuildUser",
                          message.Id, message.Author.Id);
                return;
            }

            var selfUser = await SelfUserProvider.GetSelfUserAsync();

            if (author.Id == selfUser.Id)
            {
                return;
            }

            var matches = _inviteLinkMatcher.Matches(message.Content);

            if (!matches.Any())
            {
                return;
            }

            if (await DesignatedChannelService.ChannelHasDesignationAsync(channel.Guild, channel, DesignatedChannelType.Unmoderated))
            {
                return;
            }

            if (await AuthorizationService.HasClaimsAsync(author, AuthorizationClaim.PostInviteLink))
            {
                Log.Debug("Message {MessageId} was skipped because the author {Author} has the PostInviteLink claim",
                          message.Id, message.Author.Id);
                return;
            }

            var invites = new List <IInvite>(matches.Count);

            foreach (var code in matches.Select(x => x.Groups["Code"].Value))
            {
                var invite = await DiscordClient.GetInviteAsync(code);

                invites.Add(invite);
            }

            // Allow invites to the guild in which the message was posted
            if (invites.All(x => x?.GuildId == author.GuildId))
            {
                Log.Debug("Message {MessageId} was skipped because the invite was to this server", message.Id);
                return;
            }

            Log.Debug("Message {MessageId} is going to be deleted", message.Id);

            await ModerationService.DeleteMessageAsync(message, "Unauthorized Invite Link", selfUser.Id);

            Log.Debug("Message {MessageId} was deleted because it contains an invite link", message.Id);

            await message.Channel.SendMessageAsync($"Sorry {author.Mention} your invite link has been removed - please don't post links to other guilds");
        }