mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 09:16:19 +00:00
refactor(functionscalls.py): add aiohttp import to improve code readability and maintainability
refactor(functionscalls.py): extract get_final_url function to improve code modularity and reusability refactor(functionscalls.py): simplify send_a_stock_image function by using get_final_url function and improving message formatting
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import discord
|
||||
import aiohttp
|
||||
|
||||
functions = [
|
||||
{
|
||||
"name": "add_reaction_to_last_message",
|
||||
@@ -74,7 +76,13 @@ server_normal_channel_functions = [
|
||||
},
|
||||
]
|
||||
|
||||
unsplash_random_image_url = "https://source.unsplash.com/random/1920x1080"
|
||||
unsplash_random_image_url = "https://source.unsplash.com/random"
|
||||
|
||||
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
|
||||
|
||||
async def add_reaction_to_last_message(message_to_react_to: discord.Message, emoji, message=""):
|
||||
if message == "":
|
||||
@@ -88,11 +96,10 @@ async def reply_to_last_message(message_to_reply_to: discord.Message, message):
|
||||
|
||||
async def send_a_stock_image(message_in_channel_in_wich_to_send: discord.Message, query: str, message:str = ""):
|
||||
query = query.replace(" ", "+")
|
||||
if message == "":
|
||||
await message_in_channel_in_wich_to_send.channel.send(f"https://source.unsplash.com/random/1920x1080?{query}")
|
||||
else:
|
||||
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)
|
||||
await message_in_channel_in_wich_to_send.channel.send(f"https://source.unsplash.com/random/1920x1080?{query}")
|
||||
|
||||
async def create_a_thread(channel_in_which_to_create_the_thread: discord.TextChannel, name: str, message: str):
|
||||
msg = await channel_in_which_to_create_the_thread.send(message)
|
||||
|
||||
Reference in New Issue
Block a user