You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nextcord斜杠命令无响应问题排查求助

问题分析与修复方案

你的代码存在两个关键错误,导致斜杠命令触发超时提示:

1. 多余的交互处理调用

在ping函数中,await client.process_application_commands(interaction)是完全不必要的。@client.slash_command装饰器已经自动处理了斜杠命令的交互流程,重复调用该方法会干扰Discord的响应机制,直接引发超时。

2. 未正确等待异步函数返回结果

你的embed函数是异步定义的(带async关键字),调用时必须加上await才能获取到真正的nextcord.Embed对象。直接调用embed(...)会返回一个协程对象,而非合法的消息嵌入内容,导致发送消息失败,最终触发超时。

修复后的完整代码

import nextcord, os
from dotenv import load_dotenv
from nextcord.ext import commands
from flask import Flask
from threading import Thread

load_dotenv()

app = Flask('')
@app.route('/')
def home() -> str:
    return 'Running!'
def run() -> None:
    app.run(port=int(os.environ.get('PORT', 33507)))
def keep_alive() -> None:
    Thread(target=run).start()

TOKEN = os.environ['DISCORD_TOKEN']
description = '[bot name] [version]'
intents = nextcord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix='/', description=description, intents=intents)

async def embed(title, description, reason) -> nextcord.Embed:
    return nextcord.Embed(
        title=title,
        description=description,
        color=0x00FF00
    ).set_author(
        name='[bot name] [version]',
        icon_url='[image link]'
    ).set_footer(
        text=f'This message was sent because {reason}.'
    )

@client.event
async def on_ready():
    print(f'Logged in as {client.user} (ID: {client.user.id})')
    await client.change_presence(activity=nextcord.Game(name='[version]'))

@client.slash_command(name='ping', description='Returns bot latency')
async def ping(interaction: nextcord.Interaction):
    await interaction.response.defer()
    # 等待异步embed函数返回合法的Embed对象
    ping_embed = await embed(':ping_pong: Pong!', f'{client.latency * 100:.2f} ms', f'{interaction.user} used the "ping" command')
    await interaction.followup.send(embed=ping_embed)

if __name__ == '__main__':
    keep_alive()
    client.run(TOKEN)

额外优化说明

  • 给延迟数值添加.2f格式化,让显示结果更简洁美观(如12.34 ms而非12.345678 ms)
  • interaction.response.defer()默认会显示"正在思考"状态,无需额外指定with_message=True

内容的提问来源于stack exchange,提问作者silvncr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 16:01:14