4
4
import aiohttp
5
5
from bs4 import BeautifulSoup
6
6
from copy import deepcopy
7
+ import aiosqlite
7
8
8
9
image_types = {'image/jpeg' , 'image/png' , 'image/webp' }
9
10
@@ -48,7 +49,7 @@ async def create_send_embeds(ctx: discord.Interaction, messages: list[discord.Me
48
49
if not show_original and not embeds [i ].fields [- 1 ].inline :
49
50
embeds [i ].remove_field (- 1 )
50
51
if show_ids :
51
- embeds [i ].set_author (name = f 'ID: ' + str (i + 1 ))
52
+ embeds [i ].set_author (name = 'ID: ' + str (i + 1 ))
52
53
continue
53
54
tenor = message .embeds and message .embeds [0 ].url is not None and message .embeds [0 ].url .startswith ('https://tenor.com/view/' ) # kill tenor
54
55
image = discord .utils .find (lambda a : a .content_type in image_types , message .attachments )
@@ -72,9 +73,60 @@ async def create_send_embeds(ctx: discord.Interaction, messages: list[discord.Me
72
73
if show_original :
73
74
embeds [i ].add_field (name = '' , value = f'-# [{ message .author .name } ・<t:{ int (message .created_at .timestamp ())} :t>]({ message .jump_url } )' , inline = False )
74
75
if show_ids :
75
- embeds [i ].set_author (name = f 'ID: ' + str (i + 1 ))
76
+ embeds [i ].set_author (name = 'ID: ' + str (i + 1 ))
76
77
if anonymous :
77
78
return {'embeds' : embeds }
78
79
view = discord .ui .View ()
79
80
view .add_item (discord .ui .Button (label = f'Forwarded by { ctx .user .name } ' if not ctx .locale is discord .Locale .russian else f'Переслано { ctx .user .name } ' , disabled = True ))
80
81
return {'embeds' : embeds , 'view' : view }
82
+
83
+ # database shit
84
+
85
+ async def initiate_db (filename : str ):
86
+ conn = await aiosqlite .connect (filename )
87
+ cursor = await conn .cursor ()
88
+ await cursor .executescript ('''
89
+ CREATE TABLE IF NOT EXISTS Messages (
90
+ id INTEGER,
91
+ message_id INTEGER,
92
+ user_id INTEGER,
93
+ jump_url TEXT,
94
+ message TEXT,
95
+ timestamp INTEGER
96
+ );
97
+ CREATE TABLE IF NOT EXISTS Attachments (
98
+ message_id INTEGER,
99
+ url TEXT,
100
+ image INTEGER
101
+ )''' )
102
+ await conn .commit ()
103
+ await conn .close ()
104
+
105
+ async def add_message (ctx : discord .Interaction , message : discord .Message ):
106
+ conn = await aiosqlite .connect ('messages.db' )
107
+ cursor = await conn .cursor ()
108
+ await cursor .execute ('SELECT id FROM Messages WHERE user_id = ? ORDER BY id DESC' , (ctx .user .id ,))
109
+ id = (await cursor .fetchone ())[0 ] + 1
110
+ if message .author .id == ctx .client .user .id :
111
+ pass
112
+ else :
113
+ await cursor .execute ('INSERT INTO Messages VALUES (?, ?, ?, ?, ?, ?)' , (id , message .id , ctx .user .id , message .jump_url , message .content , int (message .created_at .timestamp ())))
114
+ tenor = message .embeds and message .embeds [0 ].url is not None and message .embeds [0 ].url .startswith ('https://tenor.com/view/' )
115
+ image = discord .utils .find (lambda a : a .content_type in image_types , message .attachments )
116
+ if image is None :
117
+ if tenor :
118
+ async with aiohttp .ClientSession (headers = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' }) as session :
119
+ async with session .get (message .embeds [0 ].url ) as r :
120
+ data = await r .text ()
121
+ image = f"https://c.tenor.com/{ BeautifulSoup (data , 'html.parser' ).find ('meta' , itemprop = 'contentUrl' )['content' ].split ('/' )[4 ]} /tenor.gif"
122
+ elif message .embeds and message .embeds [0 ].type == 'image' :
123
+ image = message .embeds [0 ].url
124
+ else :
125
+ image = image .url
126
+ await cursor .execute ('INSERT INTO Attachments VALUES (?, ?, ?)' , (message .id , image , 1 ))
127
+ for attachment in message .attachments :
128
+ if attachment .url == image :
129
+ continue
130
+ await cursor .execute ('INSERT INTO Attachments VALUES (?, ?, ?)' , (message .id , attachment .url , 0 ))
131
+ await conn .commit ()
132
+ await conn .close ()
0 commit comments