Hic
Hic

Reputation: 37

Pycord - Converting to Slash Commands

I am trying to convert this working code to a slash command one but it doesn't work

The code i am trying to convert: (minimal)

import discord
from discord.ext import commands
from discord import Intents
import animec

intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

@bot.command()
async def anime(ctx, *, query):

    anime = animec.Anime(query)

    embedanime = discord.Embed(title= anime.title_english, url= anime.url, description= f"{anime.description[:1000]}...", color=0x774dea)
    await ctx.send(embed=embedanime)

bot.run(TOKEN)

I tried to do it the same way as other slash commands but it did not respond

import discord
from discord.ext import commands
from discord import Intents, Option
import animec


intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

@bot.slash_command(name="anime", description="Search for an anime on MAL", guild=discord.Object(id=824342611774144543))
async def anime(interaction: discord.Interaction, *, search: Option(str, description="What anime you want to search for?", required=True)):

    anime = animec.Anime(search)

    embedanime = discord.Embed(title= anime.title_english, url= anime.url, description= f"{anime.description[:1000]}...", color=0x774dea)
    await interaction.response.send_message(embed=embedanime)


bot.run(TOKEN)

The error i am getting when i try the slash command:

Application Command raised an exception:
NotFound: 404 Not Found (error code: 10062):
Unknown interaction

Upvotes: 1

Views: 1275

Answers (1)

Middledot
Middledot

Reputation: 112

Firstly, pycord uses contexts and takes the integer IDs for guild_ids.

For the error, the command might be taking more than 3 seconds to respond (probably because of animec.Anime(search)). You can use await ctx.defer() to postpone responding to the interaction if it takes longer than 3 seconds.

So you should instead be doing:

@bot.slash_command(name="anime", description="Search for an anime on MAL", guild_ids=[824342611774144543])
async def anime(ctx: discord.ApplicationContext, *, search = Option(str, description="What anime you want to search for?", required=True)):
    await ctx.defer()
    anime = animec.Anime(search)

    embedanime = discord.Embed(title=anime.title_english, url=anime.url, description=f"{anime.description[:1000]}...", color=0x774dea)
    await ctx.respond(embed=embedanime)

Upvotes: 2

Related Questions