lunarflu HF staff commited on
Commit
3af4c4f
1 Parent(s): b049cc9

[experimental] antispam on_message

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -54,6 +54,7 @@ ping alerts for list of keywords
54
  '''
55
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
56
  number_of_messages = 0
 
57
 
58
  @bot.event
59
  async def on_message(message):
@@ -64,6 +65,58 @@ async def on_message(message):
64
  number_of_messages = number_of_messages + 1
65
  message_link = f"[#{message.channel.name}]({message.jump_url})"
66
  dm_message = await user.send(f"{number_of_messages}| {message_link} |{message.author}: {message.content}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  # Allow other event handlers to process the message
69
  await bot.process_commands(message)
 
54
  '''
55
  # stats stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------
56
  number_of_messages = 0
57
+ user_cooldowns = {}
58
 
59
  @bot.event
60
  async def on_message(message):
 
65
  number_of_messages = number_of_messages + 1
66
  message_link = f"[#{message.channel.name}]({message.jump_url})"
67
  dm_message = await user.send(f"{number_of_messages}| {message_link} |{message.author}: {message.content}")
68
+
69
+
70
+
71
+
72
+
73
+ ### antispam experiment
74
+ cooldown_duration = 1 # 1 message per second
75
+ if message.author.id not in user_cooldowns:
76
+ user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
77
+ else:
78
+ if (message.created_at - user_cooldowns[message.author.id]['timestamp']).total_seconds() > cooldown_duration:
79
+ var1 = message.created_at
80
+ var2 = user_cooldowns[message.author.id]['timestamp']
81
+ print(f"seconds since last message by {message.author}: ({var1} - {var2}).seconds = {(var1 - var2).total_seconds()}")
82
+
83
+ # if we wait longer than cooldown_duration, count will reset
84
+ user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
85
+ else:
86
+ user_cooldowns[message.author.id]['count'] += 1
87
+
88
+ # tldr; if we post 2 messages with less than 1s between them
89
+ if user_cooldowns[message.author.id]['count'] > 1:
90
+ var1 = message.created_at
91
+ var2 = user_cooldowns[message.author.id]['timestamp']
92
+ print(f"seconds since last message by {message.author}: {(var1 - var2).total_seconds()}")
93
+ spam_count = user_cooldowns[message.author.id]['count']
94
+ print(f"count: {user_cooldowns[message.author.id]['count']}")
95
+
96
+ test_server = os.environ.get('TEST_SERVER')
97
+ if test_server == 'True':
98
+ alert = "<@&1106995261487710411>" # test @alerts role
99
+ if test_server == 'False':
100
+ alert = "<@&1108342563628404747>" # normal @alerts role
101
+
102
+ await bot.log_channel.send(
103
+ f"[EXPERIMENTAL ALERT] {message.author.mention} is posting too quickly! \n"
104
+ f"Spam count: {user_cooldowns[message.author.id]['count']}\n"
105
+ f"Message content: {message.content}\n"
106
+ f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})\n"
107
+ f"{alert}"
108
+ )
109
+ """
110
+ if user_cooldowns[message.author.id]['count']/5 > cooldown_duration:
111
+ # ping admins
112
+ # timeout for user
113
+ # kick user
114
+ """
115
+ user_cooldowns[message.author.id]['timestamp'] = message.created_at
116
+
117
+
118
+
119
+
120
 
121
  # Allow other event handlers to process the message
122
  await bot.process_commands(message)