Skip to content

Commit 837a9b2

Browse files
committedSep 24, 2024
few sqlite functions
1 parent 1eca3ae commit 837a9b2

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.env
2-
__pycache__
2+
__pycache__
3+
messages.db

‎main.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import discord
22
from discord import app_commands
33
from dotenv import load_dotenv
4-
from os import getenv
4+
from os import getenv, path
55
from utils import *
6+
import aiosqlite
67
load_dotenv()
78

89
bot = discord.Client(intents=discord.Intents.default())
@@ -15,6 +16,8 @@ async def on_ready():
1516
global SEND, DELETE, initialized
1617
if initialized:
1718
return
19+
if not path.isfile('messages.db'):
20+
await initiate_db('messages.db')
1821
await tree.set_translator(MyTranslator())
1922
for command in await tree.sync():
2023
if command.name == 'send':

‎requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
python-dotenv
22
discord.py>=2.4
33
aiohttp
4-
beautifulsoup4
4+
beautifulsoup4
5+
aiosqlite

‎utils.py

+54-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import aiohttp
55
from bs4 import BeautifulSoup
66
from copy import deepcopy
7+
import aiosqlite
78

89
image_types = {'image/jpeg', 'image/png', 'image/webp'}
910

@@ -48,7 +49,7 @@ async def create_send_embeds(ctx: discord.Interaction, messages: list[discord.Me
4849
if not show_original and not embeds[i].fields[-1].inline:
4950
embeds[i].remove_field(-1)
5051
if show_ids:
51-
embeds[i].set_author(name=f'ID: ' + str(i+1))
52+
embeds[i].set_author(name='ID: ' + str(i+1))
5253
continue
5354
tenor = message.embeds and message.embeds[0].url is not None and message.embeds[0].url.startswith('https://tenor.com/view/') # kill tenor
5455
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
7273
if show_original:
7374
embeds[i].add_field(name='', value=f'-# [{message.author.name}・<t:{int(message.created_at.timestamp())}:t>]({message.jump_url})', inline=False)
7475
if show_ids:
75-
embeds[i].set_author(name=f'ID: ' + str(i+1))
76+
embeds[i].set_author(name='ID: ' + str(i+1))
7677
if anonymous:
7778
return {'embeds': embeds}
7879
view = discord.ui.View()
7980
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))
8081
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

Comments
 (0)