This is my code that gives and image of a random quote (used with Pillow)
import discord
from discord.ext import commands, tasks
import random
from itertools import cycle
import os
import asyncio
import json
import httpx
import requests
from PIL import Image, ImageDraw, ImageFont
import textwrap
def make_quote():
y = 50
img_number = random.randint(1, 4)
img = Image.open(f"Quote things/Backgrounds/{img_number}.png")
with open("Quote things/quotes.txt") as file:
selected = file.readlines()
single_line = random.choice(selected)
quote, author = single_line.split(" ~")
wrapped = textwrap.TextWrapper(width=29)
word_list = wrapped.wrap(text=quote)
quoteFont = ImageFont.truetype("Quote things/Font/Poppins-Bold.ttf", 45)
authorFont = ImageFont.truetype("Quote things/Font/Poppins-BoldItalic.ttf", 45)
quote_draw = ImageDraw.Draw(img)
author_draw = ImageDraw.Draw(img)
for i in word_list:
quote_draw.text((25, y), f"{i}", font=quoteFont, fill=(255, 255, 255), stroke_width=2, stroke_fill=(0, 0, 0))
y += 50
author_draw.text((25, 700), f"{author}", font=authorFont, fill=(54, 53, 50), stroke_width=2,
stroke_fill=(255, 255, 255))
img.save("Quote things/Images/Quote.png")
@client.command(name="quoteimage", description="quote image")
async def quoteimage(ctx):
make_quote()
await ctx.send(file=discord.File("Quote things/Images/Quote.png"))
It works and sends an image when i do !quoteimage.
How would i make it work with /quoteimage