From d5cd996864fe213d1c804911c2a17a6d04b8e170 Mon Sep 17 00:00:00 2001 From: Joseph Banks Date: Sun, 12 Apr 2020 01:00:27 +0100 Subject: Add a timeout to prevent the bot from being overloaded with presence updates --- bot/bot.py | 2 +- bot/cogs/stats.py | 12 ++++++++++-- bot/constants.py | 8 +++++++- config-default.yml | 4 +++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index ef4a325dc..6dd5ba896 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -33,7 +33,7 @@ class Bot(commands.Bot): self._resolver = None self._guild_available = asyncio.Event() - statsd_url = constants.Bot.statsd_host + statsd_url = constants.Stats.statsd_host if DEBUG_MODE: # Since statsd is UDP, there are no errors for sending to a down port. diff --git a/bot/cogs/stats.py b/bot/cogs/stats.py index c15d0eb1b..df4827ba1 100644 --- a/bot/cogs/stats.py +++ b/bot/cogs/stats.py @@ -1,9 +1,10 @@ import string +from datetime import datetime from discord import Member, Message, Status from discord.ext.commands import Bot, Cog, Context -from bot.constants import Channels, Guild +from bot.constants import Channels, Guild, Stats as StatConf CHANNEL_NAME_OVERRIDES = { @@ -13,7 +14,7 @@ CHANNEL_NAME_OVERRIDES = { Channels.staff_lounge: "staff_lounge" } -ALLOWED_CHARS = string.ascii_letters + string.digits +ALLOWED_CHARS = string.ascii_letters + string.digits + "-" class Stats(Cog): @@ -21,6 +22,7 @@ class Stats(Cog): def __init__(self, bot: Bot): self.bot = bot + self.last_presence_update = None @Cog.listener() async def on_message(self, message: Message) -> None: @@ -73,6 +75,12 @@ class Stats(Cog): if after.guild.id != Guild.id: return + if self.last_presence_update: + if (datetime.now() - self.last_presence_update).seconds < StatConf.presence_update_timeout: + return + + self.last_presence_update = datetime.now() + online = 0 idle = 0 dnd = 0 diff --git a/bot/constants.py b/bot/constants.py index 33c1d530d..2add028e7 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -199,7 +199,6 @@ class Bot(metaclass=YAMLGetter): prefix: str token: str sentry_dsn: str - statsd_host: str class Filter(metaclass=YAMLGetter): section = "filter" @@ -351,6 +350,13 @@ class CleanMessages(metaclass=YAMLGetter): message_limit: int +class Stats(metaclass=YAMLGetter): + section = "bot" + subsection = "stats" + + presence_update_timeout: int + statsd_host: str + class Categories(metaclass=YAMLGetter): section = "guild" diff --git a/config-default.yml b/config-default.yml index 567caacbf..4cd61ce10 100644 --- a/config-default.yml +++ b/config-default.yml @@ -3,7 +3,9 @@ bot: token: !ENV "BOT_TOKEN" sentry_dsn: !ENV "BOT_SENTRY_DSN" - statsd_host: "graphite" + stats: + statsd_host: "graphite" + presence_update_timeout: 300 cooldowns: # Per channel, per tag. -- cgit v1.2.3