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

discord.py清除消息Slash命令报错求助:保留嵌入与临时消息

解决discord.py Slash命令clearmsg的报错问题

问题根源

你碰到的两个错误都是因为discord.py版本不匹配+方法/参数用错:

  • 加discord.Interaction注解报错:说明你用的是v1.x版本的discord.py,但硬套了v2.x的类型;v1.x里Slash命令的交互对象是SlashContext,不是Interaction。
  • 用ctx.respond报错:v1.x里根本没这个方法,而且如果是v2.x的交互对象,回复得用interaction.response.send_message(),不是直接respond()。

对应版本的正确代码

如果你用的是discord.py v1.x(1.7.x左右)

v1.x本身不支持Slash命令,得装discord-py-slash-command库,代码这么写:

import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
from discord_slash.utils.manage_commands import create_option

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)

@slash.slash(
    name="clearmsg",
    description="清除指定数量的消息",
    options=[
        create_option(
            name="quantity",
            description="要清除的消息数量",
            option_type=4,  # 整数类型
            required=True
        )
    ]
)
async def _clearmsg(ctx: SlashContext, quantity: int):
    # 检查机器人权限
    if not ctx.channel.permissions_for(ctx.me).manage_messages:
        embed = discord.Embed(title="权限不够", description="我需要「管理消息」权限才能干活", color=discord.Color.red())
        await ctx.send(embed=embed, hidden=True)  # hidden=True就是临时消息,只有你能看到
        return
    
    # 限制清除数量,防止误操作
    if quantity <= 0 or quantity > 100:
        embed = discord.Embed(title="参数错了", description="请输入1到100之间的整数", color=discord.Color.orange())
        await ctx.send(embed=embed, hidden=True)
        return
    
    # 清除消息,+1是把命令本身也删掉
    deleted = await ctx.channel.purge(limit=quantity + 1)
    embed = discord.Embed(title="搞定了", description=f"成功清了**{len(deleted)-1}**条消息", color=discord.Color.green())
    await ctx.send(embed=embed, hidden=True)

bot.run("你的机器人TOKEN")

如果你用的是discord.py v2.x(最新版)

v2.x原生支持Slash命令,代码这么写:

import discord
from discord import app_commands
from discord.ext import commands

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f"登录成功,当前账号:{bot.user}")
    try:
        synced = await bot.tree.sync()
        print(f"同步了 {len(synced)} 条Slash命令")
    except Exception as e:
        print(e)

@bot.tree.command(name="clearmsg", description="清除指定数量的消息")
@app_commands.describe(quantity="要清除的消息数量")
async def clearmsg(interaction: discord.Interaction, quantity: int):
    # 检查权限
    if not interaction.channel.permissions_for(interaction.guild.me).manage_messages:
        embed = discord.Embed(title="权限不够", description="我需要「管理消息」权限才能干活", color=discord.Color.red())
        await interaction.response.send_message(embed=embed, ephemeral=True)  # ephemeral=True是临时消息
        return
    
    if quantity <= 0 or quantity > 100:
        embed = discord.Embed(title="参数错了", description="请输入1到100之间的整数", color=discord.Color.orange())
        await interaction.response.send_message(embed=embed, ephemeral=True)
        return
    
    deleted = await interaction.channel.purge(limit=quantity + 1)
    embed = discord.Embed(title="搞定了", description=f"成功清了**{len(deleted)-1}**条消息", color=discord.Color.green())
    await interaction.response.send_message(embed=embed, ephemeral=True)

bot.run("你的机器人TOKEN")

必看注意事项

  • v1.x要先装依赖:执行pip install discord-py-slash-command
  • v2.x要确保是最新版:执行pip install -U discord.py
  • 临时消息在v1.x用hidden=True,v2.x用ephemeral=True
  • v2.x里Slash命令的交互对象必须用discord.Interaction注解,回复必须用interaction.response.send_message(),不能直接调用respond()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 14:36:17