Add basic rate-limiting to combat spam

feature/embed-uploads
Tristan Gosselin-Hane 7 years ago
parent 452e9c89bb
commit 21e2420eaf
  1. 1
      config.example.json
  2. 1
      config.py
  3. 19
      webhook-bridge.py

@ -7,6 +7,7 @@
"MC_ONLINE": true,
"DISCORD_APP_TOKEN": "",
"LOG_LEVEL": "INFO",
"MESSAGE_DELAY": 1.5,
"ADMINS": [
283983554051047425
]

@ -13,6 +13,7 @@ class Configuration(object):
self.mc_online = self._config["MAIN"]["MC_ONLINE"]
self.discord_token = self._config["MAIN"]["DISCORD_APP_TOKEN"]
self.logging_level = self._config["MAIN"]["LOG_LEVEL"]
self.message_delay = self._config["MAIN"]["MESSAGE_DELAY"]
self.admin_users = self._config["MAIN"]["ADMINS"]
self.auth_ip = self._config["AUTH_SERVER"]["BIND_IP"]
self.auth_port = self._config["AUTH_SERVER"]["PORT"]

@ -15,6 +15,8 @@ from config import Configuration
from database import DiscordChannel, AccountLinkToken, DiscordAccount
import database_session
from datetime import datetime, timedelta, timezone
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
@ -30,6 +32,8 @@ from bidict import bidict
UUID_CACHE = bidict()
WEBHOOKS = []
BOT_USERNAME = ""
NEXT_MESSAGE_TIME = datetime.now(timezone.utc)
PREVIOUS_MESSAGE = ""
def mc_uuid_to_username(uuid):
@ -509,6 +513,21 @@ def main():
channels = session.query(DiscordChannel).all()
session.close()
del session
global PREVIOUS_MESSAGE, NEXT_MESSAGE_TIME
if message_to_send == PREVIOUS_MESSAGE or \
datetime.now(timezone.utc) < NEXT_MESSAGE_TIME:
send_channel = message.channel
if isinstance(message.channel, discord.abc.GuildChannel):
dm_channel = message.author.dm_channel
if not dm_channel:
await message.author.create_dm()
send_channel = message.author.dm_channel
msg = "Your message \"{}\" has been rate-limited.".format(message.clean_content)
await send_channel.send(msg)
return
PREVIOUS_MESSAGE = message_to_send
NEXT_MESSAGE_TIME = datetime.now(timezone.utc) + timedelta(seconds=config.message_delay)
for channel in channels:
webhooks = await discord_bot.get_channel(channel.channel_id).webhooks()

Loading…
Cancel
Save