lunarflu HF staff commited on
Commit
e6fb5ac
1 Parent(s): 815ddae

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (1) hide show
  1. app.py +43 -8
app.py CHANGED
@@ -64,7 +64,27 @@ async def on_message(message):
64
  print(f"Antispam->Detecting certain unwanted strings Error: {e}")
65
 
66
  #Posting too fast
67
- cooldown_duration = 3 # messages per n seconds, was 1, now 3, could try 5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  if message.author.id not in user_cooldowns:
69
  user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
70
  else:
@@ -77,14 +97,18 @@ async def on_message(message):
77
  user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
78
  else:
79
  user_cooldowns[message.author.id]['count'] += 1
80
-
81
- # tldr; if we post 2 messages with less than 1s between them
82
- if user_cooldowns[message.author.id]['count'] > 3: # 4 in a row, helps avoid false positives for posting in threads
 
 
 
 
 
83
  var1 = message.created_at
84
  var2 = user_cooldowns[message.author.id]['timestamp']
85
  print(f"seconds since last message by {message.author}: {(var1 - var2).total_seconds()}")
86
- spam_count = user_cooldowns[message.author.id]['count']
87
- print(f"count: {user_cooldowns[message.author.id]['count']}")
88
 
89
  test_server = os.environ.get('TEST_SERVER')
90
  if test_server == 'True':
@@ -94,17 +118,28 @@ async def on_message(message):
94
 
95
  await bot.log_channel.send(
96
  f"[EXPERIMENTAL ALERT] {message.author} may be posting too quickly! \n"
97
- f"Spam count: {user_cooldowns[message.author.id]['count']}\n"
98
  f"Message content: {message.content}\n"
99
  f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})\n"
100
  f"{alert}"
101
  )
102
  await cakiki.send(
103
  f"[EXPERIMENTAL ALERT] {message.author} may be 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
  )
 
 
 
 
 
 
 
 
 
 
 
108
  """
109
  if user_cooldowns[message.author.id]['count']/5 > cooldown_duration:
110
  # ping admins
 
64
  print(f"Antispam->Detecting certain unwanted strings Error: {e}")
65
 
66
  #Posting too fast
67
+ """
68
+ cooldown_duration determines the time window within which the bot tracks a user's posting behavior.
69
+ This is useful for detecting "staggered" instances of spam, where 20-50 messages are sent 2-10s apart,
70
+ over timespans of typically a few minutes.
71
+
72
+ If a user hasn't posted anything for a duration longer than cooldown_duration, their record is cleared,
73
+ and they start fresh if they post again.
74
+
75
+ If a user posts within the cooldown_duration, their activity count is updated,
76
+ and their record persists until it exceeds the specified threshold or until the cooldown_duration window resets.
77
+
78
+ Increasing cooldown_duration = More robust at detecting "staggered" / "delayed" spam, but more false positives (fast chatters)
79
+
80
+ """
81
+
82
+ # cooldown_duration = 3; false_positive_threshld = 3; -> 99% spam at spam_count of 10+ (could still be wrong, so we timeout)
83
+ cooldown_duration = 5 # messages per n seconds, was 1, now 3, could try 5
84
+ false_positive_threshold = 5 # big = alert less (catch less spam), small = alert more (catch more spam)
85
+ timeout_threshold = 10 # number of messages before issuing a timeout (similar function to ban, easier to reverse)
86
+ timeout_duration = 604800 # timeout duration in seconds (1 week)
87
+
88
  if message.author.id not in user_cooldowns:
89
  user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
90
  else:
 
97
  user_cooldowns[message.author.id] = {'count': 1, 'timestamp': message.created_at}
98
  else:
99
  user_cooldowns[message.author.id]['count'] += 1
100
+ spam_count = user_cooldowns[message.author.id]['count']
101
+
102
+ # tldr; if we post 2 messages with less than [cooldown_duration]seconds between them
103
+ if spam_count >= false_positive_threshold: # n in a row, helps avoid false positives for posting in threads
104
+ # warning for 5+
105
+ channel = message.channel
106
+ await channel.send(f"{message.author.mention}, you may be posting too quickly! Please slow down a bit 🤗")
107
+
108
  var1 = message.created_at
109
  var2 = user_cooldowns[message.author.id]['timestamp']
110
  print(f"seconds since last message by {message.author}: {(var1 - var2).total_seconds()}")
111
+ print(f"spam_count: {spam_count}")
 
112
 
113
  test_server = os.environ.get('TEST_SERVER')
114
  if test_server == 'True':
 
118
 
119
  await bot.log_channel.send(
120
  f"[EXPERIMENTAL ALERT] {message.author} may be posting too quickly! \n"
121
+ f"Spam count: {spam_count}\n"
122
  f"Message content: {message.content}\n"
123
  f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})\n"
124
  f"{alert}"
125
  )
126
  await cakiki.send(
127
  f"[EXPERIMENTAL ALERT] {message.author} may be posting too quickly! \n"
128
+ f"Spam count: {spam_count}\n"
129
  f"Message content: {message.content}\n"
130
  f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})\n"
131
  )
132
+
133
+ # timeout for a week
134
+ if spam_count >= timeout_threshold:
135
+ user = message.author
136
+ await user.send("You have been temporarily timed out (7 Days) for spamming. If this was an error, message <@811235357663297546> or an `@admin`")
137
+ await user.timeout(datetime.timedelta(seconds=timeout_duration), reason="Spamming")
138
+ # reset spam count and timestamp
139
+ user_cooldowns[message.author.id] = {'count': 0, 'timestamp': message.created_at}
140
+ print(f"{bot.user} timed out {message.author} for {timeout_duration} for Spam")
141
+ await bot.log_channel.send(f" {alert} {bot.user} timed out {message.author} for {timeout_duration} for Spam")
142
+
143
  """
144
  if user_cooldowns[message.author.id]['count']/5 > cooldown_duration:
145
  # ping admins