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

如何用Python通过用户ID发送Discord私信(无需服务器机器人)

用Python通过用户ID批量发送Discord私信(无需机器人)

重要提醒

使用个人账号Token自动化发送私信违反Discord服务条款,可能导致账号被封禁,请谨慎操作。

实现步骤

  1. 获取个人账号Token
    在Discord客户端按Ctrl+Shift+I打开开发者工具,切换到Application标签页,左侧Local Storage中找到https://discord.com,提取token字段的值。注意:绝对不要泄露你的Token,否则他人可完全控制你的账号。

  2. 核心逻辑
    Discord私信需先创建会话(获取会话ID)才能发消息,流程为:

  • 针对每个目标用户ID,发送请求创建私信会话
  • 拿到会话ID后,再发送消息至该会话
  1. Python代码实现
    先安装依赖:
pip install requests

示例代码:

import requests

# 配置信息
DISCORD_TOKEN = "你的个人账号Token"
TARGET_USER_IDS = ["用户ID1", "用户ID2", "用户ID3"]  # 替换为实际目标用户ID列表
MESSAGE_CONTENT = "你要发送的私信内容"

# 请求头
headers = {
    "Authorization": DISCORD_TOKEN,
    "Content-Type": "application/json"
}

def create_dm_channel(user_id):
    """创建与指定用户的私信会话"""
    url = "https://discord.com/api/v9/users/@me/channels"
    payload = {"recipient_id": user_id}
    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        return response.json()["id"]
    except requests.exceptions.RequestException as e:
        print(f"创建与用户 {user_id} 的会话失败: {str(e)}")
        return None

def send_dm_message(channel_id, message):
    """向指定会话发送消息"""
    url = f"https://discord.com/api/v9/channels/{channel_id}/messages"
    payload = {"content": message}
    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()
        print(f"消息发送成功到会话 {channel_id}")
    except requests.exceptions.RequestException as e:
        print(f"发送消息到会话 {channel_id} 失败: {str(e)}")

# 批量发送流程
for user_id in TARGET_USER_IDS:
    channel_id = create_dm_channel(user_id)
    if channel_id:
        send_dm_message(channel_id, MESSAGE_CONTENT)

额外提示

  • 若已和用户有过私信会话,create_dm_channel会直接返回已有会话ID,不会重复创建
  • 避免短时间内批量发送大量消息,Discord的反机器人机制容易触发账号限制
  • 遇到403错误时,可能是账号被限制,或目标用户拉黑了你/关闭了私信

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 00:09:24