Guest User

Untitled

a guest
Dec 31st, 2022
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import disnake
  2. from disnake.ext import commands
  3. import aiohttp
  4. import json
  5. from dotenv import load_dotenv
  6. import os
  7.  
  8. load_dotenv()
  9. catapi = os.environ['catapi']
  10. dogapi = os.environ['dogapi']
  11.  
  12. class animals(commands.Cog):
  13.  
  14. def __init__(self, bot: commands.InteractionBot):
  15. self.bot = bot
  16.  
  17. @commands.slash_command()
  18. async def animals(self, inter):
  19. """Displays an animal photo."""
  20. animalList = ['Cats', 'Dogs', 'Ducks', 'Foxes']
  21. await inter.response.send_message("Select an animal.", components = disnake.ui.StringSelect(options = animalList))
  22.  
  23. @commands.Cog.listener("on_dropdown")
  24. async def animalDropdown(self, inter: disnake.MessageInteraction):
  25. selected = inter.values[0]
  26. if selected == "Cats":
  27. async with aiohttp.ClientSession() as session:
  28. async with session.get(f"https://api.thecatapi.com/v1/images/search?has_breeds=1&api_key={catapi}") as resp:
  29. data = json.loads(await resp.text())
  30. catEmbed = disnake.Embed(
  31. title = "Cats!",
  32. colour = disnake.Colour.random()
  33. ).set_image(
  34. url = data[0]['url']
  35. ).set_footer(
  36. text = f"This is a {data[0]['breeds'][0]['name']} cat.")
  37. await inter.response.send_message(embed = catEmbed)
  38. elif selected == "Dogs":
  39. async with aiohttp.ClientSession() as session:
  40. async with session.get(f"https://api.thedogapi.com/v1/images/search?has_breeds=1&api_key={dogapi}") as resp:
  41. data = json.loads(await resp.text())
  42. dogEmbed = disnake.Embed(
  43. title = "Dogs!",
  44. colour = disnake.Colour.random()
  45. ).set_image(
  46. url = data[0]['url']
  47. ).set_footer(
  48. text = f"This is a {data[0]['breeds'][0]['name']} dog.")
  49. await inter.response.send_message(embed = dogEmbed)
  50. elif selected == "Ducks":
  51. async with aiohttp.ClientSession() as session:
  52. async with session.get(f"https://random-d.uk/api/v2/quack") as resp:
  53. data = json.loads(await resp.text())
  54. duckEmbed = disnake.Embed(
  55. title = "Ducks!",
  56. colour = disnake.Colour.random()
  57. ).set_image(
  58. url = data['url']
  59. ).set_footer(
  60. text = data['message'])
  61. await inter.response.send_message(embed = duckEmbed)
  62. elif selected== "Foxes":
  63. async with aiohttp.ClientSession() as session:
  64. async with session.get(f"https://some-random-api.ml/animal/fox") as resp:
  65. data = json.loads(await resp.text())
  66. foxEmbed = disnake.Embed(
  67. title = "Foxes!",
  68. colour = disnake.Colour.random()
  69. ).set_image(
  70. url = data['image']
  71. ).set_footer(
  72. text = data['fact'])
  73. await inter.response.send_message(embed = foxEmbed)
  74.  
  75. def setup(bot):
  76. bot.add_cog(animals(bot))
  77. print("| Animals - Loaded")
Advertisement
Add Comment
Please, Sign In to add comment