Пример #1
0
        public async Task Update(CommandContext ctx, [RemainingText, Description("An invite link or an invite token")] string invite)
        {
            var(_, _, invites) = await ctx.Client.GetInvitesAsync(invite, tryMessageAsACode : true).ConfigureAwait(false);

            if (invites.Count == 0)
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, "Need to specify an invite link or token").ConfigureAwait(false);

                return;
            }

            var errors = 0;

            foreach (var i in invites)
            {
                if (!await InviteWhitelistProvider.IsWhitelistedAsync(i).ConfigureAwait(false))
                {
                    errors++;
                }
            }

            if (errors == 0)
            {
                await ctx.ReactWithAsync(Config.Reactions.Success, "Invite whitelist was successfully updated!").ConfigureAwait(false);
            }
            else
            {
                await ctx.ReactWithAsync(Config.Reactions.Failure, $"Failed to update {errors} invite{StringUtils.GetSuffix(errors)}").ConfigureAwait(false);
            }
            await List(ctx).ConfigureAwait(false);
        }
        public static async Task <bool> CheckMessageForInvitesAsync(DiscordClient client, DiscordMessage message)
        {
            if (message.Channel.IsPrivate)
            {
                return(true);
            }

            if (message.Author.IsBotSafeCheck())
            {
                return(true);
            }

#if !DEBUG
            if (message.Author.IsWhitelisted(client, message.Channel.Guild))
            {
                return(true);
            }
#endif

            if (message.Reactions.Any(r => r.Emoji == Config.Reactions.Moderated && r.IsMe))
            {
                return(true);
            }

            var(hasInvalidResults, attemptedWorkaround, invites) = await client.GetInvitesAsync(message.Content, message.Author).ConfigureAwait(false);

            if (!hasInvalidResults && invites.Count == 0)
            {
                return(true);
            }

            if (hasInvalidResults)
            {
                try
                {
                    DeletedMessagesMonitor.RemovedByBotCache.Set(message.Id, true, DeletedMessagesMonitor.CacheRetainTime);
                    await message.DeleteAsync("Not a white-listed discord invite link").ConfigureAwait(false);

                    await client.ReportAsync("🛃 An unapproved discord invite", message, "In invalid or expired invite", null, ReportSeverity.Low).ConfigureAwait(false);

                    await message.Channel.SendMessageAsync($"{message.Author.Mention} please refrain from posting invites that were not approved by a moderator, especially expired or invalid.").ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Warn(e);
                    await client.ReportAsync("🛃 An unapproved discord invite", message, "In invalid or expired invite", null, ReportSeverity.Medium).ConfigureAwait(false);

                    await message.ReactWithAsync(Config.Reactions.Moderated,
                                                 $"{message.Author.Mention} please remove this expired or invalid invite, and refrain from posting it again until you have received an approval from a moderator.",
                                                 true
                                                 ).ConfigureAwait(false);
                }
                return(false);
            }

            foreach (var invite in invites)
            {
                if (!await InviteWhitelistProvider.IsWhitelistedAsync(invite).ConfigureAwait(false))
                {
                    if (!InviteCodeCache.TryGetValue(message.Author.Id, out HashSet <string> recentInvites))
                    {
                        recentInvites = new HashSet <string>();
                    }
                    var circumventionAttempt = !recentInvites.Add(invite.Code) && attemptedWorkaround; //do not flip, must add to cache always
                    InviteCodeCache.Set(message.Author.Id, recentInvites, CacheDuration);
                    var removed = false;
                    try
                    {
                        DeletedMessagesMonitor.RemovedByBotCache.Set(message.Id, true, DeletedMessagesMonitor.CacheRetainTime);
                        await message.DeleteAsync("Not a white-listed discord invite").ConfigureAwait(false);

                        removed = true;
                    }
                    catch (Exception e)
                    {
                        Config.Log.Warn(e);
                    }

                    var    codeResolveMsg = $"Invite {invite.Code} was resolved to the {invite.Guild?.Name} server";
                    var    reportMsg      = codeResolveMsg;
                    string userMsg;
                    if (circumventionAttempt)
                    {
                        reportMsg += "\nAlso tried to workaround filter despite being asked not to do so.";
                        userMsg    = $"{message.Author.Mention} you have been asked nicely to not post invites to this unapproved discord server before.";
                    }
                    else
                    {
                        userMsg = $"{message.Author.Mention} invites to other servers must be whitelisted first.\n";
                        if (removed)
                        {
                            userMsg += "Please refrain from posting it again until you have received an approval from a moderator.";
                        }
                        else
                        {
                            userMsg += "Please remove it and refrain from posting it again until you have received an approval from a moderator.";
                        }
                    }
                    await client.ReportAsync("🛃 An unapproved discord invite", message, reportMsg, null, ReportSeverity.Low).ConfigureAwait(false);

                    await message.Channel.SendMessageAsync(userMsg).ConfigureAwait(false);

                    if (circumventionAttempt)
                    {
                        await Warnings.AddAsync(client, message, message.Author.Id, message.Author.Username, client.CurrentUser, "Attempted to circumvent discord invite filter", codeResolveMsg);
                    }
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        private static async Task CheckMessageForInvitesAsync(DiscordClient client, DiscordMessage message)
        {
            if (DefaultHandlerFilter.IsFluff(message))
            {
                return;
            }

            if (message.Author.IsWhitelisted(client, message.Channel.Guild))
            {
                return;
            }

            if (message.Reactions.Any(r => r.Emoji == Config.Reactions.Moderated && r.IsMe))
            {
                return;
            }

            var(hasInvalidResults, invites) = await client.GetInvitesAsync(message.Content).ConfigureAwait(false);

            if (!hasInvalidResults && invites.Count == 0)
            {
                return;
            }

            if (hasInvalidResults)
            {
                try
                {
                    await message.DeleteAsync("Not a white-listed discord invite link").ConfigureAwait(false);

                    await client.ReportAsync("An unapproved discord invite", message, "In invalid or expired invite", null, ReportSeverity.Low).ConfigureAwait(false);

                    await message.Channel.SendMessageAsync($"{message.Author.Mention} please refrain from posting invites that were not approved by a moderator, especially expired or invalid.").ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Config.Log.Warn(e);
                    await client.ReportAsync("An unapproved discord invite", message, "In invalid or expired invite", null, ReportSeverity.Medium).ConfigureAwait(false);

                    await message.ReactWithAsync(
                        client,
                        Config.Reactions.Moderated,
                        $"{message.Author.Mention} please remove this expired or invalid invite, and refrain from posting it again until you have recieved an approval from a moderator.",
                        true
                        ).ConfigureAwait(false);
                }
                return;
            }

            foreach (var invite in invites)
            {
                if (!await InviteWhitelistProvider.IsWhitelistedAsync(invite).ConfigureAwait(false))
                {
                    try
                    {
                        await message.DeleteAsync("Not a white-listed discord invite link").ConfigureAwait(false);

                        await client.ReportAsync("An unapproved discord invite", message, $"Invite {invite.Code} was resolved to the {invite.Guild.Name} server", null, ReportSeverity.Low).ConfigureAwait(false);

                        await message.Channel.SendMessageAsync($"{message.Author.Mention} invites to the {invite.Guild.Name.Sanitize()} server are not whitelisted.\n" +
                                                               $"Please refrain from posting it again until you have recieved an approval from a moderator.").ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        Config.Log.Warn(e);
                        await client.ReportAsync("An unapproved discord invite", message, $"Invite {invite.Code} was resolved to the {invite.Guild.Name} server", null, ReportSeverity.Medium).ConfigureAwait(false);

                        await message.ReactWithAsync(
                            client,
                            Config.Reactions.Moderated,
                            $"{message.Author.Mention} invites to the {invite.Guild.Name.Sanitize()} server are not whitelisted.\n" +
                            $"Please remove it and refrain from posting it again until you have recieved an approval from a moderator.",
                            true
                            ).ConfigureAwait(false);
                    }
                    return;
                }
            }
        }