dragon-bot/app/core_utils.py

57 lines
1.3 KiB
Python
Executable File

import requests
import discord
def download_image(url, path=None):
request = requests.get(url)
suffix_list = [
"jpeg",
"jpg",
"png",
"tif",
"svg",
]
extension = request.headers["content-type"].split("/")[1]
if not path:
path = "/tmp/image.{}".format(extension)
if extension in suffix_list:
open(path, "wb").write(requests.get(url).content)
return path
return "Invalid image format"
def generate_embed(
embed_url=None,
embed_title=None,
embed_description=None,
embed_color=None,
author_name=None,
author_image=None,
):
"""
generate_embed(embed_url=None, embed_title=None, embed_description=None, embed_color=None)
Generates a discord embed object based on the URL passed in
Optionally, you can set the title and description text for the embed object.
"""
if not embed_description and embed_url:
embed_description = "[Direct Link]({})".format(embed_url)
if not embed_color:
embed_color = discord.Color.gold()
embed = discord.Embed(
title=embed_title, description=embed_description, color=embed_color, type="rich"
)
if embed_url:
embed.set_image(url=embed_url)
if author_image or author_name:
embed.set_author(name=author_name, icon_url=author_image)
return embed