From 89778cbd3b4a2772cb4a6d97c3d8dc24ab751c44 Mon Sep 17 00:00:00 2001 From: Paillat Date: Tue, 18 Jul 2023 23:18:47 +0200 Subject: [PATCH] fix(requirements.txt): add bs4 package to requirements.txt for BeautifulSoup usage feat(functionscalls.py): add new functions send_ascii_art_text and send_ascii_art_image to send messages and images in ascii art fix(makeprompt.py): import send_ascii_art_text and send_ascii_art_image functions and add corresponding if statements to handle function calls --- requirements.txt | 3 +- src/functionscalls.py | 83 ++++++++++++++++++++++++++++++++++++++++--- src/makeprompt.py | 13 +++++++ 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 58f0bc3..29b1b8a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ emoji # Google api google-api-python-client google-cloud-vision -tiktoken \ No newline at end of file +tiktoken +bs4 \ No newline at end of file diff --git a/src/functionscalls.py b/src/functionscalls.py index 3a02fe2..bff964b 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -1,9 +1,11 @@ import discord import aiohttp import random - +import time +from bs4 import BeautifulSoup from src.config import tenor_api_key - +randomseed = time.time() +random.seed(randomseed) tenor_api_url = f"https://tenor.googleapis.com/v2/search?key={tenor_api_key}&q=" functions = [ @@ -73,6 +75,46 @@ functions = [ "required": ["query"], }, }, + { + "name": "send_ascii_art_text", + "description": "Sendsa message in huge ascii art text.", + "parameters": { + "type": "object", + "properties": { + "text": { + "type": "string", + "description": "The text to send as ascii art", + }, + "font": { + "type": "string", + "description": "The font to use, between 'standard'(default), 'shadow', 'money' (made out of $), 'bloody', 'dos-rebel'(like in the matrix, a really cool one). Remember to use this with not too long text (max 5 characters), otherwise it will look weird. To send multiple max 5 length word, add line breaks between them.", + }, + "message": { + "type": "string", + "description": "Your message to send with the ascii art. It will not be converted to ascii art, just sent as a normal message.", + } + }, + "required": ["text"], + }, + }, + { + "name": "send_ascii_art_image", + "description": "Sends an image in ascii art.", + "parameters": { + "type": "object", + "properties": { + "query": { + "type": "string", + "description": "The query to search for, words separated by spaces, max 2 words", + }, + "message": { + "type": "string", + "description": "Your message to send with the image", + } + }, + "required": ["query"], + }, + }, ] server_normal_channel_functions = [ @@ -93,6 +135,14 @@ server_normal_channel_functions = [ }, ] +font_matches = { + "standard": "ANSI Regular", + "shadow": "ANSI Shadow", + "money": random.choice(["Big Money-ne", "Big Money-nw", "Big Money-se", "Big Money-sw"]), + "bloody": "Bloody", + "dos-rebel": "DOS Rebel", +} + unsplash_random_image_url = "https://source.unsplash.com/random" @@ -102,10 +152,14 @@ async def get_final_url(url): final_url = str(response.url) return final_url -async def do_async_request(url): +async def do_async_request(url, json=True): async with aiohttp.ClientSession() as session: async with session.get(url) as response: - response = await response.json() + + if json: + response = await response.json() + else: + response = await response.text() return response async def add_reaction_to_last_message( @@ -145,6 +199,25 @@ async def send_a_gif(message_in_channel_in_wich_to_send: discord.Message, query: response = await do_async_request(image_url) json = response print(json) - gif_url = random.choice(json["results"])["itemurl"] + gif_url = random.choice(json["results"])["itemurl"] # type: ignore message = message + "\n" + gif_url + await message_in_channel_in_wich_to_send.channel.send(message) + +async def send_ascii_art_text(message_in_channel_in_wich_to_send: discord.Message, text: str, font: str = "standard", message: str = ""): + font = font_matches[font] + text = text.replace(" ", "+") + asciiiar_url = f"https://asciified.thelicato.io/api?text={text}&font={font}" + response = await do_async_request(asciiiar_url, json=False) + message = f"```\n{response}```\n{message}" + await message_in_channel_in_wich_to_send.channel.send(message) + +async def send_ascii_art_image(message_in_channel_in_wich_to_send: discord.Message, query: str, message: str = ""): + query = query.replace(" ", "-") + asciiiar_url = f"https://emojicombos.com/{query}" + response = await do_async_request(asciiiar_url, json=False) + soup = BeautifulSoup(response, "html.parser") + combos = soup.find_all("div", class_=lambda x: x and "combo-ctn" in x and "hidden" not in x)[:5] # type: ignore + combos = [combo["data-combo"] for combo in combos if len(combo["data-combo"]) <= 2000] + combo = random.choice(combos) + message = f"```\n{combo}```\n{message}" await message_in_channel_in_wich_to_send.channel.send(message) \ No newline at end of file diff --git a/src/makeprompt.py b/src/makeprompt.py index 79f51fc..55f81e4 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -13,6 +13,8 @@ from src.functionscalls import ( send_a_stock_image, create_a_thread, send_a_gif, + send_ascii_art_text, + send_ascii_art_image, functions, server_normal_channel_functions, ) @@ -151,6 +153,17 @@ async def chatgpt_process( reply = arguments.get("message", "") limit = arguments.get("limit", 15) await send_a_gif(message, query, reply, limit) + if name == "send_ascii_art_text": + if arguments.get("text"): + text = arguments.get("text") + font = arguments.get("font", "standard") + reply = arguments.get("message", "") + await send_ascii_art_text(message, text, font, reply) + if name == "send_ascii_art_image": + if arguments.get("query"): + query = arguments.get("query") + reply = arguments.get("message", "") + await send_ascii_art_image(message, query, reply) if name == "": await message.channel.send( "The function call is empty. Please retry.", delete_after=10