首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pycord -转换为Slash命令

Pycord -转换为Slash命令
EN

Stack Overflow用户
提问于 2022-10-03 14:25:26
回答 1查看 244关注 0票数 0

我试图将此工作代码转换为斜杠命令--但它不起作用

我试图转换的代码:(最小)

代码语言:javascript
复制
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)

我试着用和其他斜杠命令一样的方式来做,但是它没有响应。

代码语言:javascript
复制
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)

当我尝试使用斜杠命令时所遇到的错误:

代码语言:javascript
复制
Application Command raised an exception:
NotFound: 404 Not Found (error code: 10062):
Unknown interaction
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-05 00:51:38

首先,pycord使用上下文并为guild_ids获取整数ID。

对于错误,命令可能需要超过3秒才能响应(可能是因为animec.Anime(search))。如果交互时间超过3秒,可以使用await ctx.defer()延迟响应。

所以你应该做的是:

代码语言:javascript
复制
@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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73936888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档