misc/migration related

This commit is contained in:
2025-06-08 08:53:27 -04:00
parent 046ad4f94a
commit 75addd629c
6 changed files with 113 additions and 33 deletions

View File

@ -136,10 +136,10 @@ class Meme(commands.Cog):
def __init__(self, bot: Havoc) -> None:
self.bot: Havoc = bot
self.stats_db_path: str = os.path.join(
"/mnt/data/share", "sqlite_dbs", "stats.db"
"/", "usr", "local", "share", "sqlite_dbs", "stats.db"
)
self.memedb_path: str = os.path.join(
"/mnt/data/share", "sqlite_dbs", "meme.db"
self.meme_db_path: str = os.path.join(
"/", "usr", "local", "share", "sqlite_dbs", "meme.db"
)
self.meme_choices: list = []
self.meme_counter: int = 0
@ -264,9 +264,10 @@ class Meme(commands.Cog):
phash: str = str(imagehash.phash(_image))
query: str = "INSERT INTO memes(discord_uid, timestamp, image, message_ids, phash) VALUES(?, ?, ?, ?, ?)"
image.seek(0)
async with sqlite3.connect(self.memedb_path, timeout=5) as db_conn:
converted = self.convert_to_png(image)
async with sqlite3.connect(self.meme_db_path, timeout=5) as db_conn:
insert = await db_conn.execute_insert(
query, (discord_uid, timestamp, image.read(), message_id, phash)
query, (discord_uid, timestamp, converted, message_id, phash)
)
if insert:
await db_conn.commit()
@ -286,7 +287,7 @@ class Meme(commands.Cog):
try:
phash: str = str(imagehash.phash(image))
query: str = "SELECT message_ids FROM memes WHERE phash = ? LIMIT 1"
async with sqlite3.connect(self.memedb_path, timeout=2) as db_conn:
async with sqlite3.connect(self.meme_db_path, timeout=2) as db_conn:
db_conn.row_factory = sqlite3.Row
async with await db_conn.execute(query, (phash,)) as db_cursor:
result = await db_cursor.fetchone()
@ -741,12 +742,15 @@ class Meme(commands.Cog):
dupe_check = await self.dupe_check(Image.open(image))
if dupe_check:
channel = message.channel
original_message = await channel.fetch_message(dupe_check) # type: ignore
original_message_url = original_message.jump_url
await message.add_reaction(
emoji="<:quietscheentchen:1255956612804247635>"
)
await message.reply(original_message_url)
try:
original_message = await channel.fetch_message(dupe_check) # type: ignore
original_message_url = original_message.jump_url
await message.add_reaction(
emoji="<:quietscheentchen:1255956612804247635>"
)
await message.reply(original_message_url)
except Exception as e:
logging.info("Failed to mark original image: %s", str(e))
else:
image.seek(0)
unique_memes.append(image)
@ -856,6 +860,24 @@ class Meme(commands.Cog):
else:
await ctx.respond("NO embed :(")
def convert_to_png(self, in_buffer: io.BytesIO) -> bytes:
"""
Convert an in-memory buffer to PNG
Args:
in_buffer (io.BytesIO)
Returns:
bytes
"""
in_buffer.seek(0)
with Image.open(in_buffer) as im:
if im.format == "PNG":
raise ValueError("Already a PNG")
out_buffer = io.BytesIO()
im.save(out_buffer, format="PNG")
out_buffer.seek(0)
return out_buffer.read()
def cog_unload(self) -> None:
self.meme_stream_loop.cancel()
self.explosm_loop.cancel()