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

如何让Discord机器人将指定命令消息发送至指定频道?

解决Discord机器人Training命令发送消息到指定频道的问题

你遇到的send属性错误,核心原因是没有正确获取有效的频道对象,另外Discord的斜杠命令要求必须在3秒内对交互做出响应,不能直接跳过交互回复去调用频道发送,否则会触发超时错误。

解决思路

  1. 先对交互发起者做出快速响应,避免命令超时
  2. 获取目标通知频道的有效对象
  3. 在目标频道发送训练通知

方案1:固定目标频道(通过频道ID)

将通知发送到预设的固定频道,适合服务器有固定通知渠道的场景:

@bot.tree.command(name='training', description='Set up a training date & announce it to everyone in the server')
@app_commands.describe(date = "Date", time = "Time", location = "Location")
async def training(interaction: discord.Interaction, date: str, time: str, location: str):
    # 先回复发起者(仅发起者可见),避免命令超时
    await interaction.response.send_message("训练通知已发布!", ephemeral=True)
    
    # 替换为你需要发送通知的频道ID(整数类型)
    target_channel_id = 123456789012345678
    target_channel = interaction.guild.get_channel(target_channel_id)
    
    if target_channel:
        # 在目标频道发送公开通知,可按需添加@everyone或角色提及
        await target_channel.send(f"📢 {interaction.user.mention} 发起训练通知!\n地点:{location}\n日期:{date}\n时间:{time}")
    else:
        # 频道不存在时通知发起者
        await interaction.followup.send("无法找到指定通知频道,请检查频道ID是否正确!", ephemeral=True)

方案2:让用户选择目标频道(更灵活)

允许用户在执行命令时指定发送通知的频道,适合多渠道通知的场景:

@bot.tree.command(name='training', description='Set up a training date & announce it to everyone in the server')
@app_commands.describe(
    date = "训练日期",
    time = "训练时间",
    location = "训练地点",
    target_channel = "发送通知的频道"
)
async def training(interaction: discord.Interaction, date: str, time: str, location: str, target_channel: discord.TextChannel):
    # 快速回复发起者
    await interaction.response.send_message("训练通知已发布!", ephemeral=True)
    
    # 在用户指定的频道发送通知
    await target_channel.send(f"📢 {interaction.user.mention} 发起训练通知!\n地点:{location}\n日期:{date}\n时间:{time}")

关键注意事项

  • 必须优先调用interaction.response.send_message或interaction.response.defer()响应交互,否则Discord会判定命令超时失败
  • 使用ephemeral=True的回复仅对命令发起者可见,不会污染频道消息
  • 确保机器人拥有目标频道的发送消息权限,如果需要@全体成员,还要开启mention_everyone权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:40:29