|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+from discord import Guild, Intents, Member, Message, PartialEmoji, RawReactionActionEvent
|
|
|
2
|
+from discord.ext import commands
|
|
|
3
|
+from storage import Storage
|
|
|
4
|
+
|
|
|
5
|
+class JoinRecord:
|
|
|
6
|
+ """
|
|
|
7
|
+ Data object containing details about a guild join event.
|
|
|
8
|
+ """
|
|
|
9
|
+ def __init__(self, member: Member):
|
|
|
10
|
+ self.member = member
|
|
|
11
|
+ self.join_time = member.joined_at or datetime.now()
|
|
|
12
|
+ self.is_kicked = False
|
|
|
13
|
+ self.is_banned = False
|
|
|
14
|
+
|
|
|
15
|
+ def age_seconds(self, now: datetime) -> float:
|
|
|
16
|
+ """
|
|
|
17
|
+ Returns the age of this join in seconds from the given "now" time.
|
|
|
18
|
+ """
|
|
|
19
|
+ a = now - self.join_time
|
|
|
20
|
+ return float(a.total_seconds())
|
|
|
21
|
+
|
|
|
22
|
+class RaidPhase:
|
|
|
23
|
+ """
|
|
|
24
|
+ Enum of phases in a JoinRaid. Phases progress monotonically.
|
|
|
25
|
+ """
|
|
|
26
|
+ NONE = 0
|
|
|
27
|
+ JUST_STARTED = 1
|
|
|
28
|
+ CONTINUING = 2
|
|
|
29
|
+ ENDED = 3
|
|
|
30
|
+
|
|
|
31
|
+class JoinRaid:
|
|
|
32
|
+ """
|
|
|
33
|
+ Tracks recent joins to a guild to detect join raids, where a large number of automated users
|
|
|
34
|
+ all join at the same time.
|
|
|
35
|
+ """
|
|
|
36
|
+ def __init__(self):
|
|
|
37
|
+ self.joins = []
|
|
|
38
|
+ self.phase = RaidPhase.NONE
|
|
|
39
|
+ # datetime when the raid started, or None.
|
|
|
40
|
+ self.raid_start_time = None
|
|
|
41
|
+ # Message posted to Discord to warn of the raid. Convenience property managed
|
|
|
42
|
+ # by caller. Ignored by this class.
|
|
|
43
|
+ self.warning_message = None
|
|
|
44
|
+
|
|
|
45
|
+ def handle_join(self,
|
|
|
46
|
+ member: Member,
|
|
|
47
|
+ now: datetime,
|
|
|
48
|
+ max_age_seconds: float,
|
|
|
49
|
+ max_join_count: int) -> None:
|
|
|
50
|
+ """
|
|
|
51
|
+ Processes a new member join to a guild and detects join raids. Updates
|
|
|
52
|
+ self.phase and self.raid_start_time properties.
|
|
|
53
|
+ """
|
|
|
54
|
+ # Check for existing record for this user
|
|
|
55
|
+ print(f'handle_join({member.name}) start')
|
|
|
56
|
+ join: JoinRecord = None
|
|
|
57
|
+ i: int = 0
|
|
|
58
|
+ while i < len(self.joins):
|
|
|
59
|
+ elem = self.joins[i]
|
|
|
60
|
+ if elem.member.id == member.id:
|
|
|
61
|
+ print(f'Member {member.name} already in join list at index {i}. Removing.')
|
|
|
62
|
+ join = self.joins.pop(i)
|
|
|
63
|
+ join.join_time = now
|
|
|
64
|
+ break
|
|
|
65
|
+ i += 1
|
|
|
66
|
+ # Add new record to end
|
|
|
67
|
+ self.joins.append(join or JoinRecord(member))
|
|
|
68
|
+ # Check raid status and do upkeep
|
|
|
69
|
+ self.__process_joins(now, max_age_seconds, max_join_count)
|
|
|
70
|
+ print(f'handle_join({member.name}) end')
|
|
|
71
|
+
|
|
|
72
|
+ def __process_joins(self,
|
|
|
73
|
+ now: datetime,
|
|
|
74
|
+ max_age_seconds: float,
|
|
|
75
|
+ max_join_count: int) -> None:
|
|
|
76
|
+ """
|
|
|
77
|
+ Processes self.joins after each addition, detects raids, updates self.phase,
|
|
|
78
|
+ and throws out unneeded records.
|
|
|
79
|
+ """
|
|
|
80
|
+ print('__process_joins {')
|
|
|
81
|
+ i: int = 0
|
|
|
82
|
+ recent_count: int = 0
|
|
|
83
|
+ should_cull: bool = self.phase == RaidPhase.NONE
|
|
|
84
|
+ while i < len(self.joins):
|
|
|
85
|
+ join: JoinRecord = self.joins[i]
|
|
|
86
|
+ age: float = join.age_seconds(now)
|
|
|
87
|
+ is_old: bool = age > max_age_seconds
|
|
|
88
|
+ if not is_old:
|
|
|
89
|
+ recent_count += 1
|
|
|
90
|
+ print(f'- {i}. {join.member.name} is {age}s old - recent_count={recent_count}')
|
|
|
91
|
+ if is_old and should_cull:
|
|
|
92
|
+ self.joins.pop(i)
|
|
|
93
|
+ print(f'- {i}. {join.member.name} is {age}s old - too old, removing')
|
|
|
94
|
+ else:
|
|
|
95
|
+ print(f'- {i}. {join.member.name} is {age}s old - moving on to next')
|
|
|
96
|
+ i += 1
|
|
|
97
|
+ is_raid = recent_count > max_join_count
|
|
|
98
|
+ print(f'- is_raid {is_raid}')
|
|
|
99
|
+ if is_raid:
|
|
|
100
|
+ if self.phase == RaidPhase.NONE:
|
|
|
101
|
+ self.phase = RaidPhase.JUST_STARTED
|
|
|
102
|
+ self.raid_start_time = now
|
|
|
103
|
+ print('- Phase moved to JUST_STARTED. Recording raid start time.')
|
|
|
104
|
+ elif self.phase == RaidPhase.JUST_STARTED:
|
|
|
105
|
+ self.phase = RaidPhase.CONTINUING
|
|
|
106
|
+ print('- Phase moved to CONTINUING.')
|
|
|
107
|
+ elif self.phase == self.phase in (RaidPhase.JUST_STARTED, RaidPhase.CONTINUING):
|
|
|
108
|
+ self.phase = RaidPhase.ENDED
|
|
|
109
|
+ print('- Phase moved to ENDED.')
|
|
|
110
|
+
|
|
|
111
|
+ # Undo join add if the raid is over
|
|
|
112
|
+ if self.phase == RaidPhase.ENDED and len(self.joins) > 0:
|
|
|
113
|
+ last = self.joins.pop(-1)
|
|
|
114
|
+ print(f'- Popping last join for {last.member.name}')
|
|
|
115
|
+ print('} __process_joins')
|
|
|
116
|
+
|
|
|
117
|
+ async def kick_all(self,
|
|
|
118
|
+ reason: str = "Part of join raid") -> list[Member]:
|
|
|
119
|
+ """
|
|
|
120
|
+ Kicks all users in this join raid. Skips users who have already been
|
|
|
121
|
+ flagged as having been kicked or banned. Returns a List of Members
|
|
|
122
|
+ who were newly kicked.
|
|
|
123
|
+ """
|
|
|
124
|
+ kicks = []
|
|
|
125
|
+ for join in self.joins:
|
|
|
126
|
+ if join.is_kicked or join.is_banned:
|
|
|
127
|
+ continue
|
|
|
128
|
+ await join.member.kick(reason=reason)
|
|
|
129
|
+ join.is_kicked = True
|
|
|
130
|
+ kicks.append(join.member)
|
|
|
131
|
+ self.phase = RaidPhase.ENDED
|
|
|
132
|
+ return kicks
|
|
|
133
|
+
|
|
|
134
|
+ async def ban_all(self,
|
|
|
135
|
+ reason: str = "Part of join raid",
|
|
|
136
|
+ delete_message_days: int = 0) -> list[Member]:
|
|
|
137
|
+ """
|
|
|
138
|
+ Bans all users in this join raid. Skips users who have already been
|
|
|
139
|
+ flagged as having been banned. Users who were previously kicked can
|
|
|
140
|
+ still be banned. Returns a List of Members who were newly banned.
|
|
|
141
|
+ """
|
|
|
142
|
+ bans = []
|
|
|
143
|
+ for join in self.joins:
|
|
|
144
|
+ if join.is_banned:
|
|
|
145
|
+ continue
|
|
|
146
|
+ await join.member.ban(reason=reason, delete_message_days=delete_message_days)
|
|
|
147
|
+ join.is_banned = True
|
|
|
148
|
+ bans.append(join.member)
|
|
|
149
|
+ self.phase = RaidPhase.ENDED
|
|
|
150
|
+ return bans
|
|
|
151
|
+
|
|
|
152
|
+class JoinRaidCog(commands.Cog):
|
|
|
153
|
+ """
|
|
|
154
|
+ Cog for monitoring member joins and detecting potential bot raids.
|
|
|
155
|
+ """
|
|
|
156
|
+ def __init__(self, bot):
|
|
|
157
|
+ self.bot = bot
|
|
|
158
|
+
|
|
|
159
|
+ @commands.group(
|
|
|
160
|
+ brief='Manages join raid detection and handling',
|
|
|
161
|
+ )
|
|
|
162
|
+ @commands.has_permissions(ban_members=True)
|
|
|
163
|
+ @commands.guild_only()
|
|
|
164
|
+ async def joinraid(self, context: commands.Context):
|
|
|
165
|
+ 'Command group'
|
|
|
166
|
+ if context.invoked_subcommand is None:
|
|
|
167
|
+ await context.send_help()
|
|
|
168
|
+
|
|
|
169
|
+ @joinraid.command(
|
|
|
170
|
+ name='enable',
|
|
|
171
|
+ brief='Enables join raid detection',
|
|
|
172
|
+ description='Join raid detection is off by default.',
|
|
|
173
|
+ )
|
|
|
174
|
+ async def joinraid_enable(self, context: commands.Context):
|
|
|
175
|
+ 'Command handler'
|
|
|
176
|
+ # TODO
|
|
|
177
|
+ pass
|
|
|
178
|
+
|
|
|
179
|
+ @joinraid.command(
|
|
|
180
|
+ name='disable',
|
|
|
181
|
+ brief='Disables join raid detection',
|
|
|
182
|
+ description='Join raid detection is off by default.',
|
|
|
183
|
+ )
|
|
|
184
|
+ async def joinraid_disable(self, context: commands.Context):
|
|
|
185
|
+ 'Command handler'
|
|
|
186
|
+ # TODO
|
|
|
187
|
+ pass
|
|
|
188
|
+
|
|
|
189
|
+ @joinraid.command(
|
|
|
190
|
+ name='setrate',
|
|
|
191
|
+ brief='Sets the rate of joins which triggers a warning to mods',
|
|
|
192
|
+ description='Each time a member joins, the join records from the ' +
|
|
|
193
|
+ 'previous _x_ seconds are counted up, where _x_ is the number of ' +
|
|
|
194
|
+ 'seconds configured by this command. If that count meets or ' +
|
|
|
195
|
+ 'exceeds the maximum join count configured by this command then ' +
|
|
|
196
|
+ 'a raid is detected and a warning is issued to the mods.',
|
|
|
197
|
+ usage='<join_count> <seconds>',
|
|
|
198
|
+ )
|
|
|
199
|
+ async def joinraid_setrate(self, context: commands.Context,
|
|
|
200
|
+ joinCount: int,
|
|
|
201
|
+ seconds: int):
|
|
|
202
|
+ 'Command handler'
|
|
|
203
|
+ # TODO
|
|
|
204
|
+ pass
|
|
|
205
|
+
|
|
|
206
|
+ @joinraid.command(
|
|
|
207
|
+ name='getrate',
|
|
|
208
|
+ brief='Shows the rate of joins which triggers a warning to mods',
|
|
|
209
|
+ )
|
|
|
210
|
+ async def joinraid_getrate(self, context: commands.Context):
|
|
|
211
|
+ 'Command handler'
|
|
|
212
|
+ # TODO
|
|
|
213
|
+ pass
|
|
|
214
|
+
|
|
|
215
|
+ @commands.Cog.listener()
|
|
|
216
|
+ async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
|
|
|
217
|
+ 'Event handler'
|
|
|
218
|
+ # TODO
|
|
|
219
|
+ pass
|
|
|
220
|
+
|
|
|
221
|
+ @commands.Cog.listener()
|
|
|
222
|
+ async def on_member_join(self, member: Member) -> None:
|
|
|
223
|
+ 'Event handler'
|
|
|
224
|
+ # TODO
|
|
|
225
|
+ pass
|