簡體   English   中英

Pycord - 轉換為斜杠命令

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM