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

使用discord.py开发机器人时斜杠命令提示‘app didn't respond in time’的问题求助

discord.py开发机器人时斜杠命令提示‘app didn't respond in time’的问题求助

我正在开发一个Discord机器人,想要添加斜杠命令功能,但遇到了麻烦——每次使用斜杠命令时,Discord客户端都会提示“app didn't respond”或者“didn't respond in time”的提示。

我原本尝试过app_commands,现在换成了hybrid_commands,但问题依然存在。奇怪的是,用前缀形式的命令(比如直接输入/ping作为前缀命令)是可以正常工作的,但切换成斜杠命令就不行。

我在on_ready事件里已经做了命令同步的操作,启动日志里也能看到命令同步成功的提示,但斜杠命令就是无法正常响应。

这是我当前的代码:

import discord
from discord.ext import commands
from datetime import datetime


intents = discord.Intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix="/", intents=intents)


# Fetch the token from environment variables
token = 'token'
# Ensure you set 'DISCORD_TOKEN' in your .env file

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")
    print("Registered commands:")
    for command in bot.tree.get_commands():
        print(f"- {command.name}: {command.description}")
    await bot.tree.sync()
    print("Commands synced.")


@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    user = str(message.author)
    user_message = str(message.content)
    channel = str(message.channel.name)

    print(f'Message {user_message} by {user} on {channel}')

    if channel == "test":
        if user_message.lower() == "hi":
            await message.channel.send(f"Hey {user}")
        elif user_message.lower() == "joke":  # Corrected the joke condition
            await message.channel.send("What did the perfume say to the sweat when it defeated it?\nDon't be salty!!")
    await bot.process_commands(message)


def convert_to_unix_timestamp(date_time):
    date_obj = datetime.strptime(date_time, '%Y-%m-%d %H:%M:%S')
    timestamp = int(date_obj.timestamp())
    return timestamp


@commands.hybrid_command(name="ping", description="To Test connectivity")
async def ping(ctx):
    await ctx.defer()

    await ctx.send("Pong!")


@bot.hybrid_command(name="time")
async def time(ctx, date, time):
    date_time = f"{date} {time}"
    stamp = convert_to_unix_timestamp(date_time)
    await ctx.send(f"<t:{stamp}:R>")


bot.add_command(ping)


# Use the token variable for running the bot
bot.run(token)

备注:内容来源于stack exchange,提问作者Lansi Emad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:27:58