简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM