-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
622 lines (507 loc) · 15.9 KB
/
main.py
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import time
import api
from config import *
from log import *
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
from typing import *
import utils
# loading token
load_dotenv()
TOKEN = os.getenv('BOT_TOKEN')
bot = commands.Bot(command_prefix=PREFIX, intents=discord.Intents.default(), help_command=None)
mg = api.Manager(USERS_FILE)
# functions
def get_paginated_embed(
page:int, results:List[api.Message]
) -> Tuple[discord.Embed, List[api.Message], int]:
'''
Converts a list of search results to a paginated embed.
'''
max_page = int(len(results)/PAGE_LEN) + \
(1 if len(results)%PAGE_LEN != 0 else 0)
page = min(max(page, 1), max_page)
stripped: List[api.Message] = results[(page-1)*PAGE_LEN:page*PAGE_LEN]
embed = discord.Embed(
color=discord.Color.green(),
title=f"**Found {len(results)} bookmarks**"
)
for i in stripped:
desc = f'-# {utils.remove_md(i.author_name)} ・ [Jump]({i.link})'
if i.attachments:
lenat = len(i.attachments)
desc += f' ・ {lenat} {ATT}'
if i.tags:
desc += f'\n-# Tags: `{"`, `".join(i.tags)}`'
embed.add_field(name=utils.shorten_string(i.note, NOTE_LEN), value=desc, inline=False)
embed.set_footer(text=f'Showing page {page} of {max_page}')
return (embed, stripped, page)
def get_manage_view(
bm:api.Message,
jump:bool=True,
note:bool=True,
remove:bool=True,
tags:bool=True
):
view = discord.ui.View()
if jump:
button = discord.ui.Button(
style=discord.ButtonStyle.gray,
label='Go to message', url=bm.link
)
view.add_item(button)
if note:
n_button = discord.ui.Button(
style=discord.ButtonStyle.blurple,
label='Set note',
custom_id=f's{bm.id}'
)
view.add_item(n_button)
if remove:
r_button = discord.ui.Button(
style=discord.ButtonStyle.red,
label='Remove',
custom_id=f'r{bm.id}'
)
view.add_item(r_button)
if tags:
t_button = discord.ui.Button(
style=discord.ButtonStyle.green,
label='Add tag',
custom_id=f't{bm.id}',
row=1,
disabled=len(bm.tags) >= MAX_TAGS
)
view.add_item(t_button)
if bm.tags != []:
t_dropdown = discord.ui.Select(
placeholder='Remove tags...',
min_values=1,
max_values=1,
options=[
discord.SelectOption(label=i, value=i) for i in bm.tags
],
custom_id=f'u{bm.id}'
)
view.add_item(t_dropdown)
return view
def get_bm_embed(bm:api.Message):
'''
Returns an embed to show when managing a bookmark.
'''
# author, send and save time
send_time = f'<t:{int(bm.sent_at)}:R>'
save_time = f'<t:{int(bm.saved_at)}:R>'
stats = f'-# {utils.remove_md(bm.author_name)}'\
f' ・ {SENT} {send_time} ・ {SAVE} {save_time}'
# tags
if bm.tags != []:
stats += f'\n-# Tags: `{"`, `".join(bm.tags)}`'
# attachments
lenat = len(bm.attachments)
if lenat != 0:
stats += f'\n\n-# {lenat} attachment{"s" if lenat != 1 else ""}'
for i in bm.attachments:
stats += f'\n- -# [{i.filename.removesuffix("."+i.extension)}]({i.url}) ・ '\
f'{i.type.capitalize()}, .{i.extension}'
# composing embed
embed = discord.Embed(
title=bm.note,
color=discord.Color.green(),
description=bm.text
)
embed.add_field(
name='',
value=stats
)
# channel and guild id
embed.set_footer(
text=f'Message: {bm.id}'+\
f'\nChannel: {bm.channel_id}'+\
(f'\nGuild: {bm.guild_id}' if bm.guild_id else '')
)
return embed
# connection events
@bot.event
async def on_ready():
log(f'Ready as {bot.user.name}!')
# commands = await bot.tree.sync()
# log(f'Synced tree with {len(commands)} commands', level=SUCCESS)
# events
async def handle_modal(inter:discord.Interaction):
'''
Handles modal submitting.
'''
action = inter.data['custom_id'][0]
id = int(inter.data['custom_id'][1:])
# note
if action == 'n':
note = inter.data['components'][0]['components'][0]['value']
mg.set_note(inter.user.id, id, note)
embed = discord.Embed(
color=discord.Color.green(),
description=f'**Note updated!**\n\n{note}'
)
# note but editing manage message
elif action == 's':
note = inter.data['components'][0]['components'][0]['value']
mg.set_note(inter.user.id, id, note)
bm = mg.get_bookmark(inter.user.id, id)
embed = get_bm_embed(bm)
view = get_manage_view(bm)
await inter.response.defer()
await inter.edit_original_response(
embed=embed, view=view
)
return
# tag
elif action == 't':
tag = inter.data['components'][0]['components'][0]['value']
out = mg.add_tag(inter.user.id, id, tag)
tag = tag.lower().replace(' ','_')
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description=f'**Unable to add a tag!**\n\n'\
'- This tag may already be on a message\n'\
f'- The maximum number of tags has been reached ({MAX_TAGS})\n'\
'- This message is not bookmarked'
)
else:
bm = mg.get_bookmark(inter.user.id, id)
view = get_manage_view(bm)
embed = get_bm_embed(bm)
await inter.response.defer()
await inter.edit_original_response(
embed=embed, view=view
)
return
# unknown
else:
embed = discord.Embed(
color=discord.Color.red(),
description='**Unknown action!**'
)
await inter.response.send_message(embed=embed, ephemeral=True)
async def handle_dropdown(inter:discord.Interaction):
'''
Handles dropdown submitting.
Only handles dropdowns that do not have a single character in their custom id.
'''
action = inter.data['custom_id'][0]
id = int(inter.data['custom_id'][1:])
value = inter.data['values'][0]
# removing tag
if action == 'u':
out = mg.remove_tag(inter.user.id, id, value)
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description=f'**Tag `{value}` not found or message is not bookmarked!**'
)
else:
# changing message
bm = mg.get_bookmark(inter.user.id, id)
view = get_manage_view(bm)
embed = get_bm_embed(bm)
await inter.response.defer()
await inter.edit_original_response(
embed=embed, view=view
)
return
# unknown
else:
embed = discord.Embed(
color=discord.Color.red(),
description='**Unknown action!**'
)
await inter.response.send_message(embed=embed, ephemeral=True)
@bot.event
async def on_interaction(inter:discord.Interaction):
'''
Gets called when a button is pressed or a command is used.
'''
if inter.type == discord.InteractionType.modal_submit:
await handle_modal(inter)
return
if inter.type != discord.InteractionType.component:
return
# answering
if inter.data['component_type'] == 3:
if len(inter.data['custom_id']) > 1:
await handle_dropdown(inter)
return
action = inter.data['custom_id'][0]
id = int(inter.data['values'][0])
else:
action = inter.data['custom_id'][0]
id = int(inter.data['custom_id'][1:])
# viewing
if action == 'b':
out = mg.get_bookmark(inter.user.id, id)
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description='**Not bookmarked!**'
)
else:
view = get_manage_view(out)
embed = get_bm_embed(out)
await inter.response.send_message(
embed=embed, view=view, ephemeral=True
)
return
# removing bookmark
if action == 'r':
out = mg.remove_bookmark(inter.user.id, id)
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description='**Not bookmarked!**'
)
else:
embed = discord.Embed(
color=discord.Color.green(),
description='**Bookmark removed!**'
)
await inter.response.defer()
await inter.edit_original_response(embed=embed, view=None)
return
# setting note
elif action == 'n' or action == 's':
bm = mg.get_bookmark(inter.user.id, id)
if bm != None:
modal = discord.ui.Modal(
title='Set a note',
custom_id=f'{action}{id}'
)
input = discord.ui.TextInput(
label='Note',
custom_id='note',
placeholder=bm.note,
max_length=NOTE_LEN,
min_length=1
)
modal.add_item(input)
await inter.response.send_modal(modal)
return
embed = discord.Embed(
color=discord.Color.red(),
description='**Not bookmarked!**'
)
# adding tag
elif action == 't':
bm = mg.get_bookmark(inter.user.id, id)
if len(bm.tags) >= MAX_TAGS:
embed = discord.Embed(
color=discord.Color.red(),
description=f'**You can only have {len(bm.tags)} tags on a bookmark!**'
)
await inter.response.send_message(embed=embed, ephemeral=True)
if bm != None:
modal = discord.ui.Modal(
title='Add a tag',
custom_id=f't{id}'
)
input = discord.ui.TextInput(
label='Booru tag',
custom_id='tag',
placeholder=bm.note,
max_length=TAG_LEN,
min_length=1
)
modal.add_item(input)
await inter.response.send_modal(modal)
return
embed = discord.Embed(
color=discord.Color.red(),
description='**Not bookmarked!**'
)
# unknown action
else:
embed = discord.Embed(
color=discord.Color.red(),
description='**Unknown action!**'
)
await inter.response.send_message(embed=embed, ephemeral=True)
# context menus
@bot.tree.context_menu(name='Set note')
@discord.app_commands.user_install()
async def note(
inter:discord.Interaction,
message:discord.Message
):
'''
Bookmarks a message.
'''
# bookmarking
out = mg.get_bookmark(inter.user.id, message.id)
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description='**Not bookmarked!**'
)
else:
modal = discord.ui.Modal(
title='Set a note',
custom_id=f'n{message.id}'
)
input = discord.ui.TextInput(
label='Note',
custom_id='note',
placeholder=out.note,
max_length=NOTE_LEN,
min_length=1
)
modal.add_item(input)
await inter.response.send_modal(modal)
return
await inter.response.send_message(
embed=embed, ephemeral=True
)
@bot.tree.context_menu(name='Bookmark')
@discord.app_commands.user_install()
async def bookmark(
inter:discord.Interaction,
message:discord.Message
):
'''
Bookmarks a message.
'''
# bookmarking
out = mg.bookmark(inter.user.id, message)
if not out:
embed = discord.Embed(
color=discord.Color.red(),
description='**Already bookmarked!**'
)
else:
embed = discord.Embed(
color=discord.Color.green(),
description='**Bookmarked!**'
)
view = discord.ui.View()
button = discord.ui.Button(
style=discord.ButtonStyle.blurple,
label='Manage',
custom_id=f'b{message.id}'
)
view.add_item(button)
await inter.response.send_message(
embed=embed, view=view, ephemeral=True
)
# commands
@bot.tree.command(
name='search',
description='Search or view bookmarks.'
)
@discord.app_commands.user_install()
@discord.app_commands.describe(
prompt='Search prompt. Leave blank to show all.',
case='Whether the search query is case-sensitive or not.',
page='Page to skip to.'
)
async def view_text(
inter:discord.Interaction,
prompt:str='',
case:Literal['Case sensitive','Case insensitive']='Case insensitive',
page:int=1
):
'''
Searches for bookmarked messages.
'''
if len(prompt) > MAX_PROMPT_LEN:
embed = discord.Embed(
color=discord.Color.red(),
description=f'**Prompt too long!**\n\n'\
f'{MAX_PROMPT_LEN} characters max.'
)
await inter.response.send_message(embed=embed, ephemeral=True)
return
user = mg.get_user(inter.user.id)
search: List[api.Message] = user.search(
prompt, case == 'Case sensitive'
)
if len(search) == 0:
embed = discord.Embed(
color=discord.Color.red(),
description='**No bookmarks found!**'
)
else:
embed, elements, page = get_paginated_embed(
page=page, results=search
)
elements: List[api.Message]
max_page = int(len(search)/PAGE_LEN) + \
(1 if len(search)%PAGE_LEN != 0 else 0)
# view = get_paginated_view(page, max_page)
view = discord.ui.View()
dd = discord.ui.Select(
placeholder='Manage...',
min_values=1,
max_values=1,
custom_id='b',
options=[
discord.SelectOption(
label=i.note,
description=f'{i.author_name} - {i.id}',
value=f'{i.id}',
) for i in elements
]
)
view.add_item(dd)
await inter.response.send_message(
embed=embed, view=view, ephemeral=True
)
return
await inter.response.send_message(embed=embed, ephemeral=True)
@bot.tree.command(
name='manage',
description='Edit or delete a bookmark.'
)
@discord.app_commands.user_install()
@discord.app_commands.describe(
id='Bookmark ID'
)
async def view_text(
inter:discord.Interaction,
id:str
):
'''
Manage a bookmark.
'''
if id.isnumeric():
bm = mg.get_bookmark(inter.user.id, int(id))
else:
bm = None
if bm == None:
embed = discord.Embed(
color=discord.Color.red(),
description='**Bookmark not found!**'
)
await inter.response.send_message(embed=embed,ephemeral=True)
return
view = get_manage_view(bm)
embed = get_bm_embed(bm)
await inter.response.send_message(
embed=embed, view=view, ephemeral=True
)
@view_text.autocomplete('id')
async def manage_autocomplete(
inter:discord.Interaction,
current:str
) -> List[discord.app_commands.Choice[str]]:
'''
Autocomplete for manage command.
'''
user = mg.get_user(inter.user.id)
return [
discord.app_commands.Choice(
name=f'{utils.shorten_string(i.note,50)} ({i.id})', value=str(i.id)
) for i in user.saved.values()\
if str(i.id).startswith(current) or current == ''
][::-1][:25]
## RUNNING BOT
bot.run(TOKEN)