-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_emoji.py
More file actions
556 lines (512 loc) Β· 18.8 KB
/
Copy pathmain_emoji.py
File metadata and controls
556 lines (512 loc) Β· 18.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
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
# Generic bot code (Unicode emoji instead of custom emoji). Use when self-hosting.
import ezcord
import discord
import platform
import importlib.metadata
import os
from datetime import datetime as dt
from dotenv import load_dotenv
load_dotenv()
website = (
"https://caltrop.asterisk.lol" # Will save a lot of time if the domain changes
)
public = (
1311438512045949029
)
webhookurl = str(os.getenv("WEBHOOK_URL"))
intents = discord.Intents.default()
bot = ezcord.Bot(
language="en", error_webhook_url=webhookurl, intents=intents, ready_event=None
)
def human_readable_size(size_in_bytes):
"""Convert bytes to a human-readable format."""
if size_in_bytes < 1024:
return f"{size_in_bytes} Bytes"
elif size_in_bytes < 1024**2:
return f"{size_in_bytes / 1024:.2f} KB"
elif size_in_bytes < 1024**3:
return f"{size_in_bytes / (1024 ** 2):.2f} MB"
else:
return f"{size_in_bytes / (1024 ** 3):.2f} GB"
def is_valid_hex_colour(colour):
"""Is this a hex code?"""
return len(colour) == 7 and colour.startswith("#")
colour_list = {
# Hex codes for all CSS named colours.
"aliceblue": 0xF0F8FF,
"antiquewhite": 0xFAEBD7,
"aqua": 0x00FFFF,
"aquamarine": 0x7FFFD4,
"azure": 0xF0FFFF,
"beige": 0xF5F5DC,
"bisque": 0xFFE4C4,
"black": 0x000000,
"blanchedalmond": 0xFFEBCD,
"blue": 0x0000FF,
"blueviolet": 0x8A2BE2,
"brown": 0xA52A2A,
"burlywood": 0xDEB887,
"cadetblue": 0x5F9EA0,
"chartreuse": 0x7FFF00,
"chocolate": 0xD2691E,
"coral": 0xFF7F50,
"cornflowerblue": 0x6495ED,
"cornsilk": 0xFFF8DC,
"crimson": 0xDC143C,
"cyan": 0x00FFFF,
"darkblue": 0x00008B,
"darkcyan": 0x008B8B,
"darkgoldenrod": 0xB8860B,
"darkgrey": 0xA9A9A9,
"darkgray": 0xA9A9A9,
"darkgreen": 0x006400,
"darkkhaki": 0xBDB76B,
"darkmagenta": 0x8B008B,
"darkolivegreen": 0x556B2F,
"darkorange": 0xFF8C00,
"darkorchid": 0x9932CC,
"darkred": 0x8B0000,
"darksalmon": 0xE9967A,
"darkseagreen": 0x8FBC8F,
"darkslateblue": 0x483D8B,
"darkslategrey": 0x2F4F4F,
"darkslategray": 0x2F4F4F,
"darkturquoise": 0x00CED1,
"darkviolet": 0x9400D3,
"deeppink": 0xFF1493,
"deepskyblue": 0x00BFFF,
"dimgrey": 0x696969,
"dimgray": 0x696969,
"dodgerblue": 0x1E90FF,
"firebrick": 0xB22222,
"floralwhite": 0xFFFAF0,
"forestgreen": 0x228B22,
"fuchsia": 0xFF00FF,
"gainsboro": 0xDCDCDC,
"ghostwhite": 0xF8F8FF,
"gold": 0xFFD700,
"goldenrod": 0xDAA520,
"gray": 0x808080,
"grey": 0x808080,
"green": 0x008000,
"greenyellow": 0xADFF2F,
"honeydew": 0xF0FFF0,
"hotpink": 0xFF69B4,
"indianred": 0xCD5C5C,
"indigo": 0x4B0082,
"ivory": 0xFFFFF0,
"khaki": 0xF0E68C,
"lavender": 0xE6E6FA,
"lavenderblush": 0xFFF0F5,
"lawngreen": 0x7CFC00,
"lemonchiffon": 0xFFFACD,
"lightblue": 0xADD8E6,
"lightcoral": 0xF08080,
"lightcyan": 0xE0FFFF,
"lightgoldenrodyellow": 0xFAFAD2,
"lightgrey": 0xD3D3D3,
"lightgray": 0xD3D3D3,
"lightgreen": 0x90EE90,
"lightpink": 0xFFB6C1,
"lightsalmon": 0xFFA07A,
"lightseagreen": 0x20B2AA,
"lightskyblue": 0x87CEFA,
"lightslategrey": 0x778899,
"lightslategray": 0x778899,
"lightsteelblue": 0xB0C4DE,
"lightyellow": 0xFFFFE0,
"lime": 0x00FF00,
"limegreen": 0x32CD32,
"linen": 0xFAF0E6,
"magenta": 0xFF00FF,
"maroon": 0x800000,
"mediumaquamarine": 0x66CDAA,
"mediumblue": 0x0000CD,
"mediumorchid": 0xBA55D3,
"mediumpurple": 0x9370DB,
"mediumseagreen": 0x3CB371,
"mediumslateblue": 0x7B68EE,
"mediumspringgreen": 0x00FA9A,
"mediumturquoise": 0x48D1CC,
"mediumvioletred": 0xC71585,
"midnightblue": 0x191970,
"mintcream": 0xF5FFFA,
"mistyrose": 0xFFE4E1,
"moccasin": 0xFFE4B5,
"navajowhite": 0xFFDEAD,
"navy": 0x000080,
"oldlace": 0xFDF5E6,
"olive": 0x808000,
"olivedrab": 0x6B8E23,
"orange": 0xFFA500,
"orangered": 0xFF4500,
"orchid": 0xDA70D6,
"palegoldenrod": 0xEEE8AA,
"palegreen": 0x98FB98,
"paleturquoise": 0xAFEEEE,
"palevioletred": 0xDB7093,
"papayawhip": 0xFFEFD5,
"peachpuff": 0xFFDAB9,
"peru": 0xCD853F,
"pink": 0xFFC0CB,
"plum": 0xDDA0DD,
"powderblue": 0xB0E0E6,
"purple": 0x800080,
"red": 0xFF0000,
"rosybrown": 0xBC8F8F,
"royalblue": 0x4169E1,
"saddlebrown": 0x8B4513,
"salmon": 0xFA8072,
"sandybrown": 0xF4A460,
"seagreen": 0x2E8B57,
"seashell": 0xFFF5EE,
"sienna": 0xA0522D,
"silver": 0xC0C0C0,
"skyblue": 0x87CEEB,
"slateblue": 0x6A5ACD,
"slategrey": 0x708090,
"slategray": 0x708090,
"snow": 0xFFFAFA,
"springgreen": 0x00FF7F,
"steelblue": 0x4682B4,
"tan": 0xD2B48C,
"teal": 0x008080,
"thistle": 0xD8BFD8,
"tomato": 0xFF6347,
"turquoise": 0x40E0D0,
"violet": 0xEE82EE,
"wheat": 0xF5DEB3,
"white": 0xFFFFFF,
"whitesmoke": 0xF5F5F5,
"yellow": 0xFFFF00,
"yellowgreen": 0x9ACD32,
}
@bot.event
async def on_ready():
activity = discord.CustomActivity(name="ποΈ Archiving messages")
await bot.change_presence(status=discord.Status.online, activity=activity)
bot.add_view(DeleteBookmark())
bot.ready(
style=ezcord.ReadyEvent.default,
)
class TagSet(discord.ui.Modal):
def __init__(self, *args, **kwargs) -> None:
super().__init__(timeout=None, *args, **kwargs)
# Modal for tag input
self.add_item(discord.ui.InputText(label="Message Tags", required=False))
async def callback(self, interaction: discord.Interaction):
await interaction.response.defer()
class ColourModal(discord.ui.Modal):
def __init__(self):
super().__init__(title="Customizer")
self.add_item(
discord.ui.InputText(
label="Embed Colour",
placeholder="Colour name (lightgreen) or hex code (#90ee90)",
required=True,
)
)
async def callback(self, interaction: discord.Interaction):
colour_input = self.children[0].value.strip()
if is_valid_hex_colour(colour_input):
colour = int(colour_input[1:], 16) # Convert hex to int
else:
# Check if the input is a named color
colour = colour_list.get(colour_input.lower())
if colour is None:
# Send an error message if the colour is invalid
await interaction.response.send_message(
content=f"β `{colour_input}` is not a valid colour! (try its hex code instead?)\n\nConsult the list of [CSS named colours](<https://www.w3.org/TR/css-color-4/#named-colors>) if you don't know the name of your colour.",
ephemeral=True,
)
return # Exit the callback
# Update the global embed colour
embed = interaction.message.embeds[0].copy()
embed.color = colour
# Edit the original message with the updated embed
await interaction.response.edit_message(embed=embed)
class DeleteBookmark(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(
label="", custom_id="delete", style=discord.ButtonStyle.secondary, emoji="ποΈ"
)
async def button_callback(self, button, interaction):
await interaction.response.defer()
embed = interaction.message.embeds[0]
numembeds = None
numfiles = None
for field in embed.fields:
name_lower = field.name.lower()
if "embeds" in name_lower:
try:
numembeds = int(field.value)
except ValueError:
numembeds = 0
if "attachments" in name_lower:
try:
numfiles = int(field.value)
except ValueError:
numfiles = 0
numembeds = numembeds or 0
numfiles = numfiles or 0
self.disable_all_items()
await interaction.message.delete()
if numembeds or numfiles != 0:
channel = interaction.channel
to_delete = int(numembeds + numfiles)
messages = await channel.history(
limit=to_delete, after=interaction.message.created_at
).flatten()
for i in messages:
await i.delete()
@discord.ui.button(
label="", custom_id="pin", style=discord.ButtonStyle.secondary, emoji="π"
)
async def pin_callback(self, button, interaction):
await interaction.message.pin()
await interaction.response.defer()
@discord.ui.button(
label="", custom_id="edit_tag", style=discord.ButtonStyle.secondary, emoji="βοΈ"
)
async def edit_tag_callback(self, button, interaction):
await interaction.response.send_modal(EditTagModal())
@discord.ui.button(
label="",
custom_id="move_to_bottom",
style=discord.ButtonStyle.secondary,
emoji="π½",
)
async def button2_callback(self, button, interaction):
for i in interaction.message.embeds:
i.set_footer(
text="πΌ Go back to the original message to view attachments and embeds."
)
await interaction.response.send_message(embed=i, view=DeleteBookmark())
@discord.ui.button(
label="", custom_id="customize", style=discord.ButtonStyle.secondary, emoji="π¨"
)
async def customizer(self, button, interaction):
await interaction.response.send_modal(ColourModal())
class EditTagModal(discord.ui.Modal):
def __init__(self):
super().__init__(title="Edit Tag(s)")
self.add_item(
discord.ui.InputText(
label="New tags",
placeholder="Enter new tag(s), leave this empty to remove all tags",
required=False,
)
)
async def callback(self, interaction: discord.Interaction):
embed = interaction.message.embeds[0].copy()
new_tag = self.children[0].value.strip()
fields = []
tag_found = False
for field in embed.fields:
if field.name == "π Tags":
tag_found = True
if new_tag:
fields.append(
discord.EmbedField(name="π Tags", value=new_tag, inline=True)
)
# If new_tag is empty, skip adding this field (removes it)
else:
fields.append(field)
# If no tag field was found and new_tag is not empty, add it
if not tag_found and new_tag:
fields.append(
discord.EmbedField(name="π Tags", value=new_tag, inline=True)
)
embed.clear_fields()
for field in fields:
embed.add_field(name=field.name, value=field.value, inline=field.inline)
await interaction.response.edit_message(embed=embed, view=DeleteBookmark())
class About(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
SiteButton = discord.ui.Button(
label="Website",
style=discord.ButtonStyle.url,
url="https://archiver.asterisk.lol",
)
self.add_item(SiteButton)
CodeButton = discord.ui.Button(
label="Source Code",
style=discord.ButtonStyle.url,
url="https://github.com/Ast3risk-ops/archiver",
)
self.add_item(CodeButton)
@bot.slash_command(
# This command can be used by guild members, but also by users anywhere if they install it
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="help",
description="How to use the bot",
)
async def help(ctx):
context_img = discord.File("context.png", filename="context.png")
embed = discord.Embed(
title="Using Archiver",
description="To use Archiver, simply right click or hold down on a message and go to **Apps > Archive Message**.",
color=discord.Colour.from_rgb(255, 255, 255),
)
embed.set_image(url="attachment://context.png")
await ctx.respond(embed=embed, file=context_img, ephemeral=True)
@bot.slash_command(
# This command can be used by guild members, but also by users anywhere if they install it
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="about",
description="Links and stuff",
)
async def about(ctx):
app_info = await bot.application_info()
owner = app_info.owner
managed_by = f"\n\nThis instance is managed by <@{owner.id}>."
embed = discord.Embed(
title="About",
description=f"[**Archiver**]({website}) is a bot to archive Discord messages, developed by [**Asterisk**](https://asterisk.lol).{managed_by}",
color=discord.Colour.from_rgb(255, 255, 255),
)
python_version = platform.python_version()
try:
pycord_version = importlib.metadata.version("py-cord")
except importlib.metadata.PackageNotFoundError:
pycord_version = "?"
try:
ezcord_version = importlib.metadata.version("ezcord")
except importlib.metadata.PackageNotFoundError:
ezcord_version = "?"
embed.set_thumbnail(url=ezcord.utils.avatar(str(public)))
embed.add_field(name="π° Servers", value=int(len(bot.guilds)), inline=True)
embed.add_field(name="π€ Users", value="?", inline=True)
embed.add_field(
name="π₯οΈ Host", value=f"{platform.system()} {platform.release()}", inline=True
)
embed.add_field(name="π Python", value=python_version, inline=True)
embed.add_field(
name="π Libraries",
value=f"[Pycord](https://pycord.dev) {pycord_version} w/ [ezcord](https://ezcord.rtfd.io) {ezcord_version}",
inline=True,
)
await ctx.respond(embed=embed, view=About(), ephemeral=True)
@bot.slash_command(
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="ping",
description="Pong! Check Discord API latency",
)
async def ping(ctx):
await ctx.respond(
f"π Pong! Discord API latency is **{round(bot.latency * 1000)}ms**",
ephemeral=True,
)
@bot.message_command(
# This command can be used by guild members, but also by users anywhere if they install it
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="Archive Message",
)
async def bookmark_tag(ctx, message: discord.Message):
modal = TagSet(title="Message Tag(s)")
await ctx.send_modal(modal)
await modal.wait() # Wait for the modal to be submitted before archiving
reactionlist = [] # Empty list of reactions to use later
for i in message.reactions:
count = i.count # Number of times reacted
formatted_emoji = str(i.emoji)
reactionlist.append(f"{count}x {formatted_emoji} ")
if message.content:
embed = discord.Embed(
title=f"ποΈ Archived Message On {discord.utils.format_dt(dt.now(), 'f')} ({discord.utils.format_dt(dt.now(), 'R')})",
description=f"-------\n\n{message.content}\n\n--------",
color=discord.Colour.random(),
)
else:
embed = discord.Embed(
title=f"ποΈ Archived Message On {discord.utils.format_dt(dt.now(), 'f')} ({discord.utils.format_dt(dt.now(), 'R')})",
color=discord.Colour.random(),
)
if message.poll:
embed.description = (
f"\n**π Poll**\n-------\nβ {message.poll.question.text}\n\n"
)
answertext = []
for i in message.poll.answers:
answerf = f"{i.id}. [{i.emoji}] {i.text} \n"
answertext.append(answerf)
embed.add_field(name="", value="".join(answertext), inline=False)
embed.add_field(name="\n\n", value=", ".join(reactionlist), inline=False)
embed.add_field(
name="π€ Author",
value=f"`@{message.author.name}` (<@{message.author.id}>)",
inline=True,
)
embed.add_field(name="π Link", value=f"{message.jump_url}", inline=True)
embed.add_field(name="πͺͺ Message ID", value=f"{message.id}", inline=True)
if message.guild:
embed.add_field(name="π° Guild", value=f"[{message.guild.id}](https://discordlookup.com/guild/{message.guild.id})", inline=True)
else:
embed.add_field(name="π° Guild", value="DM", inline=True)
if message.embeds:
embed.add_field(name="π² Embeds", value=f"{len(message.embeds)}", inline=True)
if message.attachments:
embed.add_field(name="πΈ Attachments", value=f"{len(message.attachments)}", inline=True)
embed.add_field(
name="π
Send Date",
value=f"{discord.utils.format_dt(message.created_at, 'F')}",
inline=True,
)
if modal.children[0].value:
embed.add_field(name="π Tags", value=f"{modal.children[0].value}", inline=True)
embed.set_thumbnail(url=ezcord.utils.avatar(f"{message.author.id}"))
try:
await ctx.user.send(embed=embed, view=DeleteBookmark())
except discord.Forbidden:
await ctx.respond(
f"βΉοΈ I can't DM you! Please enable DMs for this server or [install me as a user app](<https://discord.com/oauth2/authorize?client_id={bot.application_id}&integration_type=1&scope=applications.commands>) and try again.",
ephemeral=True,
)
else:
for i in message.attachments:
path = f"./{i.filename}"
await i.save(path)
try:
await ctx.user.send(file=discord.File(path))
except discord.HTTPException as e:
if e.status == 413:
filesize = human_readable_size(i.size)
await ctx.respond(
content=f"<@{ctx.user.id}> β οΈ Attachment `{i.filename}` is too large to re-upload ({filesize}). The URL for this attachment will be saved instead (this expires).",
ephemeral=True,
)
await ctx.user.send(content=i.url)
os.remove(path)
else:
await ctx.respond(
content=f"<@{ctx.user.id}> β Error while re-uploading attachment `{i.filename}`: \n\n```\n{e}\n```",
ephemeral=True,
)
os.remove(path)
else:
os.remove(path)
if message.stickers:
stickers = []
for i in message.stickers:
format = f"[{i.name}]({i.url})"
stickers.append(format)
await ctx.user.send(" ".join(stickers))
if message.embeds:
for i in message.embeds:
await ctx.user.send(embed=i)
if __name__ == "__main__":
bot.run(str(os.getenv("TOKEN"))) # run the bot with the token