Exemple #1
0
    async def on_member_join(self, member: Member) -> None:
        """Check newly joining users to see if they meet the account age threshold."""
        if self.threshold:
            now = datetime.utcnow()

            if now - member.created_at < relativedelta_to_timedelta(
                    self.threshold):
                log.info(f"Rejecting user {member}: Account is too new")

                message_sent = False

                try:
                    await member.send(
                        REJECTION_MESSAGE.format(user=member.mention))

                    message_sent = True
                except Exception:
                    log.exception(
                        f"Unable to send rejection message to user: {member}")

                await member.kick(reason="DEFCON active, user is too new")
                self.bot.stats.incr("defcon.leaves")

                message = (
                    f"{format_user(member)} was denied entry because their account is too new."
                )

                if not message_sent:
                    message = f"{message}\n\nUnable to send rejection message via DM; they probably have DMs disabled."

                await self.mod_log.send_log_message(
                    Icons.defcon_denied, Colours.soft_red, "Entry denied",
                    message, member.avatar_url_as(static_format="png"))
Exemple #2
0
    async def set_slowmode(self, ctx: Context, channel: Optional[TextChannel],
                           delay: DurationDelta) -> None:
        """Set the slowmode delay for a text channel."""
        # Use the channel this command was invoked in if one was not given
        if channel is None:
            channel = ctx.channel

        # Convert `dateutil.relativedelta.relativedelta` to `datetime.timedelta`
        # Must do this to get the delta in a particular unit of time
        slowmode_delay = time.relativedelta_to_timedelta(delay).total_seconds()

        humanized_delay = time.humanize_delta(delay)

        # Ensure the delay is within discord's limits
        if slowmode_delay <= SLOWMODE_MAX_DELAY:
            log.info(
                f'{ctx.author} set the slowmode delay for #{channel} to {humanized_delay}.'
            )

            await channel.edit(slowmode_delay=slowmode_delay)
            if channel.id in COMMONLY_SLOWMODED_CHANNELS:
                log.info(
                    f'Recording slowmode change in stats for {channel.name}.')
                self.bot.stats.gauge(
                    f"slowmode.{COMMONLY_SLOWMODED_CHANNELS[channel.id]}",
                    slowmode_delay)

            await ctx.send(
                f'{Emojis.check_mark} The slowmode delay for {channel.mention} is now {humanized_delay}.'
            )

        else:
            log.info(
                f'{ctx.author} tried to set the slowmode delay of #{channel} to {humanized_delay}, '
                'which is not between 0 and 6 hours.')

            await ctx.send(
                f'{Emojis.cross_mark} The slowmode delay must be between 0 and 6 hours.'
            )
Exemple #3
0
    async def on_member_join(self, member: Member) -> None:
        """Check newly joining users to see if they meet the account age threshold."""
        if self.threshold:
            now = arrow.utcnow()

            if now - member.created_at < relativedelta_to_timedelta(
                    self.threshold):
                log.info(f"Rejecting user {member}: Account is too new")

                message_sent = False

                try:
                    await member.send(
                        REJECTION_MESSAGE.format(user=member.mention))
                    message_sent = True
                except Forbidden:
                    log.debug(
                        f"Cannot send DEFCON rejection DM to {member}: DMs disabled"
                    )
                except Exception:
                    # Broadly catch exceptions because DM isn't critical, but it's imperative to kick them.
                    log.exception(
                        f"Error sending DEFCON rejection message to {member}")

                await member.kick(reason="DEFCON active, user is too new")
                self.bot.stats.incr("defcon.leaves")

                message = (
                    f"{format_user(member)} was denied entry because their account is too new."
                )

                if not message_sent:
                    message = f"{message}\n\nUnable to send rejection message via DM; they probably have DMs disabled."

                await self.mod_log.send_log_message(Icons.defcon_denied,
                                                    Colours.soft_red,
                                                    "Entry denied", message,
                                                    member.display_avatar.url)
Exemple #4
0
 def _log_threshold_stat(self, threshold: relativedelta) -> None:
     """Adds the threshold to the bot stats in days."""
     threshold_days = relativedelta_to_timedelta(
         threshold).total_seconds() / SECONDS_IN_DAY
     self.bot.stats.gauge("defcon.threshold", threshold_days)