[英]Pycord - Converting to Slash Commands
我正在尝试将此工作代码转换为斜杠命令,但它不起作用
我要转换的代码:(最小)
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)
我尝试以与其他斜杠命令相同的方式执行此操作,但它没有响应
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)
尝试斜线命令时出现的错误:
Application Command raised an exception:
NotFound: 404 Not Found (error code: 10062):
Unknown interaction
首先,pycord 使用上下文并为guild_ids
采用 integer ID。
对于错误,命令可能需要超过 3 秒才能响应(可能是因为animec.Anime(search)
)。 如果超过 3 秒,您可以使用await ctx.defer()
来推迟响应交互。
所以你应该这样做:
@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.