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

如何使用从Embed页脚获取的用户ID向Discord用户发送DM?

问题原因

client.get_user() 仅从 bot 的本地内存缓存中读取用户数据,如果目标用户不在缓存中(比如 bot 启动后该用户从未在 bot 可见的频道发过消息、触发过交互),就会返回 None,你后续尝试发送 None 就触发了空消息报错。
该问题和你使用的 slash commands 框架没有直接关联。

解决方案

改用异步API方法 client.fetch_user() 直接请求 Discord 接口拉取用户数据,不受本地缓存限制,同时增加异常处理兼容边界场景。

修正后代码片段

msg = await ctx.channel.fetch_message(id)
id2 = int(msg.embeds[0].footer.text)
print(type(id2))
await ctx.send(str(id2), hidden=True)
# 替换get_user为fetch_user,需要加await
try:
    user = await client.fetch_user(id2)
    print(type(user))
    await ctx.send(f"获取到用户:{user}", hidden=True)
    await user.send("IT WORKED!!!")
except discord.NotFound:
    await ctx.send("目标用户不存在", hidden=True)
except discord.Forbidden:
    await ctx.send("无法向该用户发送DM,可能对方已屏蔽bot或未开启私信权限", hidden=True)

额外检查项

  • 确认你在 Discord 开发者后台的 bot 配置页,已经开启了 Server Members Intent 权限,否则拉取用户数据可能被拦截
  • 确认你的 bot 拥有发送私信的基础权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 08:45:04