So today I was trying to add a command to my discord bot that would allow it to connect to TheCatApi, and I was unable to do it due to multiple errors. Here's my code below:
import nextcord
import os
from nextcord import Interaction, SlashOption
from nextcord.ext import commands
import asyncio
import catapi
loop = asyncio.new_event_loop()
api = catapi.CatApi(api_key=os.environ['CAT_API_KEY'])
def run_coro(coroutine):
return loop.run_until_complete(coroutine)
class Fun(commands.Cog):
def __init__(self, client):
self.client = client
testServerID = 827933970742116442
@nextcord.slash_command(name = "cat", description = "Sends a random cat photo uwu", guild_ids=[testServerID])
async def cat(self, interaction: Interaction):
results = run_coro(api.search_images(limit=1))
print(results[0].url)
await interaction.response.send_message("heer")
print("Loaded cog: Fun.py")
def setup(client):
client.add_cog(Fun(client))
And the monumental error below:
Ignoring exception in on_interaction
Traceback (most recent call last):
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 415, in _run_event
await coro(*args, **kwargs)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 1741, in on_interaction
await self.process_application_commands(interaction)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 1757, in process_application_commands
await app_cmd.call_from_interaction(interaction)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 1182, in call_from_interaction
await self.call(self._state, interaction, interaction.data.get("options", {}))
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 1186, in call
await super().call(state, interaction, option_data)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/app
Loaded cog: Extras.py
Loaded cog: Time.py
Loaded cog: MathShit.py
Loaded cog: Admin.py
Loaded cog: Fun.py
The bot is running sir! Logged in as Meowsicle bot#4403
-------------------------------------------------------
Ignoring exception in on_interaction
Traceback (most recent call last):
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 415, in _run_event
await coro(*args, **kwargs)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 1741, in on_interaction
await self.process_application_commands(interaction)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py", line 1757, in process_application_commands
await app_cmd.call_from_interaction(interaction)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 1182, in call_from_interaction
await self.call(self._state, interaction, interaction.data.get("options", {}))
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 1186, in call
await super().call(state, interaction, option_data)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 663, in call
await self.call_invoke_slash(state, interaction, option_data)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 704, in call_invoke_slash
await self.invoke_slash(interaction, **kwargs)
File "/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/application_command.py", line 719, in invoke_slash
await self.callback(self._self_argument, interaction, **kwargs)
File "/home/runner/Meowsicle-bot/cogs/Fun.py", line 27, in cat
results = run_coro(api.search_images(limit=1))
File "/home/runner/Meowsicle-bot/cogs/Fun.py", line 14, in run_coro
return loop.run_until_complete(coroutine)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/asyncio/base_events.py", line 592, in run_until_complete
self._check_running()
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/asyncio/base_events.py", line 554, in _check_running
raise RuntimeError(
RuntimeError: Cannot run the event loop while another loop is running
/home/runner/Meowsicle-bot/venv/lib/python3.8/site-packages/nextcord/client.py:422: RuntimeWarning: coroutine 'CatApi.search_images' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
I have tried putting different parts of code in and out of the class, reformatting the code, even deleting the entire thing and rewriting it and I achieved nothing. I'm using this async wrapper for the API connection. That's all of the detail I can give you but stackoverflow is asking for more.... Anyways, thanks in advance to anyone who can help! Thanks again, and good luck to anyone who decides to try and help!
Well i have something that might help you
Heres the code i wrote:
import requests
import nextcord
from nextcord import Interaction
from nextcord.ext import commands
class Test(commands.Cog):
def __init__(self, client):
self.client = client
guild_id = 'your_id'
@nextcord.slash_command(name="cat", guild_ids=[guild_id])
async def cat(self, interaction: Interaction):
await interaction.response.defer()
data = requests.get("https://api.thecatapi.com/v1/images/search")
data = data.json()
get = data[0]
result = get["url"]
embed = nextcord.Embed(
title="A Cat",
color=nextcord.Color.random()
)
embed.set_image(url=result)
await interaction.edit_original_message(embed=embed)
def setup(client):
client.add_cog(Test(client))