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

discord.ext.slash工单close斜杠命令无法关闭频道报错问题

discord.ext.slash 工单close命令失效修复

问题场景

使用discord.ext.slash开发工单机器人的/close斜杠命令,预期执行命令后关闭对应工单频道,两次代码实现均运行报错,无法正常生效。

第一次实现与报错

初始代码如下:

@client.slash_cmd()
async def close(ctx: slash.Context):
    """Closes the ticket"""
    await ctx.delete()

运行后抛出错误:

File "/home/runner/Ticket-Bot/venv/lib/python3.8/site-packages/discord/ext/slash/__init__.py", line 1371, in on_interaction_create
    raise commands.CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10015): Unknown Webhook

第二次修改尝试与报错

将代码中ctx.delete()替换为ctx.channel.delete()后再次运行,抛出错误:

Traceback (most recent call last):
  File "/home/runner/Ticket-Bot/venv/lib/python3.8/site-packages/discord/ext/slash/__init__.py", line 1371, in on_interaction_create
    raise commands.CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Object' object has no attribute 'delete'

错误原因

  • 第一次的ctx.delete()属于方法误用:斜杠上下文的delete方法目标是交互响应关联的webhook,没有提前发送交互响应、或者交互已失效时调用,就会触发10015 Unknown Webhook错误,这个方法根本不会删除频道。
  • 第二次的ctx.channel.delete()报错是旧版discord.ext.slash的特性问题:未主动拉取完整频道实例时,ctx.channel默认返回仅携带频道ID的Object类型实例,这个类没有delete方法,直接调用必然抛出属性错误。

修复方案

按照以下逻辑修改代码即可:

  • 斜杠命令触发后必须在3秒内返回交互响应,避免交互超时失效
  • 通过频道ID主动拉取完整的频道实例,再执行删除操作
  • 提前给机器人分配目标频道的「管理频道」权限,避免无权限报错

正确可运行的代码示例:

@client.slash_cmd()
async def close(ctx: slash.Context):
    """关闭工单"""
    # 先响应交互,提示用户即将关闭频道
    await ctx.respond("工单关闭中,频道将在2秒后删除", delete_after=1)
    # 拉取完整的频道实例
    ticket_channel = await ctx.guild.fetch_channel(ctx.channel.id)
    # 执行频道删除,可传入操作日志原因
    await ticket_channel.delete(reason=f"工单由操作用户{ctx.author}手动关闭")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 23:09:15