This commit is contained in:
Auric Vente 2024-04-20 12:51:55 -06:00
parent cf387a07b8
commit fac74c883a
1 changed files with 79 additions and 3 deletions

82
main.py
View File

@ -13,6 +13,7 @@ from datetime import datetime, timedelta
from pathlib import Path from pathlib import Path
import aiofiles import aiofiles
import sys import sys
import tempfile
HERE = Path(__file__).parent HERE = Path(__file__).parent
username = os.environ.get("GLUEBOT_USERNAME") username = os.environ.get("GLUEBOT_USERNAME")
@ -30,6 +31,8 @@ prefix = ","
token = None token = None
session = None session = None
delay = 3 delay = 3
last_file = None
last_file_ext = None
gifmaker_common = [ gifmaker_common = [
"gifmaker", "gifmaker",
@ -209,15 +212,37 @@ async def run():
async def on_message(ws, message): async def on_message(ws, message):
if blocked(): global last_file, last_file_ext
return
try: try:
data = json.loads(message) data = json.loads(message)
except BaseException: except BaseException:
return return
if data["type"] in ["message", "messageEnd"]: if data["type"] == "files":
dta = data.get("data")
if not dta:
return
files = dta.get("files")
if not files:
return
first = files[0]
name = first.get("name")
ext = first.get("extension")
if (not name) or (not ext):
return
last_file = f"https://deek.chat/storage/files/{name}"
last_file_ext = ext
elif data["type"] in ["message", "messageEnd"]:
if blocked():
return
if data["data"]["name"] == username: if data["data"]["name"] == username:
return return
@ -299,6 +324,57 @@ async def on_message(ws, message):
update_time() update_time()
await shitpost(ws, room_id) await shitpost(ws, room_id)
elif cmd in ["write", "text", "meme"]:
if len(args) > 0:
update_time()
arg = " ".join(clean_list(args))
arg = clean_gifmaker(arg)
await make_meme(ws, arg, room_id)
async def make_meme(ws, arg, room_id):
if not last_file:
return
try:
url = last_file
await send_message(ws, "Generating gif...", room_id)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
with tempfile.NamedTemporaryFile(delete=False, suffix=last_file_ext) as temp_file:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
temp_file.write(chunk)
file_name = temp_file.name
command = gifmaker_command([
"--input", file_name,
"--words", arg,
"--filter", "anyhue2",
"--opacity", 0.8,
"--fontsize", 60,
"--delay", 700,
"--padding", 30,
"--fontcolor", "light2",
"--bgcolor", "black",
"--bottom", 30,
"--font", "nova",
"--frames", 3,
"--fillgen",
])
await run_gifmaker(command, room_id)
os.remove(file_name)
except Exception as e:
print("Error:", e)
return None
async def random_bird(ws, room_id): async def random_bird(ws, room_id):
birdfile = get_path("data/aves.txt") birdfile = get_path("data/aves.txt")