diff --git a/README.md b/README.md index f64993f..d6c1952 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Quote This Message An alternative to discord bots like Make it A Quote. +The version used on the hosted-by-me bot is slightly different than this, same functionality though. ## Requirements - Inconsolata (located at ~/.local/share/fonts/Inconsolata-VariableFont_wdth,wght.ttf) diff --git a/main.py b/main.py index 3d2c794..6a822d6 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ import discord from discord.ext import commands -from PIL import Image, ImageDraw, ImageFont, ImageFilter -from urllib.request import urlopen +from PIL import Image, ImageDraw, ImageFont import dotenv import os import io @@ -26,16 +25,22 @@ async def _quote_msg(message: discord.Message): quote = message.content author = message.author.display_name username = message.author.name - avatar = Image.open(io.BytesIO(await message.author.avatar.read())) - - avatar.resize((1000,1000)) - + try: + avatar = Image.open(io.BytesIO(await message.author.avatar.read())) + avatar.resize((1000,1000)) + except Exception: + avatar = Image.new("RGBA", (0,0)) image = Image.new("RGBA", (1890, 1063), (0,0,0)) draw = ImageDraw.Draw(image) + # todo: wrap on longer text strings draw.text((90, 1063/2-150/2), quote, font=quote_font) + + # todo: make quote smaller to fit author text in if text is larger? draw.multiline_text((90, 1063/2+250/2), f"- {author}\n({username})", font=author_font) + credit = "Made with QuoteThisMessage: aesth.lol/quote" + # todo: see above todo draw.text((1860-draw.textlength(credit, font=credit_font), 1063-50), credit, font=credit_font) image.paste(avatar) @@ -43,6 +48,10 @@ async def _quote_msg(message: discord.Message): image.save(file, format="PNG") file.seek(0) + # clean up + image.close() + avatar.close() + return file @bot.tree.context_menu(name="Quote this message") @@ -57,7 +66,7 @@ async def on_message(message: discord.Message): if bot.user in message.mentions: if message.reference and message.type == discord.MessageType.reply: if not message.reference.resolved: - await message.reply("The message you replied to is empty...") + await message.reply("Discord error. This isn't a bug with the bot!") return msg = await message.reply(content="Generating...") file = await _quote_msg(message.reference.resolved) @@ -68,4 +77,6 @@ async def on_message(message: discord.Message): await message.reply("Oops! You didn't reply to a message when you pinged the bot...") token = os.environ.get("DISCORD_TOKEN") +print("Quote This Message: aesth.lol/quote") + bot.run(token) \ No newline at end of file