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()

View File

@ -214,8 +214,51 @@ class Misc(commands.Cog):
except Exception as e:
logging.debug("Failed to add xmas reaction: %s", str(e))
await ctx.respond(
f"Only {days} days, {hours} hours, {minutes} minutes,\
{seconds} seconds and {ms} ms left! (UTC)".translate(xmas_trans)
f"Only {days} days, {hours} hours, {minutes} minutes, {seconds} seconds and {ms} ms left until Christmas! (UTC)".translate(
xmas_trans
)
)
except Exception as e:
traceback.print_exc()
return await ctx.respond(f"Error: {str(e)}")
@bridge.bridge_command()
async def halloween(self, ctx) -> None:
"""
Countdown til Halloween!
"""
try:
emojis: dict = {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
}
with ctx.channel.typing():
countdown = self.util.get_days_to_halloween()
if not isinstance(countdown, tuple) or len(countdown) < 6:
return await ctx.respond(
"Oops, Halloween is cancelled."
) # Invalid countdown from util
(days, hours, minutes, seconds, ms, _) = countdown
now: datetime.datetime = datetime.datetime.now()
if now.month == 12 and now.day == 25:
return await ctx.respond("# IT IS HALLOWEEN!!!!!!!!\n-# GET ROWDY")
halloween_trans: dict = str.maketrans(emojis)
try:
await ctx.message.add_reaction(emoji="🎃")
except Exception as e:
logging.debug("Failed to add Halloween reaction: %s", str(e))
await ctx.respond(
f"Only {days} days, {hours} hours, {minutes} minutes, {seconds} seconds and {ms} ms left until Halloween! (UTC)".translate(
halloween_trans
)
)
except Exception as e:
traceback.print_exc()

View File

@ -107,9 +107,7 @@ class Radio(commands.Cog):
but they exist.
"""
logging.info("Detected VC not playing... playing!")
source: discord.FFmpegAudio = discord.FFmpegOpusAudio(
self.STREAM_URL
)
source: discord.FFmpegAudio = discord.FFmpegOpusAudio(self.STREAM_URL)
vc.play( # type: ignore
source,
after=lambda e: logging.info("Error: %s", e) if e else None,

View File

@ -59,10 +59,9 @@ class Sing(commands.Cog):
return await ctx.respond(
"**Error**: No song specified, no activity found to read."
)
await ctx.respond(
"*Searching...*",
ephemeral=True
"*Searching...*", ephemeral=True
) # Must respond to interactions within 3 seconds, per Discord
parsed = self.utility.parse_song_input(song, activity)