-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
522 lines (476 loc) · 14.8 KB
/
bot.py
File metadata and controls
522 lines (476 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import sys
import time
import datetime
import datetime
import uuid
import json
import discord
import os
import re
import pandas as pd
import hashlib
import requests
########################################################################
# CONFIG SETUP
########################################################################
CONFIGFILE = sys.argv[1]
CONFIG = json.load(open(CONFIGFILE))
DISCORD_BOT_TOKEN = CONFIG['discord_bot_token']
SINK_URL = CONFIG['sink_url']
SINK_AUTHORIZATION = CONFIG['sink_authorization']
CERTIFICATE_PATH = CONFIG['certificate_path'] if CONFIG['certificate_path'] != '' else False
########################################################################
# VALIDATOR SETUP
########################################################################
df = pd.read_csv('discord-channels.csv')
APPROVED_CHANNELS = df.to_dict(orient='records')
APPROVED_CHANNEL_COMBOS = []
for entry in APPROVED_CHANNELS:
APPROVED_CHANNEL_COMBOS.append(str(entry['guild_id'])+':'+str(entry['channel_id'])+':'+str(entry['category_id']))
del df
########################################################################
# FUNCTIONS
########################################################################
def string_to_hash(any) -> str:
"""
Function to hash a string
"""
return hashlib.sha256(str(any).encode('utf-8')).hexdigest()
def validator(message: discord.Message) -> bool:
"""
Function to validate the guild:channel:category combos for incoming message
"""
observed_combo_on_channel = string_to_hash(str(message.guild.id))+':'+string_to_hash(str(message.channel.id))+':'+str('nan')
observed_combo_on_guild = string_to_hash(str(message.guild.id))+':'+str('nan')+':'+string_to_hash(str(message.channel.category_id))
if observed_combo_on_channel in APPROVED_CHANNEL_COMBOS or observed_combo_on_guild in APPROVED_CHANNEL_COMBOS:
return True
else:
return False
def genTS() -> dict:
"""
Function to generate a timestamp in both int and isoformat
"""
ts = datetime.datetime.utcnow().timestamp()
return {
'timestamp_int': int(ts),
'timestamp': datetime.datetime.fromtimestamp(ts).isoformat()
}
def genUID() -> str:
"""
Function to generate a unique ID
"""
return str(uuid.uuid4())
def createRecord(data: dict) -> dict:
"""
Function to create a record
"""
record = genTS()
record['uid'] = genUID()
record['record_id'] = str(record['timestamp_int']) + '-' + record['uid']
record.update(data)
record.update({'config': {
'name': CONFIG['name'],
'created_at': CONFIG['created_at'],
'filename': CONFIGFILE
}})
return record
def sinkData(data: dict = {}):
"""
Function to sink data to a location
"""
record = createRecord(data)
headers = {
'authorization': SINK_AUTHORIZATION
}
response = requests.post(SINK_URL, headers=headers, json=data, verify=CERTIFICATE_PATH)
return response
def parse_event_message(message: discord.Message) -> dict:
"""
Function to parse a message event
"""
try:
raw_mentions = message.raw_mentions
except:
raw_mentions = []
try:
raw_channel_mentions = message.raw_channel_mentions
except:
raw_channel_mentions = []
try:
raw_role_mentions = message.raw_role_mentions
except:
raw_role_mentions = []
try:
channel_mentions = message.channel_mentions
except:
channel_mentions = []
channel_mentions = []
for channel_tmp in message.channel_mentions:
channel_container = {}
try:
channel_container['id'] = channel_tmp.id
except:
channel_container['id'] = None
try:
channel_container['name'] = channel_tmp.name
except:
channel_container['name'] = None
try:
channel_container['position'] = channel_tmp.position
except:
channel_container['position'] = None
try:
channel_container['nsfw'] = channel_tmp.nsfw
except:
channel_container['nsfw'] = None
try:
channel_container['news'] = channel_tmp.news
except:
channel_container['news'] = None
try:
channel_container['category_id'] = channel_tmp.category_id
except:
channel_container['category_id'] = None
channel_mentions.append(channel_container)
try:
clean_content = message.clean_content
except:
clean_content = None
try:
created_at = message.created_at.isoformat()#.strftime('%Y-%m-%dT%H:%M:%SZ')
except:
created_at = None
try:
edited_at = message.edited_at.isoformat()#.strftime('%Y-%m-%dT%H:%M:%SZ')
except:
edited_at = None
try:
is_system = message.is_system()
except:
is_system = None
try:
system_content = message.system_content
except:
system_content = None
try:
activity = message.activity
except:
activity = None
try:
application = message.application
except:
application = None
try:
attachments = message.attachments
except:
attachments = []
if len(attachments) > 0:
attachment_container = []
for attachment in attachments:
tmp_attachment = {}
try:
tmp_attachment['id'] = attachment.id
except:
tmp_attachment['id'] = None
try:
tmp_attachment['filename'] = attachment.filename
except:
tmp_attachment['filename'] = None
try:
tmp_attachment['url'] = attachment.url
except:
tmp_attachment['url'] = None
attachment_container.append(tmp_attachment)
attachments = attachment_container
author = {}
try:
author['id'] = message.author.id
except:
author['id'] = None
try:
author['name'] = message.author.name
except:
author['name'] = None
try:
author['discriminator'] = message.author.discriminator
except:
author['discriminator'] = None
try:
author['bot'] = message.author.bot
except:
author['bot'] = None
try:
author['nick'] = message.author.nick
except:
author['nick'] = None
author_guild = {}
try:
author_guild_container = message.author.guild
author_guild['id'] = author_guild_container.id
author_guild['name'] = author_guild_container.name
author_guild['shard_id'] = author_guild_container.shard_id
author_guild['chunked'] = author_guild_container.chunked
author_guild['member_count'] = author_guild_container.member_count
except:
author_guild['id'] = None
author_guild['name'] = None
author_guild['shard_id'] = None
author_guild['chunked'] = None
author_guild['member_count'] = None
try:
components = message.components
except:
components = None
try:
content = message.content
except:
content = None
embeds_container = []
for embed in message.embeds:
try:
embeds_container.append(embed.to_dict())
except:
embeds_container.append({})
embeds = embeds_container
try:
flags = message.flags.value
except:
flags = None
try:
interaction = message.interaction
except:
interaction = None
try:
mention_everyone = message.mention_everyone
except:
mention_everyone = None
mentions = []
try:
for member in message.mentions:
mentions.append({'id':member.id,'content':member.content})
except:
pass
try:
nonce = message.nonce
except:
nonce = None
try:
pinned = message.pinned
except:
pinned = None
try:
reactions = message.reactions
except:
reactions = None
reference = {}
try:
reference_message_id = message.reference.message_id
except:
reference_message_id = None
try:
reference_channel_id = message.reference.channel_id
except:
reference_channel_id = None
try:
reference_guild_id = message.reference.guild_id
except:
reference_guild_id = None
reference = {
'message_id':reference_message_id,
'channel_id':reference_channel_id,
'guild_id':reference_guild_id
}
role_mentions = []
try:
for role in message.role_mentions:
role_mentions.append({'id':role.id,'name':role.name})
except:
pass
stickers = []
try:
for sticker in message.stickers:
stickers.append({'id':sticker.id,'name':sticker.name,'format':sticker.format,'url':sticker.url})
except:
pass
try:
tts = message.tts
except:
tts = None
try:
message_type = message.type._asdict()
except:
message_type = None
try:
webhook_id = message.webhook_id
except:
webhook_id = None
try:
jump_url = message.jump_url
except:
jump_url = None
channel = {}
try:
channel_container = message.channel
except:
channel_container = None
try:
channel['id'] = channel_container.id
except:
channel['id'] = None
try:
channel['name'] = channel_container.name
except:
channel['name'] = None
try:
channel['position'] = channel_container.position
except:
channel['position'] = None
try:
channel['nsfw'] = channel_container.nsfw
except:
channel['nsfw'] = None
try:
channel['news'] = channel_container.news
except:
channel['news'] = None
try:
channel['category_id'] = channel_container.category_id
except:
channel['category_id'] = None
guild = {}
guild_container = message.guild
try:
guild['id'] = guild_container.id
except:
guild['id'] = None
try:
guild['name'] = guild_container.name
except:
guild['name'] = None
try:
guild['shard_id'] = guild_container.shard_id
except:
guild['shard_id'] = None
try:
guild['chunked'] = guild_container.chunked
except:
guild['chunked'] = None
try:
guild['member_count'] = guild_container.member_count
except:
guild['member_count'] = None
try:
message_id = message.id
except:
message_id = None
return {
'raw_mentions': raw_mentions,
'raw_channel_mentions': raw_channel_mentions,
'raw_role_mentions': raw_role_mentions,
'channel_mentions': channel_mentions,
'clean_content': clean_content,
'created_at': created_at,
'edited_at': edited_at,
'is_system': is_system,
'system_content': system_content,
'activity': activity,
'application': application,
'attachments': attachments,
'author': author,
'author_guild': author_guild,
'components': components,
'content': content,
'embeds': embeds,
'flags': flags,
'interaction': interaction,
'mention_everyone': mention_everyone,
'mentions': mentions,
'nonce': nonce,
'pinned': pinned,
'reactions': reactions,
'reference': reference,
'role_mentions': role_mentions,
'stickers': stickers,
'tts': tts,
'type': message_type,
'webhook_id': webhook_id,
'jump_url': jump_url,
'channel': channel,
'guild': guild,
'id': message_id
}
def has_url(content: str) -> bool:
"""
Extracts URLs from the message content
"""
urls = []
try:
urls = re.findall(r'([A-Za-z]+://)([-\w]+(?:\.\w[-\w]*)+)(:\d+)?(/[^.!,?"<>\[\]{}\s\x7F-\xFF]*(?:[.!,?]+[^.!,?"<>\[\]{}\s\x7F-\xFF]+)*)?', content)
except:
pass
if urls == []:
return False
if urls != [] and len(urls) > 0:
return True
def extract_urls_from_content(content: str):
"""
Extracts URLs from the message content
"""
urls = []
try:
urls = re.findall(r'([A-Za-z]+://)([-\w]+(?:\.\w[-\w]*)+)(:\d+)?(/[^.!,?"<>\[\]{}\s\x7F-\xFF]*(?:[.!,?]+[^.!,?"<>\[\]{}\s\x7F-\xFF]+)*)?', content)
except:
pass
return urls
########################################################################
# DISCORD CLIENT SETUP
########################################################################
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
########################################################################
# ASYNC EVENT HANDLERS
########################################################################
@client.event
async def on_message(message):
"""
Handles the on_message event
"""
'''
# This is the original code, with maximum privacy features.
# Since this is going to remain cabal only for now, so we can kick some of this.
if validator(message):
parsed_message_event = parse_event_message(message)
urls = extract_urls_from_content(parsed_message_event['content'])
content_sha256 = string_to_hash(parsed_message_event['content'])
guild_id_sha256 = string_to_hash(parsed_message_event['guild']['id'])
channel_id_sha256 = string_to_hash(parsed_message_event['channel']['id'])
author_id_sha256 = string_to_hash(parsed_message_event['author']['id'])
for url in urls:
sinkData({'url':url,'guild':guild_id_sha256,'channel': channel_id_sha256, 'author': author_id_sha256, 'content':content_sha256})
await message.add_reaction('🔗')
'''
# if the message is valid (e.g. coming from the approved channels)
if validator(message):
if has_url(message.content):
parsed_message_event = parse_event_message(message)
author_id = str(parsed_message_event['author']['id'])
channel_id = str(parsed_message_event['channel']['id'])
guild_id = str(parsed_message_event['guild']['id'])
content = str(parsed_message_event['content'])
data = parsed_message_event
response = sinkData({
'author_id': author_id,
'guild_id':guild_id,
'channel_id': channel_id,
'content':content,
'data': data
})
if response.status_code in (200,201):
await message.add_reaction('🔗')
else:
print(response.text)
########################################################################
# MAIN RUN
########################################################################
client.run(DISCORD_BOT_TOKEN)