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

Discord.py 机器人开发:如何实现!feedback命令将用户反馈私信发给开发者

实现步骤

以下是基于discord.py v2.x版本的实现方案:

1. 前置准备:开启必要的网关意图

discord.py 2.0之后默认关闭消息内容读取权限,你需要做两步配置:

  • 先前往Discord开发者门户的Bot设置页,开启Message Content Intent开关
  • 在代码中声明启用对应意图,示例代码:
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True # 必须开启,否则无法读取用户输入的反馈内容

bot = commands.Bot(command_prefix="!", intents=intents)
# 替换下方ID为你自己的Discord用户ID
DEVELOPER_USER_ID = 123456789012345678

2. 编写!feedback命令逻辑

直接在代码中添加如下命令函数即可实现基础功能:

@bot.command(name="feedback")
async def receive_feedback(ctx, *, content: str):
    # 校验用户是否输入了反馈内容
    if not content.strip():
        await ctx.send("请在命令后填写反馈内容,示例:`!feedback 希望新增XX查询功能`")
        return
    # 获取开发者用户对象并发送私信
    developer = await bot.fetch_user(DEVELOPER_USER_ID)
    feedback_notice = f"""
收到新的用户反馈:
- 反馈用户:{ctx.author}(用户ID:{ctx.author.id})
- 来源场景:{ctx.guild.name if ctx.guild else "私信"}(服务器ID:{ctx.guild.id if ctx.guild else "无"})
- 反馈内容:{content}
    """
    await developer.send(feedback_notice)
    # 给发送反馈的用户回执
    await ctx.send("反馈已成功提交给开发者,感谢你的建议!")

3. 可选优化:添加异常处理

避免因权限问题导致报错,可添加异常捕获逻辑:

@bot.command(name="feedback")
async def receive_feedback(ctx, *, content: str):
    if not content.strip():
        await ctx.send("请在命令后填写反馈内容,示例:`!feedback 希望新增XX查询功能`")
        return
    try:
        developer = await bot.fetch_user(DEVELOPER_USER_ID)
        feedback_notice = f"""
收到新的用户反馈:
- 反馈用户:{ctx.author}(用户ID:{ctx.author.id})
- 来源场景:{ctx.guild.name if ctx.guild else "私信"}(服务器ID:{ctx.guild.id if ctx.guild else "无"})
- 反馈内容:{content}
        """
        await developer.send(feedback_notice)
        await ctx.send("反馈已成功提交给开发者,感谢你的建议!")
    except discord.Forbidden:
        await ctx.send("反馈发送失败,暂时无法联系到开发者,请稍后再试。")
    except Exception:
        await ctx.send("提交反馈时出现未知错误,请稍后再试。")

注意事项

  • 一定要将DEVELOPER_USER_ID替换为你自己的Discord用户ID,可开启Discord开发者模式后右键自己的头像复制ID
  • 机器人不需要和你互加好友,只要你和机器人在同一个服务器,或之前有过私信对话即可正常收到消息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 12:24:03