简体   繁体   English

Pycord - 转换为斜杠命令

[英]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 .首先,pycord 使用上下文并为guild_ids采用 integer ID。

For the error, the command might be taking more than 3 seconds to respond (probably because of animec.Anime(search) ).对于错误,命令可能需要超过 3 秒才能响应(可能是因为animec.Anime(search) )。 You can use await ctx.defer() to postpone responding to the interaction if it takes longer than 3 seconds.如果超过 3 秒,您可以使用await ctx.defer()来推迟响应交互。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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