This commit is contained in:
2025-05-20 11:13:49 -04:00
parent b13c2eec2a
commit b3f0e084ce
6 changed files with 58 additions and 403 deletions

View File

@ -16,6 +16,7 @@ import discord
from disc_havoc import Havoc
from aiohttp import ClientSession
from discord.ext import bridge, commands, tasks
from util.discord_helpers import log_to_playground
from util.jesusmemes import JesusMemeGenerator
import scrapers.reddit_scrape as memeg
import scrapers.explosm_scrape as explosmg
@ -209,7 +210,7 @@ class Meme(commands.Cog):
else:
self.meme_leaderboard[uid] += 1
async with sqlite3.connect(self.stats_db_path, timeout=2) as db_conn:
async with sqlite3.connect(self.stats_db_path, timeout=5) as db_conn:
"""Attempts both insert/update"""
query_1: str = "UPDATE memes SET count = count + 1 WHERE discord_uid = ?"
query_1_params: tuple = (uid,)
@ -238,7 +239,7 @@ class Meme(commands.Cog):
INIT MEME LEADERBOARD
"""
self.meme_leaderboard: dict[int, int] = {}
async with sqlite3.connect(self.stats_db_path, timeout=2) as db_conn:
async with sqlite3.connect(self.stats_db_path, timeout=5) as db_conn:
db_conn.row_factory = sqlite3.Row
db_query: str = "SELECT discord_uid, count FROM memes WHERE count > 0"
async with db_conn.execute(db_query) as db_cursor:
@ -255,35 +256,49 @@ class Meme(commands.Cog):
INSERT MEME -> SQLITE DB
"""
try:
try:
image.seek(0)
_image = Image.open(image)
except UnidentifiedImageError:
return None
phash: str = str(imagehash.phash(_image))
query: str = "INSERT INTO memes(discord_uid, timestamp, image, message_ids, phash) VALUES(?, ?, ?, ?, ?)"
image.seek(0)
_image = Image.open(image)
except UnidentifiedImageError:
return None
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=2) as db_conn:
insert = await db_conn.execute_insert(
query, (discord_uid, timestamp, image.read(), message_id, phash)
async with sqlite3.connect(self.memedb_path, timeout=5) as db_conn:
insert = await db_conn.execute_insert(
query, (discord_uid, timestamp, image.read(), message_id, phash)
)
if insert:
await db_conn.commit()
return True
return None
except Exception as e:
await log_to_playground(
self.bot,
f"Exception occurred while attempting to insert meme (message id: {message_id}):\n{str(e)}",
)
if insert:
await db_conn.commit()
return True
return None
async def dupe_check(self, image) -> bool | int:
"""
CHECK DB FOR DUPLICATE MEMES!
"""
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:
db_conn.row_factory = sqlite3.Row
async with await db_conn.execute(query, (phash,)) as db_cursor:
result = await db_cursor.fetchone()
if result:
return result["message_ids"]
return False
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:
db_conn.row_factory = sqlite3.Row
async with await db_conn.execute(query, (phash,)) as db_cursor:
result = await db_cursor.fetchone()
if result:
return result["message_ids"]
return False
except Exception as e:
await log_to_playground(
self.bot,
f"Exception occurred while checking image for duplicates in DB:\n{str(e)}",
)
return False
@commands.Cog.listener()
async def on_ready(self) -> None:
@ -753,7 +768,7 @@ class Meme(commands.Cog):
"""
try:
out_top: list[tuple[int, int]] = []
async with sqlite3.connect(self.stats_db_path, timeout=2) as db_conn:
async with sqlite3.connect(self.stats_db_path, timeout=5) as db_conn:
db_conn.row_factory = sqlite3.Row
query: str = "SELECT discord_uid, count FROM memes WHERE count > 0 ORDER BY count DESC"
async with db_conn.execute(query) as db_cursor: