Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- import os
- import logging
- import io
- import aiofiles
- from PIL import Image
- logger = logging.getLogger("discord")
- logger.setLevel(logging.ERROR)
- handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
- handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
- logger.addHandler(handler)
- class Fm(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
- self.card_tags = {
- "panzer_iii_a_veteran": ["Panzer III A"],
- "panzer_iii_a_noveteran": ["Panzer III A"],
- "panzer_iii_b": ["Panzer III B"],
- }
- def find_cards_by_name(self, card_name):
- matched_cards = []
- try:
- card_name_lower = card_name.lower()
- for key, tags in self.card_tags.items():
- for tag in tags:
- if tag.lower() in card_name_lower:
- matched_cards.append(key)
- break
- except Exception as e:
- logger.exception(f"Error in 'find_cards_by_name': {e}")
- return matched_cards
- async def send_images(self, ctx, images_to_send):
- for image_data in images_to_send:
- await ctx.send(file=discord.File(image_data))
- @commands.command()
- async def fm(self, ctx, *, card_name: str):
- try:
- card_name_lower = card_name.lower()
- card_keys = self.find_cards_by_name(card_name_lower)
- if card_keys:
- images_folder = os.path.join(os.path.dirname(__file__), "fm_cards")
- images_to_send = []
- card_tags_list = []
- for card_key in card_keys:
- card_tags = self.card_tags.get(card_key)
- file_name = f"{card_key}.png"
- file_path = os.path.join(images_folder, file_name)
- try:
- async with aiofiles.open(file_path, "rb") as file:
- # Resize/compress the image if needed using Pillow (PIL) library
- img = Image.open(file)
- img.thumbnail((800, 800))
- img_byte_array = io.BytesIO()
- img.save(img_byte_array, format="PNG")
- images_to_send.append(img_byte_array.getvalue())
- card_tags_list.append(', '.join(card_tags))
- except FileNotFoundError:
- await ctx.send(f"Card image not found for {', '.join(card_tags)}")
- if images_to_send:
- await ctx.send(f"Cards of {', '.join(card_tags_list)}.")
- await self.send_images(ctx, images_to_send)
- else:
- await ctx.send("Card not found. Please enter a valid card name.")
- except Exception as e:
- await ctx.send(f"An error occurred while executing the command: {e}")
- async def setup(bot):
- await bot.add_cog(Fm(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement