Skip to content
Snippets Groups Projects
Commit 8d8e7e0e authored by Josh's avatar Josh
Browse files

Created gem & image search cogs, left to do: video,web. and embed generator.

parent ad65dc59
No related branches found
No related tags found
No related merge requests found
File added
import discord
from discord.ext import commands
import asyncio
import configparser
import requests
import logging
config = configparser.ConfigParser()
config.read('config.ini')
GEMINI_API_KEY = config['GOOGLE']['GEMINI_API_KEY']
class GemCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command(name="gem", description="Ask a question to Gemini Pro")
async def ask_gemini_slash(self, ctx, question: str):
try:
headers = {'Authorization': f'Bearer {GEMINI_API_KEY}'}
response = requests.post(
'https://api.gemini.com/v1/completions',
headers=headers,
json={'prompt': question}
)
response.raise_for_status()
data = response.json()
if 'choices' in data and len(data['choices']) > 0:
answer = data['choices'][0]['text']
embed = discord.Embed(
title="Gem says:", description=answer)
await ctx.send(embed=embed)
else:
logging.error(
"The API response format for Gemini Pro has changed, Unable to extract response.")
await ctx.send("Unexpected response from gem <:SatisfiedCheems:1230175084912840734>")
except requests.exceptions.RequestException as e:
logging.error(f"404 from gem: {e}")
await ctx.send("An error returned while trying to communicate with Gemini Pro, Try again.")
except Exception as e:
logging.error(f"Random error in 'gem' command: {e}")
await ctx.send("Gem is not working at the moment.")
async def setup(bot):
await bot.add_cog(GemCog(bot))
from discord.ext import commands
from google_images_search import GoogleImagesSearch
import logging
import asyncio
config = configparser.ConfigParser()
config.read('config.ini')
GCS_DEVELOPER_KEY = config['GOOGLE']['GCS_DEVELOPER_KEY']
GCS_CX = config['GOOGLE']['GCS_CX']
class ImageCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.gis = GoogleImagesSearch(GCS_DEVELOPER_KEY, GCS_CX)
@commands.hybrid_command(name='image', description="Search for images from Google.")
async def search_image(self, ctx, *, query):
try:
_search_params = {
'q': query,
'num': 5,
'safe': 'off',
}
self.gis.async_search(search_params=_search_params)
if not self.gis.results():
await ctx.send("No images found for your search.")
return
current_page = 0
total_pages = (len(self.gis.results()) - 1) // 5
def create_embed():
embed = discord.Embed(title=f"Image search Results for '{
query}' ({current_page+1}/{total_pages+1})")
embed.set_image(url=self.gis.results()[current_page].url)
return embed
message = await ctx.send(embed=embed)
await message.add_reaction('⬅️')
await message.add_reaction('➡️')
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️'] and reaction.message == message
while True:
try:
reaction, _ = await self.bot.wait_for('reaction_add', timeout=60.0, check=check)
if str(reaction.emoji) == '⬅️' and current_page > 0:
current_page -= 1
elif str(reaction.emoji) == '➡️' and current_page < total_pages:
current_page += 1
embed.set_image(url=self.gis.results()
[current_page * 5].url)
embed.title = f"Results for '{
query}' ({current_page+1}/{total_pages+1})"
await message.edit(embed=embed)
await message.remove_reaction(reaction, ctx.author)
except asyncio.TimeoutError:
await message.clear_reactions()
break
except Exception as e:
logging.error(f"Error during image search: {e}")
await ctx.send("An error occured while searching for '{query}'.")
async def setup(bot):
await bot.add_cog(ImageCog(bot))
import discord
from discord.ext import commands
from google_images_search import GoogleImagesSearch
import logging
import asyncio
config = configparser.ConfigParser()
config.read('config.ini')
GCS_DEVELOPER_KEY = config['GOOGLE']['GCS_DEVELOPER_KEY']
GCS_CX = config['GOOGLE']['GCS_CX']
class ImageCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.gis = GoogleImagesSearch(GCS_DEVELOPER_KEY, GCS_CX)
@commands.hybrid_command(name='image', description="Search for images from Google.")
async def search_image(self, ctx, *, query):
try:
_search_params = {
'q': query,
'num': 5,
'safe': 'off',
}
self.gis.async_search(search_params=_search_params)
if not self.gis.results():
await ctx.send("No images found for your search.")
return
current_page = 0
total_pages = (len(self.gis.results()) - 1) // 5
def create_embed():
embed = discord.Embed(title=f"Image search Results for '{query}' ({current_page+1}/{total_pages+1})")
embed.set_image(url=self.gis.results()[current_page].url)
return embed
message = await ctx.send(embed=embed)
await message.add_reaction('⬅️')
await message.add_reaction('➡️')
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️'] and
while True:
try:
reaction, _ = await self.bot.wait_for('reaction_add', timeout=60.0, check=check)
if str(reaction.emoji) == '⬅️' and current_page > 0:
current_page -= 1
elif str(reaction.emoji) == '➡️' and current_page < total_pages:
current_page += 1
await message.edit(embed=create_embed())
await message.remove_reaction(reaction, ctx.author)
except asyncio.TimeoutError:
await message.clear_reactions()
break
except Exception as e:
logging.error(f"Error during image search: {e}")
await ctx.send("An error occured while searching for '{query}'.")
async def setup(bot):
await bot.add_cog(ImageCog(bot))
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
<h1>Cogs to add</h1> <h1>Cogs to add</h1>
</div> <br> </div> <br>
- [ ] Gem - [ x ] Gem
- [ ] websearch - [ ] websearch
- [ ] imagesearch - [ x ] imagesearch
- [ ] videosearch - [ ] videosearch
- [ ] embedcreato - [ ] embedcreat
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment