2023-07-15 12:20:38 +02:00
import discord
2023-07-17 18:42:45 +02:00
import aiohttp
2023-07-18 21:53:07 +02:00
import random
from src . config import tenor_api_key
tenor_api_url = f " https://tenor.googleapis.com/v2/search?key= { tenor_api_key } &q= "
2023-07-17 18:42:45 +02:00
2023-07-16 17:11:24 +02:00
functions = [
{
" name " : " add_reaction_to_last_message " ,
" description " : " React to the last message sent by the user with an emoji. " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" emoji " : {
" type " : " string " ,
2023-07-18 17:51:13 +02:00
" description " : " an emoji to react with, only one emoji is supported " ,
2023-07-16 17:11:24 +02:00
} ,
2023-07-18 17:51:13 +02:00
" message " : { " type " : " string " , " description " : " Your message " } ,
2023-07-16 17:11:24 +02:00
} ,
2023-07-18 17:51:13 +02:00
" required " : [ " emoji " ] ,
} ,
2023-07-16 17:11:24 +02:00
} ,
{
" name " : " reply_to_last_message " ,
" description " : " Reply to the last message sent by the user. " ,
" parameters " : {
" type " : " object " ,
" properties " : {
2023-07-18 17:51:13 +02:00
" message " : { " type " : " string " , " description " : " Your message " }
2023-07-16 17:11:24 +02:00
} ,
2023-07-18 17:51:13 +02:00
" required " : [ " message " ] ,
} ,
2023-07-16 17:11:24 +02:00
} ,
{
" name " : " send_a_stock_image " ,
" description " : " Send a stock image in the channel. " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" query " : {
" type " : " string " ,
2023-07-18 17:51:13 +02:00
" description " : " The query to search for, words separated by spaces " ,
2023-07-16 17:11:24 +02:00
} ,
" message " : {
" type " : " string " ,
2023-07-18 17:51:13 +02:00
" description " : " Your message to send with the image " ,
} ,
2023-07-16 17:11:24 +02:00
} ,
2023-07-18 17:51:13 +02:00
" required " : [ " query " ] ,
} ,
} ,
2023-07-18 21:53:07 +02:00
{
" name " : " send_a_gif " ,
" description " : " Send a gif in the channel. " ,
" parameters " : {
" type " : " object " ,
" properties " : {
" query " : {
" type " : " string " ,
" description " : " The query to search for, words separated by spaces " ,
} ,
" message " : {
" type " : " string " ,
" description " : " Your message to send with the gif " ,
} ,
" limit " : {
" type " : " integer " ,
" description " : " The number of gifs to search for, a random one will be chosen. If the gif is a really specific one, you might want to have a lower limit, to avoid sending a gif that doesn ' t match your query. " ,
} ,
} ,
" required " : [ " query " ] ,
} ,
} ,
2023-07-16 17:11:24 +02:00
]
server_normal_channel_functions = [
{
" name " : " create_a_thread " ,
" description " : " Create a thread in the channel. Use this if you see a long discussion coming. " ,
" parameters " : {
" type " : " object " ,
" properties " : {
2023-07-18 17:51:13 +02:00
" name " : { " type " : " string " , " description " : " The name of the thread " } ,
2023-07-16 17:11:24 +02:00
" message " : {
" type " : " string " ,
2023-07-18 17:51:13 +02:00
" description " : " Your message to send with the thread " ,
} ,
2023-07-16 17:11:24 +02:00
} ,
2023-07-18 17:51:13 +02:00
" required " : [ " name " , " message " ] ,
} ,
2023-07-16 17:11:24 +02:00
} ,
]
2023-07-16 21:18:30 +02:00
2023-07-17 18:42:45 +02:00
unsplash_random_image_url = " https://source.unsplash.com/random "
2023-07-18 17:51:13 +02:00
2023-07-17 18:42:45 +02:00
async def get_final_url ( url ) :
async with aiohttp . ClientSession ( ) as session :
async with session . head ( url , allow_redirects = True ) as response :
final_url = str ( response . url )
return final_url
2023-07-16 21:18:30 +02:00
2023-07-18 21:53:07 +02:00
async def do_async_request ( url ) :
async with aiohttp . ClientSession ( ) as session :
async with session . get ( url ) as response :
response = await response . json ( )
return response
2023-07-18 17:51:13 +02:00
async def add_reaction_to_last_message (
message_to_react_to : discord . Message , emoji , message = " "
) :
2023-07-15 12:20:38 +02:00
if message == " " :
await message_to_react_to . add_reaction ( emoji )
else :
await message_to_react_to . channel . send ( message )
await message_to_react_to . add_reaction ( emoji )
2023-07-18 17:51:13 +02:00
2023-07-15 12:20:38 +02:00
async def reply_to_last_message ( message_to_reply_to : discord . Message , message ) :
await message_to_reply_to . reply ( message )
2023-07-18 17:51:13 +02:00
async def send_a_stock_image (
message_in_channel_in_wich_to_send : discord . Message , query : str , message : str = " "
) :
2023-07-15 12:20:38 +02:00
query = query . replace ( " " , " + " )
2023-07-17 18:42:45 +02:00
image_url = f " { unsplash_random_image_url } ? { query } "
final_url = await get_final_url ( image_url )
message = message + " \n " + final_url
await message_in_channel_in_wich_to_send . channel . send ( message )
2023-07-16 17:11:24 +02:00
2023-07-18 17:51:13 +02:00
async def create_a_thread (
channel_in_which_to_create_the_thread : discord . TextChannel , name : str , message : str
) :
2023-07-16 17:11:24 +02:00
msg = await channel_in_which_to_create_the_thread . send ( message )
2023-07-18 17:51:13 +02:00
await msg . create_thread ( name = name )
2023-07-18 21:53:07 +02:00
async def send_a_gif ( message_in_channel_in_wich_to_send : discord . Message , query : str , message : str = " " , limit : int = 15 ) :
query = query . replace ( " " , " + " )
image_url = f " { tenor_api_url } { query } &limit= { limit } "
print ( image_url )
response = await do_async_request ( image_url )
json = response
print ( json )
gif_url = random . choice ( json [ " results " ] ) [ " itemurl " ]
message = message + " \n " + gif_url
await message_in_channel_in_wich_to_send . channel . send ( message )