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

自制Discord Bot:Slash命令可用但提示“应用未响应”的原因

问题分析与修复方案

核心原因

Discord斜杠命令有3秒强制响应要求:如果Bot在3秒内没有对用户的命令请求返回任何反馈(哪怕是临时加载提示),Discord就会触发"The application did not respond"错误。你的代码只向指定频道发送了统计结果,但没有给发起命令的用户返回任何响应,导致平台判定Bot未处理请求。

另外代码还有几个细节问题:

  • 权限检查逻辑重复冗余
  • 角色/频道查找没有容错处理(找不到时会直接报错崩溃)
  • 斜杠命令描述里有拼写错误(Sergeatns应为Sergeants)

修复后的完整代码

import discord
import pytz
import re
from discord.ext import commands
from datetime import datetime, timezone
from pytz import timezone as tz

# 替换成你的Bot初始化逻辑,示例:
# client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
client = aclient()
tree = discord.app_commands.CommandTree(client)

# 封装权限检查函数,避免重复写相同逻辑
def can_run_command(ctx):
    officers_role = discord.utils.get(ctx.guild.roles, name='=CALUM= Officers')
    join_role = discord.utils.get(ctx.guild.roles, name='CÆLUM_on_member_join')
    if not officers_role or not join_role:
        return False
    max_user_role_pos = max(role.position for role in ctx.author.roles)
    required_min_pos = max(officers_role.position, join_role.position)
    return max_user_role_pos >= required_min_pos

@tree.command(description='Count members with "=CALUM= Private" role.')
@commands.check(can_run_command)
async def privates(ctx):
    # 先发送临时响应,避免超时错误
    await ctx.response.defer(ephemeral=False)  # ephemeral=True则仅发起者可见临时提示
    
    guild = ctx.guild
    target_role = discord.utils.get(guild.roles, name='=CALUM= Private')
    target_channel = discord.utils.get(guild.channels, name='role-counter')
    
    # 容错处理:找不到角色或频道时提示
    if not target_role or not target_channel:
        await ctx.followup.send("找不到目标角色或频道,请检查名称是否正确。")
        return
    
    member_count = len(target_role.members)
    result_msg = f"The number of members with the '=CALUM= Private' role is {member_count}."
    await target_channel.send(result_msg)
    # 给命令发起者发送确认消息
    await ctx.followup.send(f"统计完成:{result_msg}")

@tree.command(description='Count members with "=CALUM= Sergeants" role.')  # 修正拼写错误
@commands.check(can_run_command)
async def sergeants(ctx):
    await ctx.response.defer(ephemeral=False)
    
    guild = ctx.guild
    target_role = discord.utils.get(guild.roles, name='=CALUM= Sergeants')
    target_channel = discord.utils.get(guild.channels, name='role-counter')
    
    if not target_role or not target_channel:
        await ctx.followup.send("找不到目标角色或频道,请检查名称是否正确。")
        return
    
    member_count = len(target_role.members)
    result_msg = f"The number of members with the '=CALUM= Sergeants' role is {member_count}."
    await target_channel.send(result_msg)
    await ctx.followup.send(f"统计完成:{result_msg}")

@tree.command(description='Count members with "=CALUM= Officers" role.')
@commands.check(can_run_command)
async def officers(ctx):
    await ctx.response.defer(ephemeral=False)
    
    guild = ctx.guild
    target_role = discord.utils.get(guild.roles, name='=CALUM= Officers')
    target_channel = discord.utils.get(guild.channels, name='role-counter')
    
    if not target_role or not target_channel:
        await ctx.followup.send("找不到目标角色或频道,请检查名称是否正确。")
        return
    
    member_count = len(target_role.members)
    result_msg = f"The number of members with the '=CALUM= Officers' role is {member_count}."
    await target_channel.send(result_msg)
    await ctx.followup.send(f"统计完成:{result_msg}")

# 记得添加Bot启动和命令同步代码(示例):
# @client.event
# async def on_ready():
#     await tree.sync()
#     print(f"Logged in as {client.user}")
# 
# client.run("你的Bot Token")

关键修复点

  1. 解决超时问题:用await ctx.response.defer()在3秒内给Discord返回临时加载提示,告诉平台Bot正在处理,后续通过ctx.followup.send()发送最终结果。
  2. 增加用户反馈:除了向指定频道发送统计结果,还给命令发起者单独发送确认消息,让用户直观看到操作结果。
  3. 容错处理:添加角色/频道查找失败的判断,避免Bot直接崩溃。
  4. 代码优化:把重复的权限检查逻辑封装成函数,减少冗余代码。
  5. 修正拼写错误:修复了斜杠命令描述里的单词拼写错误。

内容的提问来源于stack exchange,提问作者cedric.t

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 07:32:49