Advertisement
Frostyy22

brejax help me pls

Aug 5th, 2023
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import os
  4. import logging
  5. import io
  6. import aiofiles
  7. from PIL import Image
  8.  
  9. logger = logging.getLogger("discord")
  10. logger.setLevel(logging.ERROR)
  11. handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
  12. handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
  13. logger.addHandler(handler)
  14.  
  15. class Fm(commands.Cog):
  16.     def __init__(self, bot):
  17.         self.bot = bot
  18.         self.card_tags = {
  19.             "panzer_iii_a_veteran": ["Panzer III A"],
  20.             "panzer_iii_a_noveteran": ["Panzer III A"],
  21.             "panzer_iii_b": ["Panzer III B"],
  22.         }
  23.  
  24.     def find_cards_by_name(self, card_name):
  25.         matched_cards = []
  26.         try:
  27.             card_name_lower = card_name.lower()
  28.             for key, tags in self.card_tags.items():
  29.                 for tag in tags:
  30.                     if tag.lower() in card_name_lower:  
  31.                         matched_cards.append(key)
  32.                         break  
  33.         except Exception as e:
  34.             logger.exception(f"Error in 'find_cards_by_name': {e}")
  35.         return matched_cards
  36.  
  37.     async def send_images(self, ctx, images_to_send):
  38.         for image_data in images_to_send:
  39.             await ctx.send(file=discord.File(image_data))
  40.  
  41.     @commands.command()
  42.     async def fm(self, ctx, *, card_name: str):
  43.         try:
  44.             card_name_lower = card_name.lower()
  45.             card_keys = self.find_cards_by_name(card_name_lower)
  46.             if card_keys:
  47.                 images_folder = os.path.join(os.path.dirname(__file__), "fm_cards")
  48.                 images_to_send = []
  49.                 card_tags_list = []
  50.                 for card_key in card_keys:
  51.                     card_tags = self.card_tags.get(card_key)
  52.                     file_name = f"{card_key}.png"
  53.                     file_path = os.path.join(images_folder, file_name)
  54.                     try:
  55.                         async with aiofiles.open(file_path, "rb") as file:
  56.                             # Resize/compress the image if needed using Pillow (PIL) library
  57.                             img = Image.open(file)
  58.                             img.thumbnail((800, 800))
  59.                             img_byte_array = io.BytesIO()
  60.                             img.save(img_byte_array, format="PNG")
  61.                             images_to_send.append(img_byte_array.getvalue())
  62.                             card_tags_list.append(', '.join(card_tags))  
  63.  
  64.                     except FileNotFoundError:
  65.                         await ctx.send(f"Card image not found for {', '.join(card_tags)}")
  66.  
  67.                 if images_to_send:
  68.                     await ctx.send(f"Cards of {', '.join(card_tags_list)}.")
  69.                     await self.send_images(ctx, images_to_send)
  70.             else:
  71.                 await ctx.send("Card not found. Please enter a valid card name.")
  72.         except Exception as e:
  73.             await ctx.send(f"An error occurred while executing the command: {e}")
  74.  
  75. async def setup(bot):
  76.     await bot.add_cog(Fm(bot))
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement