changed file to cog
changed print statements to logger
This commit is contained in:
parent
43b1dc5ae7
commit
d395d8cd3a
1 changed files with 93 additions and 95 deletions
|
|
@ -3,10 +3,15 @@ import os
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import discord
|
||||||
from discord.ext import commands
|
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 = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
|
||||||
|
|
@ -20,11 +25,9 @@ async def answer_question(topic, model="gpt-3.5-turbo"):
|
||||||
url = "https://api.openai.com/v1/chat/completions"
|
url = "https://api.openai.com/v1/chat/completions"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
http_session = aiohttp.ClientSession()
|
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
||||||
async with http_session.post(url, headers=headers, json=data) as resp:
|
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
response = response_data['choices'][0]['message']['content']
|
response = response_data['choices'][0]['message']['content']
|
||||||
await http_session.close()
|
|
||||||
return response
|
return response
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
return "error occurred in meme"
|
return "error occurred in meme"
|
||||||
|
|
@ -34,10 +37,9 @@ async def answer_question(topic, model="gpt-3.5-turbo"):
|
||||||
help="Generates a meme based on input. Usage: !meme (topic)",
|
help="Generates a meme based on input. Usage: !meme (topic)",
|
||||||
brief="Generate a meme"
|
brief="Generate a meme"
|
||||||
)
|
)
|
||||||
async def meme(ctx):
|
async def meme(self, ctx):
|
||||||
async def generate_random_meme(topic):
|
async def generate_random_meme(topic):
|
||||||
http_session = aiohttp.ClientSession()
|
async with self.bot.http_session.get('https://api.imgflip.com/get_memes') as resp:
|
||||||
async with http_session.get('https://api.imgflip.com/get_memes') as resp:
|
|
||||||
response_data = await resp.json()
|
response_data = await resp.json()
|
||||||
response = response_data['data']['memes']
|
response = response_data['data']['memes']
|
||||||
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
|
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)
|
memenumber = random.randint(1,99)
|
||||||
meme_name = response[memenumber-1]['name']
|
meme_name = response[memenumber-1]['name']
|
||||||
panel_count = response[memenumber-1]['box_count']
|
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]")
|
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
|
id = memenumber
|
||||||
|
|
@ -74,17 +75,15 @@ async def meme(ctx):
|
||||||
URL = 'https://api.imgflip.com/caption_image'
|
URL = 'https://api.imgflip.com/caption_image'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
#http_session = aiohttp.ClientSession()
|
async with self.bot.http_session.post(URL, params=params) as resp:
|
||||||
async with http_session.post(URL, params=params) as resp:
|
|
||||||
response = await resp.json()
|
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']
|
image_link = response['data']['url']
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print("Error occurred in meme")
|
self.bot.logger.exception("Error occurred in meme")
|
||||||
try:
|
try:
|
||||||
#------------------------------------Saving Image Using Aiohttp---------------------------------#
|
#------------------------------------Saving Image Using Aiohttp---------------------------------#
|
||||||
filename = memepics[id-1]['name']
|
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/"
|
folder = "tmp/meme/"
|
||||||
filename = folder + topic + str(len(os.listdir(folder))) + ".jpg"
|
filename = folder + topic + str(len(os.listdir(folder))) + ".jpg"
|
||||||
|
|
||||||
|
|
@ -94,9 +93,8 @@ async def meme(ctx):
|
||||||
if not chunk:
|
if not chunk:
|
||||||
break
|
break
|
||||||
file.write(chunk)
|
file.write(chunk)
|
||||||
except Exception as error:
|
except:
|
||||||
print("Something's Wrong with the aiohttp in meme So try again")
|
self.bot.logger.exception("Something's Wrong with the aiohttp in meme So try again")
|
||||||
await http_session.close()
|
|
||||||
return image_link, filename
|
return image_link, filename
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -105,8 +103,8 @@ async def meme(ctx):
|
||||||
link, filepath = await generate_random_meme(topic)
|
link, filepath = await generate_random_meme(topic)
|
||||||
await ctx.send(link)
|
await ctx.send(link)
|
||||||
except Exception as error:
|
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)')
|
await ctx.send('Something went wrong try again. Usage: !meme (topic)')
|
||||||
|
|
||||||
async def setup(bot):
|
async def setup(bot):
|
||||||
bot.add_command(meme)
|
await bot.add_cog(Meme(bot))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue