|
|
@@ -5,623 +5,23 @@ the sqlite database rocketbot.db (copy rocketbot.db.sample for a blank database)
|
|
5
|
5
|
Author: Ian Albert (@rocketsoup)
|
|
6
|
6
|
Date: 2021-11-11
|
|
7
|
7
|
"""
|
|
8
|
|
-from datetime import datetime
|
|
9
|
|
-import sqlite3
|
|
10
|
|
-import sys
|
|
11
|
|
-
|
|
12
|
|
-from discord import Guild, Intents, Member, Message, PartialEmoji, RawReactionActionEvent
|
|
13
|
|
-from discord.abc import GuildChannel
|
|
|
8
|
+from discord import Intents
|
|
14
|
9
|
from discord.ext import commands
|
|
15
|
|
-from discord.ext.commands.context import Context
|
|
16
|
10
|
|
|
17
|
11
|
from config import CONFIG
|
|
18
|
|
-from cogs.config import ConfigCog
|
|
19
|
|
-from cogs.general import GeneralCog
|
|
|
12
|
+from cogs.configcog import ConfigCog
|
|
|
13
|
+from cogs.generalcog import GeneralCog
|
|
|
14
|
+from cogs.joinraidcog import JoinRaidCog
|
|
20
|
15
|
|
|
21
|
16
|
class Rocketbot(commands.Bot):
|
|
22
|
17
|
def __init__(self, command_prefix, **kwargs):
|
|
23
|
18
|
super().__init__(command_prefix, **kwargs)
|
|
24
|
19
|
|
|
25
|
|
-bot = Rocketbot(CONFIG['commandPrefix'])
|
|
|
20
|
+intents = Intents.default()
|
|
|
21
|
+intents.members = True # To get join/leave events
|
|
|
22
|
+bot = Rocketbot(command_prefix=CONFIG['commandPrefix'], intents=intents)
|
|
26
|
23
|
bot.add_cog(GeneralCog(bot))
|
|
27
|
24
|
bot.add_cog(ConfigCog(bot))
|
|
|
25
|
+bot.add_cog(JoinRaidCog(bot))
|
|
28
|
26
|
bot.run(CONFIG['clientToken'], bot=True, reconnect=True)
|
|
29
|
27
|
print('\nBot aborted')
|
|
30
|
|
-
|
|
31
|
|
-# -- Classes ----------------------------------------------------------------
|
|
32
|
|
-
|
|
33
|
|
-# class GuildContext:
|
|
34
|
|
-# """
|
|
35
|
|
-# Logic and state for a single guild serviced by the bot.
|
|
36
|
|
-# """
|
|
37
|
|
-# def __init__(self, guild_id: int):
|
|
38
|
|
-# self.guild_id = guild_id
|
|
39
|
|
-# self.guild = None # Resolved later
|
|
40
|
|
-# # Config populated during load
|
|
41
|
|
-# self.warning_channel_id = None
|
|
42
|
|
-# self.warning_channel = None
|
|
43
|
|
-# self.warning_mention = None
|
|
44
|
|
-# self.join_warning_count = CONFIG['joinWarningCount']
|
|
45
|
|
-# self.join_warning_seconds = CONFIG['joinWarningSeconds']
|
|
46
|
|
-# # Non-persisted runtime state
|
|
47
|
|
-# self.current_raid = JoinRaid()
|
|
48
|
|
-# self.all_raids = [ self.current_raid ] # periodically culled of old ones
|
|
49
|
|
-
|
|
50
|
|
-# # Commands
|
|
51
|
|
-
|
|
52
|
|
-# async def command_hello(self, message: Message) -> None:
|
|
53
|
|
-# """
|
|
54
|
|
-# Command handler
|
|
55
|
|
-# """
|
|
56
|
|
-# await message.channel.send(f'Hey there, {message.author.mention}!')
|
|
57
|
|
-
|
|
58
|
|
-# async def command_testwarn(self, context: Context) -> None:
|
|
59
|
|
-# """
|
|
60
|
|
-# Command handler
|
|
61
|
|
-# """
|
|
62
|
|
-# if self.warning_channel is None:
|
|
63
|
|
-# self.__trace('No warning channel set!')
|
|
64
|
|
-# await context.message.channel.send('No warning channel set on this guild! Type ' +
|
|
65
|
|
-# f'`{bot.command_prefix}{setwarningchannel.__name__}` in the channel you ' +
|
|
66
|
|
-# 'want warnings to be posted.')
|
|
67
|
|
-# return
|
|
68
|
|
-# await self.__warn('Test warning. This is only a test.')
|
|
69
|
|
-
|
|
70
|
|
-# async def command_setwarningchannel(self, context: Context):
|
|
71
|
|
-# """
|
|
72
|
|
-# Command handler
|
|
73
|
|
-# """
|
|
74
|
|
-# self.__trace(f'Warning channel set to {context.channel.name}')
|
|
75
|
|
-# self.warning_channel = context.channel
|
|
76
|
|
-# self.warning_channel_id = context.channel.id
|
|
77
|
|
-# save_guild_context(self)
|
|
78
|
|
-# await self.__warn('Warning messages will now be sent to ' + self.warning_channel.mention)
|
|
79
|
|
-
|
|
80
|
|
-# async def command_setwarningmention(self, _context: Context, mention: str):
|
|
81
|
|
-# """
|
|
82
|
|
-# Command handler
|
|
83
|
|
-# """
|
|
84
|
|
-# self.__trace('set warning mention')
|
|
85
|
|
-# m = mention if mention is not None and len(mention) > 0 else None
|
|
86
|
|
-# self.warning_mention = m
|
|
87
|
|
-# save_guild_context(self)
|
|
88
|
|
-# if m is None:
|
|
89
|
|
-# await self.__warn('Warning messages will not mention anyone')
|
|
90
|
|
-# else:
|
|
91
|
|
-# await self.__warn('Warning messages will now mention ' + m)
|
|
92
|
|
-
|
|
93
|
|
-# async def command_setraidwarningrate(self, _context: Context, count: int, seconds: int):
|
|
94
|
|
-# """
|
|
95
|
|
-# Command handler
|
|
96
|
|
-# """
|
|
97
|
|
-# self.join_warning_count = count
|
|
98
|
|
-# self.join_warning_seconds = seconds
|
|
99
|
|
-# save_guild_context(self)
|
|
100
|
|
-# await self.__warn(f'Maximum join rate set to {count} joins per {seconds} seconds')
|
|
101
|
|
-
|
|
102
|
|
-# # Events
|
|
103
|
|
-
|
|
104
|
|
-# async def handle_join(self, member: Member) -> None:
|
|
105
|
|
-# """
|
|
106
|
|
-# Event handler for all joins to this guild.
|
|
107
|
|
-# """
|
|
108
|
|
-# print(f'{member.guild.name}: {member.name} joined')
|
|
109
|
|
-# now = member.joined_at
|
|
110
|
|
-# raid = self.current_raid
|
|
111
|
|
-# raid.handle_join(
|
|
112
|
|
-# member,
|
|
113
|
|
-# now=now,
|
|
114
|
|
-# max_age_seconds = self.join_warning_seconds,
|
|
115
|
|
-# max_join_count = self.join_warning_count)
|
|
116
|
|
-# self.__trace(f'raid phase: {raid.phase}')
|
|
117
|
|
-# if raid.phase == RaidPhase.JUST_STARTED:
|
|
118
|
|
-# await self.__on_join_raid_begin(raid)
|
|
119
|
|
-# elif raid.phase == RaidPhase.CONTINUING:
|
|
120
|
|
-# await self.__on_join_raid_updated(raid)
|
|
121
|
|
-# elif raid.phase == RaidPhase.ENDED:
|
|
122
|
|
-# self.__start_new_raid(member)
|
|
123
|
|
-# await self.__on_join_raid_end(raid)
|
|
124
|
|
-# self.__cull_old_raids(now)
|
|
125
|
|
-
|
|
126
|
|
-# def __start_new_raid(self, member: Member = None):
|
|
127
|
|
-# """
|
|
128
|
|
-# Retires self.current_raid and creates a new empty one. If `member` is passed, it will be
|
|
129
|
|
-# added to the new self.current_raid after it is created.
|
|
130
|
|
-# """
|
|
131
|
|
-# self.current_raid = JoinRaid()
|
|
132
|
|
-# self.all_raids.append(self.current_raid)
|
|
133
|
|
-# if member is not None:
|
|
134
|
|
-# self.current_raid.handle_join(
|
|
135
|
|
-# member,
|
|
136
|
|
-# member.joined_at,
|
|
137
|
|
-# max_age_seconds = self.join_warning_seconds,
|
|
138
|
|
-# max_join_count = self.join_warning_count)
|
|
139
|
|
-
|
|
140
|
|
-# async def handle_reaction_add(self, message, member, emoji):
|
|
141
|
|
-# """
|
|
142
|
|
-# Handles all message reaction events to see if they need to be acted on.
|
|
143
|
|
-# """
|
|
144
|
|
-# if member.id == bot.user.id:
|
|
145
|
|
-# # It's-a me, Rocketbot!
|
|
146
|
|
-# return
|
|
147
|
|
-# if message.author.id != bot.user.id:
|
|
148
|
|
-# # The message the user is reacting to wasn't authored by me. Ignore.
|
|
149
|
|
-# return
|
|
150
|
|
-# self.__trace(f'User {member} added emoji {emoji}')
|
|
151
|
|
-# if not member.permissions_in(message.channel).ban_members:
|
|
152
|
|
-# self.__trace('Reactor does not have ban permissions. Ignoring.')
|
|
153
|
|
-# return
|
|
154
|
|
-# if emoji.name == CONFIG['kickEmoji']:
|
|
155
|
|
-# await self.__kick_all_in_raid_message(message)
|
|
156
|
|
-# elif emoji.name == CONFIG['banEmoji']:
|
|
157
|
|
-# await self.__ban_all_in_raid_message(message)
|
|
158
|
|
-# else:
|
|
159
|
|
-# print('Unhandled emoji. Ignoring.')
|
|
160
|
|
-# return
|
|
161
|
|
-
|
|
162
|
|
-# async def __kick_all_in_raid_message(self, message: Message):
|
|
163
|
|
-# """
|
|
164
|
|
-# Kicks all the users mentioned in the given raid warning message. Users who were already
|
|
165
|
|
-# kicked or banned will be skipped.
|
|
166
|
|
-# """
|
|
167
|
|
-# raid = self.__find_raid_for_message(message)
|
|
168
|
|
-# if raid is None:
|
|
169
|
|
-# await message.reply("This is either not a raid warning or it's too old and I don't " +
|
|
170
|
|
-# "have a record for it anymore. Sorry!")
|
|
171
|
|
-# return
|
|
172
|
|
-# self.__trace('Kicking...')
|
|
173
|
|
-# members = await raid.kick_all()
|
|
174
|
|
-# msg = 'Kicked these members:'
|
|
175
|
|
-# for member in members:
|
|
176
|
|
-# msg += f'\n\t{member.name}'
|
|
177
|
|
-# if len(members) == 0:
|
|
178
|
|
-# msg += '\n\t-none-'
|
|
179
|
|
-# self.__trace(msg)
|
|
180
|
|
-# self.__start_new_raid()
|
|
181
|
|
-# await self.__update_join_raid_message(raid)
|
|
182
|
|
-
|
|
183
|
|
-# async def __ban_all_in_raid_message(self, message: Message):
|
|
184
|
|
-# """
|
|
185
|
|
-# Bans all the users mentioned in the given raid warning message. Users who were already
|
|
186
|
|
-# banned will be skipped.
|
|
187
|
|
-# """
|
|
188
|
|
-# raid = self.__find_raid_for_message(message)
|
|
189
|
|
-# if raid is None:
|
|
190
|
|
-# await message.reply("This is either not a raid warning or it's too old and I don't " +
|
|
191
|
|
-# "have a record for it anymore. Sorry!")
|
|
192
|
|
-# return
|
|
193
|
|
-# self.__trace('Banning...')
|
|
194
|
|
-# members = await raid.ban_all()
|
|
195
|
|
-# msg = 'Banned these members:'
|
|
196
|
|
-# for member in members:
|
|
197
|
|
-# msg += f'\n\t{member.name}'
|
|
198
|
|
-# if len(members) == 0:
|
|
199
|
|
-# msg += '\n\t-none-'
|
|
200
|
|
-# self.__trace(msg)
|
|
201
|
|
-# self.__start_new_raid()
|
|
202
|
|
-# await self.__update_join_raid_message(raid)
|
|
203
|
|
-
|
|
204
|
|
-# def __find_raid_for_message(self, message: Message) -> JoinRaid:
|
|
205
|
|
-# """
|
|
206
|
|
-# Retrieves a JoinRaid instance for the given raid warning message. Returns None if not found.
|
|
207
|
|
-# """
|
|
208
|
|
-# for raid in self.all_raids:
|
|
209
|
|
-# if raid.warning_message.id == message.id:
|
|
210
|
|
-# return raid
|
|
211
|
|
-# return None
|
|
212
|
|
-
|
|
213
|
|
-# def __cull_old_raids(self, now: datetime):
|
|
214
|
|
-# """
|
|
215
|
|
-# Gets rid of old JoinRaid records from self.all_raids that are too old to still be useful.
|
|
216
|
|
-# """
|
|
217
|
|
-# i: int = 0
|
|
218
|
|
-# while i < len(self.all_raids):
|
|
219
|
|
-# raid = self.all_raids[i]
|
|
220
|
|
-# if raid == self.current_raid:
|
|
221
|
|
-# i += 1
|
|
222
|
|
-# continue
|
|
223
|
|
-# age_seconds = float((raid.raid_start_time - now).total_seconds())
|
|
224
|
|
-# if age_seconds > 86400.0:
|
|
225
|
|
-# self.__trace('Culling old raid')
|
|
226
|
|
-# self.all_raids.pop(i)
|
|
227
|
|
-# else:
|
|
228
|
|
-# i += 1
|
|
229
|
|
-
|
|
230
|
|
-# def __join_raid_message(self, raid: JoinRaid):
|
|
231
|
|
-# """
|
|
232
|
|
-# Returns a 3-element tuple containing a text message appropriate for posting in
|
|
233
|
|
-# Discord, a flag of whether any of the mentioned users can be kicked, and a flag
|
|
234
|
|
-# of whether any of the mentioned users can be banned.
|
|
235
|
|
-# """
|
|
236
|
|
-# message = ''
|
|
237
|
|
-# if self.warning_mention is not None:
|
|
238
|
|
-# message = self.warning_mention + ' '
|
|
239
|
|
-# message += '**RAID JOIN DETECTED!** It includes these users:\n'
|
|
240
|
|
-# can_kick = False
|
|
241
|
|
-# can_ban = False
|
|
242
|
|
-# for join in raid.joins:
|
|
243
|
|
-# message += '\n• '
|
|
244
|
|
-# if join.is_banned:
|
|
245
|
|
-# message += '~~' + join.member.mention + '~~ - banned'
|
|
246
|
|
-# elif join.is_kicked:
|
|
247
|
|
-# message += '~~' + join.member.mention + '~~ - kicked'
|
|
248
|
|
-# can_ban = True
|
|
249
|
|
-# else:
|
|
250
|
|
-# message += join.member.mention
|
|
251
|
|
-# can_kick = True
|
|
252
|
|
-# can_ban = True
|
|
253
|
|
-# message += '\n'
|
|
254
|
|
-# if can_kick:
|
|
255
|
|
-# message += '\nTo kick all these users, react with :' + CONFIG['kickEmojiName'] + ':'
|
|
256
|
|
-# else:
|
|
257
|
|
-# message += '\nNo kickable users remain'
|
|
258
|
|
-# if can_ban:
|
|
259
|
|
-# message += '\nTo ban all these users, react with :' + CONFIG['banEmojiName'] + ':'
|
|
260
|
|
-# else:
|
|
261
|
|
-# message += '\nNo bannable users remain'
|
|
262
|
|
-# return (message, can_kick, can_ban)
|
|
263
|
|
-
|
|
264
|
|
-# async def __update_join_raid_message(self, raid: JoinRaid):
|
|
265
|
|
-# """
|
|
266
|
|
-# Updates an existing join raid warning message with updated data.
|
|
267
|
|
-# """
|
|
268
|
|
-# if raid.warning_message is None:
|
|
269
|
|
-# self.__trace('No raid warning message to update')
|
|
270
|
|
-# return
|
|
271
|
|
-# (message, can_kick, can_ban) = self.__join_raid_message(raid)
|
|
272
|
|
-# await raid.warning_message.edit(content=message)
|
|
273
|
|
-# if not can_kick:
|
|
274
|
|
-# await raid.warning_message.clear_reaction(CONFIG['kickEmoji'])
|
|
275
|
|
-# if not can_ban:
|
|
276
|
|
-# await raid.warning_message.clear_reaction(CONFIG['banEmoji'])
|
|
277
|
|
-
|
|
278
|
|
-# async def __on_join_raid_begin(self, raid):
|
|
279
|
|
-# """
|
|
280
|
|
-# Event triggered when the first member joins that triggers the raid detection.
|
|
281
|
|
-# """
|
|
282
|
|
-# self.__trace('A join raid has begun!')
|
|
283
|
|
-# if self.warning_channel is None:
|
|
284
|
|
-# self.__trace('NO WARNING CHANNEL SET')
|
|
285
|
|
-# return
|
|
286
|
|
-# (message, can_kick, can_ban) = self.__join_raid_message(raid)
|
|
287
|
|
-# raid.warning_message = await self.warning_channel.send(message)
|
|
288
|
|
-# if can_kick:
|
|
289
|
|
-# await raid.warning_message.add_reaction(CONFIG['kickEmoji'])
|
|
290
|
|
-# if can_ban:
|
|
291
|
|
-# await raid.warning_message.add_reaction(CONFIG['banEmoji'])
|
|
292
|
|
-
|
|
293
|
|
-# async def __on_join_raid_updated(self, raid):
|
|
294
|
|
-# """
|
|
295
|
|
-# Event triggered for each subsequent member join after the first one that triggered the
|
|
296
|
|
-# raid detection.
|
|
297
|
|
-# """
|
|
298
|
|
-# self.__trace('Join raid still occurring')
|
|
299
|
|
-# await self.__update_join_raid_message(raid)
|
|
300
|
|
-
|
|
301
|
|
-# async def __on_join_raid_end(self, _raid):
|
|
302
|
|
-# """
|
|
303
|
|
-# Event triggered when the first member joins who is not part of the most recent raid.
|
|
304
|
|
-# """
|
|
305
|
|
-# self.__trace('Join raid has ended')
|
|
306
|
|
-
|
|
307
|
|
-# async def __warn(self, message):
|
|
308
|
|
-# """
|
|
309
|
|
-# Posts a warning message in the configured warning channel.
|
|
310
|
|
-# """
|
|
311
|
|
-# if self.warning_channel is None:
|
|
312
|
|
-# self.__trace('NO WARNING CHANNEL SET. Warning message not posted.\n' + message)
|
|
313
|
|
-# return None
|
|
314
|
|
-# m = message
|
|
315
|
|
-# if self.warning_mention is not None:
|
|
316
|
|
-# m = self.warning_mention + ' ' + m
|
|
317
|
|
-# return await self.warning_channel.send(m)
|
|
318
|
|
-
|
|
319
|
|
-# def __trace(self, message):
|
|
320
|
|
-# """
|
|
321
|
|
-# Debugging trace.
|
|
322
|
|
-# """
|
|
323
|
|
-# print(f'{self.guild.name}: {message}')
|
|
324
|
|
-
|
|
325
|
|
-# # lookup for int(Guild.guild_id) --> GuildContext
|
|
326
|
|
-# guild_id_to_guild_context = {}
|
|
327
|
|
-
|
|
328
|
|
-# def get_or_create_guild_context(val, save=True):
|
|
329
|
|
-# """
|
|
330
|
|
-# Retrieves a cached GuildContext instance by its Guild id or Guild object
|
|
331
|
|
-# itself. If no GuildContext record exists for the Guild, one is created
|
|
332
|
|
-# and cached (and saved to the database unless `save=False`).
|
|
333
|
|
-# """
|
|
334
|
|
-# gid = None
|
|
335
|
|
-# guild = None
|
|
336
|
|
-# if val is None:
|
|
337
|
|
-# return None
|
|
338
|
|
-# if isinstance(val, int):
|
|
339
|
|
-# gid = val
|
|
340
|
|
-# elif isinstance(val, Guild):
|
|
341
|
|
-# gid = val.id
|
|
342
|
|
-# guild = val
|
|
343
|
|
-# if gid is None:
|
|
344
|
|
-# print('Unhandled datatype', type(val))
|
|
345
|
|
-# return None
|
|
346
|
|
-# looked_up = guild_id_to_guild_context.get(gid)
|
|
347
|
|
-# if looked_up is not None:
|
|
348
|
|
-# return looked_up
|
|
349
|
|
-# gc = GuildContext(gid)
|
|
350
|
|
-# gc.guild = guild or gc.guild
|
|
351
|
|
-# guild_id_to_guild_context[gid] = gc
|
|
352
|
|
-# if save:
|
|
353
|
|
-# save_guild_context(gc)
|
|
354
|
|
-# return gc
|
|
355
|
|
-
|
|
356
|
|
-# # -- Database ---------------------------------------------------------------
|
|
357
|
|
-
|
|
358
|
|
-# def run_sql_batch(batch_function):
|
|
359
|
|
-# """
|
|
360
|
|
-# Performs an SQL transaction. After a connection is opened, the passed
|
|
361
|
|
-# function is invoked with the sqlite3.Connection and sqlite3.Cursor
|
|
362
|
|
-# passed as arguments. Once the passed function finishes, the connection
|
|
363
|
|
-# is closed.
|
|
364
|
|
-# """
|
|
365
|
|
-# db_connection: sqlite3.Connection = sqlite3.connect('rocketbot.db')
|
|
366
|
|
-# db_cursor: sqlite3.Cursor = db_connection.cursor()
|
|
367
|
|
-# batch_function(db_connection, db_cursor)
|
|
368
|
|
-# db_connection.commit()
|
|
369
|
|
-# db_connection.close()
|
|
370
|
|
-
|
|
371
|
|
-# def load_guild_settings():
|
|
372
|
|
-# """
|
|
373
|
|
-# Populates the GuildContext cache with records from the database.
|
|
374
|
|
-# """
|
|
375
|
|
-# def load(_con, cur):
|
|
376
|
|
-# """
|
|
377
|
|
-# SQL
|
|
378
|
|
-# """
|
|
379
|
|
-# for row in cur.execute("""SELECT * FROM guilds"""):
|
|
380
|
|
-# guild_id = row[0]
|
|
381
|
|
-# gc = get_or_create_guild_context(guild_id, save=False)
|
|
382
|
|
-# gc.warning_channel_id = row[1]
|
|
383
|
|
-# gc.warning_mention = row[2]
|
|
384
|
|
-# gc.join_warning_count = row[3] or CONFIG['joinWarningCount']
|
|
385
|
|
-# gc.join_warning_seconds = row[4] or CONFIG['joinWarningSeconds']
|
|
386
|
|
-# print(f'Guild {guild_id} channel id is {gc.warning_channel_id}')
|
|
387
|
|
-# run_sql_batch(load)
|
|
388
|
|
-
|
|
389
|
|
-# def create_tables():
|
|
390
|
|
-# """
|
|
391
|
|
-# Creates all database tables.
|
|
392
|
|
-# """
|
|
393
|
|
-# def make_tables(_con, cur):
|
|
394
|
|
-# """
|
|
395
|
|
-# SQL
|
|
396
|
|
-# """
|
|
397
|
|
-# cur.execute("""CREATE TABLE guilds (
|
|
398
|
|
-# guildId INTEGER,
|
|
399
|
|
-# warningChannelId INTEGER,
|
|
400
|
|
-# warningMention TEXT,
|
|
401
|
|
-# joinWarningCount INTEGER,
|
|
402
|
|
-# joinWarningSeconds INTEGER,
|
|
403
|
|
-# PRIMARY KEY(guildId ASC))""")
|
|
404
|
|
-# run_sql_batch(make_tables)
|
|
405
|
|
-
|
|
406
|
|
-# def save_guild_context(gc: GuildContext):
|
|
407
|
|
-# """
|
|
408
|
|
-# Saves the state of a GuildContext record to the database.
|
|
409
|
|
-# """
|
|
410
|
|
-# def save(_con, cur):
|
|
411
|
|
-# """
|
|
412
|
|
-# SQL
|
|
413
|
|
-# """
|
|
414
|
|
-# print(f'Saving guild context with id {gc.guild_id}')
|
|
415
|
|
-# cur.execute("""
|
|
416
|
|
-# SELECT guildId
|
|
417
|
|
-# FROM guilds
|
|
418
|
|
-# WHERE guildId=?
|
|
419
|
|
-# """, (
|
|
420
|
|
-# gc.guild_id,
|
|
421
|
|
-# ))
|
|
422
|
|
-# channel_id = gc.warning_channel.id if gc.warning_channel is not None \
|
|
423
|
|
-# else gc.warning_channel_id
|
|
424
|
|
-# exists = cur.fetchone() is not None
|
|
425
|
|
-# if exists:
|
|
426
|
|
-# print('Updating existing guild record in db')
|
|
427
|
|
-# cur.execute("""
|
|
428
|
|
-# UPDATE guilds
|
|
429
|
|
-# SET warningChannelId=?,
|
|
430
|
|
-# warningMention=?,
|
|
431
|
|
-# joinWarningCount=?,
|
|
432
|
|
-# joinWarningSeconds=?
|
|
433
|
|
-# WHERE guildId=?
|
|
434
|
|
-# """, (
|
|
435
|
|
-# channel_id,
|
|
436
|
|
-# gc.warning_mention,
|
|
437
|
|
-# gc.join_warning_count,
|
|
438
|
|
-# gc.join_warning_seconds,
|
|
439
|
|
-# gc.guild_id,
|
|
440
|
|
-# ))
|
|
441
|
|
-# else:
|
|
442
|
|
-# print('Creating new guild record in db')
|
|
443
|
|
-# cur.execute("""
|
|
444
|
|
-# INSERT INTO guilds (
|
|
445
|
|
-# guildId,
|
|
446
|
|
-# warningChannelId,
|
|
447
|
|
-# warningMention,
|
|
448
|
|
-# joinWarningCount,
|
|
449
|
|
-# joinWarningSeconds)
|
|
450
|
|
-# VALUES (?, ?, ?, ?, ?)
|
|
451
|
|
-# """, (
|
|
452
|
|
-# gc.guild_id,
|
|
453
|
|
-# channel_id,
|
|
454
|
|
-# gc.warning_mention,
|
|
455
|
|
-# gc.join_warning_count,
|
|
456
|
|
-# gc.join_warning_seconds,
|
|
457
|
|
-# ))
|
|
458
|
|
-# run_sql_batch(save)
|
|
459
|
|
-
|
|
460
|
|
-# # -- Main (1) ---------------------------------------------------------------
|
|
461
|
|
-
|
|
462
|
|
-# load_guild_settings()
|
|
463
|
|
-
|
|
464
|
|
-# intents = Intents.default()
|
|
465
|
|
-# intents.members = True # To get join/leave events
|
|
466
|
|
-# bot = commands.Bot(command_prefix=CONFIG['commandPrefix'], intents=intents)
|
|
467
|
|
-
|
|
468
|
|
-# # -- Bot commands -----------------------------------------------------------
|
|
469
|
|
-
|
|
470
|
|
-# @bot.command(
|
|
471
|
|
-# brief='Simply replies to the invoker with a hello message in the same channel.'
|
|
472
|
|
-# )
|
|
473
|
|
-# async def hello(ctx: Context):
|
|
474
|
|
-# """
|
|
475
|
|
-# Command handler
|
|
476
|
|
-# """
|
|
477
|
|
-# gc: GuildContext = get_or_create_guild_context(ctx.guild)
|
|
478
|
|
-# if gc is None:
|
|
479
|
|
-# return
|
|
480
|
|
-# message = ctx.message
|
|
481
|
|
-# await gc.command_hello(message)
|
|
482
|
|
-
|
|
483
|
|
-# @bot.command(
|
|
484
|
|
-# brief='Posts a test warning message in the configured warning channel.',
|
|
485
|
|
-# help="""If no warning channel is configured, the bot will reply in the channel the command was
|
|
486
|
|
-# issued to notify no warning channel is set. If a warning mention is configured, the test
|
|
487
|
|
-# warning will tag the configured person/role."""
|
|
488
|
|
-# )
|
|
489
|
|
-# @commands.has_permissions(manage_messages=True)
|
|
490
|
|
-# async def testwarn(ctx: Context):
|
|
491
|
|
-# """
|
|
492
|
|
-# Command handler
|
|
493
|
|
-# """
|
|
494
|
|
-# gc: GuildContext = get_or_create_guild_context(ctx.guild)
|
|
495
|
|
-# if gc is None:
|
|
496
|
|
-# return
|
|
497
|
|
-# await gc.command_testwarn(ctx)
|
|
498
|
|
-
|
|
499
|
|
-# @bot.command(
|
|
500
|
|
-# brief='Sets the threshold for detecting a join raid.',
|
|
501
|
|
-# usage='<count> <seconds>',
|
|
502
|
|
-# help="""The raid threshold is expressed as number of joins within a given number of seconds.
|
|
503
|
|
-# Each time a member joins, the number of joins in the previous _x_ seconds is counted, and if
|
|
504
|
|
-# that count, _y_, equals or exceeds the count configured by this command, a raid is detected."""
|
|
505
|
|
-# )
|
|
506
|
|
-# @commands.has_permissions(manage_messages=True)
|
|
507
|
|
-# async def setraidwarningrate(ctx: Context, count: int, seconds: int):
|
|
508
|
|
-# """
|
|
509
|
|
-# Command handler
|
|
510
|
|
-# """
|
|
511
|
|
-# gc: GuildContext = get_or_create_guild_context(ctx.guild)
|
|
512
|
|
-# if gc is None:
|
|
513
|
|
-# return
|
|
514
|
|
-# await gc.command_setraidwarningrate(ctx, count, seconds)
|
|
515
|
|
-
|
|
516
|
|
-# @bot.command(
|
|
517
|
|
-# brief='Sets the current channel as the destination for bot warning messages.'
|
|
518
|
|
-# )
|
|
519
|
|
-# @commands.has_permissions(manage_messages=True)
|
|
520
|
|
-# async def setwarningchannel(ctx: Context):
|
|
521
|
|
-# """
|
|
522
|
|
-# Command handler
|
|
523
|
|
-# """
|
|
524
|
|
-# gc: GuildContext = get_or_create_guild_context(ctx.guild)
|
|
525
|
|
-# if gc is None:
|
|
526
|
|
-# return
|
|
527
|
|
-# await gc.command_setwarningchannel(ctx)
|
|
528
|
|
-
|
|
529
|
|
-# @bot.command(
|
|
530
|
|
-# brief='Sets an optional mention to include in every warning message.',
|
|
531
|
|
-# usage='<mention>',
|
|
532
|
|
-# help="""The argument provided to this command will be included verbatim, so if the intent is
|
|
533
|
|
-# to tag a user or role, the argument must be a tag, not merely the name of the user/role."""
|
|
534
|
|
-# )
|
|
535
|
|
-# @commands.has_permissions(manage_messages=True)
|
|
536
|
|
-# async def setwarningmention(ctx: Context, mention: str):
|
|
537
|
|
-# """
|
|
538
|
|
-# Command handler
|
|
539
|
|
-# """
|
|
540
|
|
-# gc: GuildContext = get_or_create_guild_context(ctx.guild)
|
|
541
|
|
-# if gc is None:
|
|
542
|
|
-# return
|
|
543
|
|
-# await gc.command_setwarningmention(ctx, mention)
|
|
544
|
|
-
|
|
545
|
|
-# # -- Bot events -------------------------------------------------------------
|
|
546
|
|
-
|
|
547
|
|
-# is_connected = False
|
|
548
|
|
-# @bot.listen()
|
|
549
|
|
-# async def on_connect():
|
|
550
|
|
-# """
|
|
551
|
|
-# Discord event handler
|
|
552
|
|
-# """
|
|
553
|
|
-# global is_connected
|
|
554
|
|
-# print('Connected')
|
|
555
|
|
-# is_connected = True
|
|
556
|
|
-# if is_connected and is_ready:
|
|
557
|
|
-# await populate_guilds()
|
|
558
|
|
-
|
|
559
|
|
-# is_ready = False
|
|
560
|
|
-# @bot.listen()
|
|
561
|
|
-# async def on_ready():
|
|
562
|
|
-# """
|
|
563
|
|
-# Discord event handler
|
|
564
|
|
-# """
|
|
565
|
|
-# global is_ready
|
|
566
|
|
-# print('Ready')
|
|
567
|
|
-# is_ready = True
|
|
568
|
|
-# if is_connected and is_ready:
|
|
569
|
|
-# await populate_guilds()
|
|
570
|
|
-
|
|
571
|
|
-# async def populate_guilds():
|
|
572
|
|
-# """
|
|
573
|
|
-# Called after both on_ready and on_connect are done. May be called more than once!
|
|
574
|
|
-# """
|
|
575
|
|
-# for guild in bot.guilds:
|
|
576
|
|
-# gc = guild_id_to_guild_context.get(guild.id)
|
|
577
|
|
-# if gc is None:
|
|
578
|
|
-# print(f'No GuildContext for {guild.id}')
|
|
579
|
|
-# continue
|
|
580
|
|
-# gc.guild = guild
|
|
581
|
|
-# if gc.warning_channel_id is not None:
|
|
582
|
|
-# gc.warning_channel = guild.get_channel(gc.warning_channel_id)
|
|
583
|
|
-# if gc.warning_channel is not None:
|
|
584
|
|
-# print(f'Recovered warning channel {gc.warning_channel}')
|
|
585
|
|
-# else:
|
|
586
|
|
-# print(f'Could not find channel with id {gc.warning_channel_id} in ' +
|
|
587
|
|
-# f'guild {guild.name}')
|
|
588
|
|
-# for channel in await guild.fetch_channels():
|
|
589
|
|
-# print(f'\t{channel.name} ({channel.id})')
|
|
590
|
|
-
|
|
591
|
|
-# @bot.listen()
|
|
592
|
|
-# async def on_member_join(member: Member) -> None:
|
|
593
|
|
-# """
|
|
594
|
|
-# Discord event handler
|
|
595
|
|
-# """
|
|
596
|
|
-# print(f'User {member.name} joined {member.guild.name}')
|
|
597
|
|
-# gc: GuildContext = get_or_create_guild_context(member.guild)
|
|
598
|
|
-# if gc is None:
|
|
599
|
|
-# print(f'No GuildContext for guild {member.guild.name}')
|
|
600
|
|
-# return
|
|
601
|
|
-# await gc.handle_join(member)
|
|
602
|
|
-
|
|
603
|
|
-# @bot.listen()
|
|
604
|
|
-# async def on_member_remove(member: Member) -> None:
|
|
605
|
|
-# """
|
|
606
|
|
-# Discord event handler
|
|
607
|
|
-# """
|
|
608
|
|
-# print(f'User {member.name} left {member.guild.name}')
|
|
609
|
|
-
|
|
610
|
|
-# @bot.listen()
|
|
611
|
|
-# async def on_raw_reaction_add(payload: RawReactionActionEvent) -> None:
|
|
612
|
|
-# """
|
|
613
|
|
-# Discord event handler
|
|
614
|
|
-# """
|
|
615
|
|
-# guild: Guild = bot.get_guild(payload.guild_id)
|
|
616
|
|
-# channel: GuildChannel = guild.get_channel(payload.channel_id)
|
|
617
|
|
-# message: Message = await channel.fetch_message(payload.message_id)
|
|
618
|
|
-# member: Member = payload.member
|
|
619
|
|
-# emoji: PartialEmoji = payload.emoji
|
|
620
|
|
-# gc: GuildContext = get_or_create_guild_context(guild)
|
|
621
|
|
-# await gc.handle_reaction_add(message, member, emoji)
|
|
622
|
|
-
|
|
623
|
|
-# # -- Main -------------------------------------------------------------------
|
|
624
|
|
-
|
|
625
|
|
-# print('Starting bot')
|
|
626
|
|
-# bot.run(CONFIG['clientToken'])
|
|
627
|
|
-# print('Bot done')
|