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

Nextcord Cog中执行斜杠命令后发送普通消息的实现问题

在Nextcord Cog中实现斜杠命令回复消息

正确用法示例

在Nextcord里,斜杠命令的交互对象(Interaction)需通过interaction.response.send_message()完成回复,若要发送后续消息则用followup.send()。以下是Cog中的完整实现代码:

基础斜杠命令回复

from nextcord import Interaction
from nextcord.ext import commands

class MyCommands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command(name="hello", description="发送问候消息")
    async def hello_command(self, interaction: Interaction):
        # 直接回复斜杠命令交互
        await interaction.response.send_message("你好!这是斜杠命令的回复")

def setup(bot):
    bot.add_cog(MyCommands(bot))

发送后续消息

如果需要在初始回复后再发额外内容,可以使用后续消息方法:

@commands.slash_command(name="multi_msg", description="发送多条消息")
async def multi_message(self, interaction: Interaction):
    # 先完成交互响应,防止超时
    await interaction.response.send_message("第一条消息")
    # 发送后续消息
    await interaction.followup.send("这是后续发送的第二条消息")

你之前的错误原因

  • interaction.send_message:Nextcord的Interaction对象不存在这个方法,正确的交互响应方法是interaction.response.send_message()
  • self.bot.send_message:该方法已被废弃,若要直接给频道发消息,需用await interaction.channel.send("消息内容"),但斜杠命令场景优先用交互响应方法,避免触发交互超时提示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 12:00:22