Facebook
From Whipped Tamarin, 3 Years ago, written in Python.
Embed
Download Paste or View Raw
Hits: 77
  1. import config, discord, time
  2. client = discord.Client()
  3.  
  4. message_cache = {}
  5. previous_delays = {}
  6. last_updated = 0
  7.  
  8.  
  9. def get_delay(message_count):
  10.     # get message limits in descending order, compare with message count
  11.     message_limits = sorted(config.time_configs.keys(), reverse=True)
  12.  
  13.     for limit in message_limits:
  14.         if message_count >= limit:
  15.             # return delay determined in config
  16.             return config.time_configs[limit]
  17.    
  18.     # if nothing already returned, return a slowmode delay of 0
  19.     return 0
  20.  
  21.  
  22. async def update_slowmode():
  23.     global last_updated, message_cache, previous_delays
  24.     new_channel_delays = {}
  25.  
  26.     # iterate through cache and fetch new delay times
  27.     for channel_id in message_cache.keys():
  28.         delay = get_delay(message_cache[channel_id])
  29.  
  30.         # if delay is the same as previous delay, skip iteration
  31.         if channel_id in previous_delays.keys():
  32.             if previous_delays[channel_id] == delay:
  33.                 new_channel_delays[channel_id] = delay
  34.                 continue
  35.        
  36.         # edit channel slowmode and update new_channel_delays
  37.         channel = client.get_channel(channel_id)
  38.         await channel.edit(slowmode_delay=delay)
  39.         new_channel_delays[channel_id] = delay
  40.  
  41.    
  42.     # reset message cache and update last_updated & previous_delays
  43.     message_cache = {}
  44.     last_updated = time.time()
  45.     previous_delays = new_channel_delays
  46.  
  47.  
  48. @client.event
  49. async def on_message(message):
  50.     global last_updated, message_cache
  51.     channel_id = message.channel.id
  52.  
  53.     # update slowmode if it has been x seconds since last update
  54.     if time.time() >= last_updated + config.check_frequency:
  55.         await update_slowmode()
  56.    
  57.     # ignore message if channel blacklisted or not whitelisted
  58.     if config.channel_whitelisting_enabled:
  59.         if channel_id not in config.whitelisted_channels:
  60.             return
  61.     else:
  62.         if channel_id in config.blacklisted_channels:
  63.             return
  64.  
  65.     # add channel id to cache if not already added
  66.     if message.channel.id not in message_cache.keys():
  67.         message_cache[channel_id] = 1
  68.         return
  69.    
  70.     # increase message count by 1 for channel if cache exists
  71.     message_cache[channel_id] += 1
  72.  
  73.  
  74. if not config.i_have_read_config:
  75.     # user has not read config file, don't start code
  76.     print("You must modify config.py before running!")
  77.     input("Press enter to continue...")
  78.     exit()
  79.  
  80. client.run(config.bot_token)