changed file to cog

changed print statements to logger
This commit is contained in:
phixxy 2024-01-27 21:38:34 -08:00
parent 43b1dc5ae7
commit d395d8cd3a

View file

@ -3,10 +3,15 @@ import os
import random
import time
import aiohttp
import discord
from discord.ext import commands
async def answer_question(topic, model="gpt-3.5-turbo"):
class Meme(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def answer_question(self, topic, model="gpt-3.5-turbo"):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
@ -20,24 +25,21 @@ async def answer_question(topic, model="gpt-3.5-turbo"):
url = "https://api.openai.com/v1/chat/completions"
try:
http_session = aiohttp.ClientSession()
async with http_session.post(url, headers=headers, json=data) as resp:
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
response_data = await resp.json()
response = response_data['choices'][0]['message']['content']
await http_session.close()
return response
except Exception as error:
return "error occurred in meme"
@commands.command(
@commands.command(
description="Meme",
help="Generates a meme based on input. Usage: !meme (topic)",
brief="Generate a meme"
)
async def meme(ctx):
async def meme(self, ctx):
async def generate_random_meme(topic):
http_session = aiohttp.ClientSession()
async with http_session.get('https://api.imgflip.com/get_memes') as resp:
async with self.bot.http_session.get('https://api.imgflip.com/get_memes') as resp:
response_data = await resp.json()
response = response_data['data']['memes']
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
@ -46,7 +48,6 @@ async def meme(ctx):
memenumber = random.randint(1,99)
meme_name = response[memenumber-1]['name']
panel_count = response[memenumber-1]['box_count']
print("panel_count ",panel_count)
panel_text = await answer_question("Create text for a meme. The meme is " + meme_name + ". It has " + str(panel_count) + " panels. Only create one meme. Do not use emojis or hashtags! Use the topic: " + topic + ". Use the output format (DO NOT USE EXTRA NEWLINES AND DO NOT DESCRIBE THE PICTURE IN YOUR OUTPUT): \n1: [panel 1 text]\n2: [panel 2 text]")
id = memenumber
@ -74,17 +75,15 @@ async def meme(ctx):
URL = 'https://api.imgflip.com/caption_image'
try:
#http_session = aiohttp.ClientSession()
async with http_session.post(URL, params=params) as resp:
async with self.bot.http_session.post(URL, params=params) as resp:
response = await resp.json()
print(f"Generated Meme = {response['success']}\nImage Link = {response['data']['url']}\nPage Link = {response['data']['page_url']}")
image_link = response['data']['url']
except Exception as error:
print("Error occurred in meme")
self.bot.logger.exception("Error occurred in meme")
try:
#------------------------------------Saving Image Using Aiohttp---------------------------------#
filename = memepics[id-1]['name']
async with http_session.get(image_link) as response:
async with self.bot.http_session.get(image_link) as response:
folder = "tmp/meme/"
filename = folder + topic + str(len(os.listdir(folder))) + ".jpg"
@ -94,9 +93,8 @@ async def meme(ctx):
if not chunk:
break
file.write(chunk)
except Exception as error:
print("Something's Wrong with the aiohttp in meme So try again")
await http_session.close()
except:
self.bot.logger.exception("Something's Wrong with the aiohttp in meme So try again")
return image_link, filename
try:
@ -105,8 +103,8 @@ async def meme(ctx):
link, filepath = await generate_random_meme(topic)
await ctx.send(link)
except Exception as error:
print("Error occurred in meme")
self.bot.logger.exception("Error occurred in meme")
await ctx.send('Something went wrong try again. Usage: !meme (topic)')
async def setup(bot):
bot.add_command(meme)
await bot.add_cog(Meme(bot))