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

使用Discord.py(用户令牌)给所有好友发消息报错求助

问题分析与解决方案

错误原因

报错TypeError: 'module' object is not callable是因为你调用了discord.client(),但discord.client是模块,不是可调用的类。正确的类是discord.Client(首字母大写C)。

另外还有两个潜在问题需要修正:

  1. client.user.friends在Discord.py v2.x版本中已被移除,需要改用client.fetch_friends()异步方法获取好友列表。
  2. on_connect事件触发时,客户端可能还未完全初始化,client.user可能为None,建议改用on_ready事件,确保客户端准备就绪后再执行操作。

修正后的代码

import discord

with open('TOKEN_TESTUSER.txt', 'r') as f:
    # 读取令牌时去除换行符,避免登录失败
    TOKEN = f.readline().strip()

# 使用正确的Client类(大写C),并启用默认权限
client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print(f"已登录为 {client.user}")
    # 异步遍历好友列表
    async for user in client.fetch_friends():
        try:
            await user.send('测试消息')
            print(f"成功发送消息给 {user.name}")
        except Exception as e:
            # 捕获具体错误信息,方便排查问题
            print(f"发送消息给 {user.name} 失败: {str(e)}")

# 运行客户端,指定bot=False表示使用个人账号
client.run(TOKEN, bot=False)

额外注意事项

  • 确保Discord.py版本为v2.x以上,可通过pip install -U discord.py更新。
  • 个人账号使用令牌登录违反Discord服务条款,可能导致账号封禁,请谨慎操作。
  • 读取令牌时用strip()清除文件中的换行符,避免因令牌格式问题引发登录错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 02:20:55