Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import disnake
- from disnake.ext import commands
- import aiohttp
- import json
- from dotenv import load_dotenv
- import os
- load_dotenv()
- catapi = os.environ['catapi']
- dogapi = os.environ['dogapi']
- class animals(commands.Cog):
- def __init__(self, bot: commands.InteractionBot):
- self.bot = bot
- @commands.slash_command()
- async def animals(self, inter):
- """Displays an animal photo."""
- animalList = ['Cats', 'Dogs', 'Ducks', 'Foxes']
- await inter.response.send_message("Select an animal.", components = disnake.ui.StringSelect(options = animalList))
- @commands.Cog.listener("on_dropdown")
- async def animalDropdown(self, inter: disnake.MessageInteraction):
- selected = inter.values[0]
- if selected == "Cats":
- async with aiohttp.ClientSession() as session:
- async with session.get(f"https://api.thecatapi.com/v1/images/search?has_breeds=1&api_key={catapi}") as resp:
- data = json.loads(await resp.text())
- catEmbed = disnake.Embed(
- title = "Cats!",
- colour = disnake.Colour.random()
- ).set_image(
- url = data[0]['url']
- ).set_footer(
- text = f"This is a {data[0]['breeds'][0]['name']} cat.")
- await inter.response.send_message(embed = catEmbed)
- elif selected == "Dogs":
- async with aiohttp.ClientSession() as session:
- async with session.get(f"https://api.thedogapi.com/v1/images/search?has_breeds=1&api_key={dogapi}") as resp:
- data = json.loads(await resp.text())
- dogEmbed = disnake.Embed(
- title = "Dogs!",
- colour = disnake.Colour.random()
- ).set_image(
- url = data[0]['url']
- ).set_footer(
- text = f"This is a {data[0]['breeds'][0]['name']} dog.")
- await inter.response.send_message(embed = dogEmbed)
- elif selected == "Ducks":
- async with aiohttp.ClientSession() as session:
- async with session.get(f"https://random-d.uk/api/v2/quack") as resp:
- data = json.loads(await resp.text())
- duckEmbed = disnake.Embed(
- title = "Ducks!",
- colour = disnake.Colour.random()
- ).set_image(
- url = data['url']
- ).set_footer(
- text = data['message'])
- await inter.response.send_message(embed = duckEmbed)
- elif selected== "Foxes":
- async with aiohttp.ClientSession() as session:
- async with session.get(f"https://some-random-api.ml/animal/fox") as resp:
- data = json.loads(await resp.text())
- foxEmbed = disnake.Embed(
- title = "Foxes!",
- colour = disnake.Colour.random()
- ).set_image(
- url = data['image']
- ).set_footer(
- text = data['fact'])
- await inter.response.send_message(embed = foxEmbed)
- def setup(bot):
- bot.add_cog(animals(bot))
- print("| Animals - Loaded")
Advertisement
Add Comment
Please, Sign In to add comment