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

Discord自机器人登录无响应及dmall命令故障求助

嘿,我仔细看了你的Discord自机器人代码,发现几个核心问题导致on_ready事件不触发,还有dmall命令故障。下面逐一分析并给出修复方案:

1. 最核心问题:缺失Client/Bot实例化

你的代码里完全没有定义client对象,但却直接使用了@client.event和@client.command装饰器——程序根本不知道client是什么,自然无法触发任何事件或执行命令!

因为你用到了命令框架(比如dmall),推荐用commands.Bot来实例化(而非基础的discord.Client),它自带命令支持,同时需要开启Discord要求的意图权限。请在所有函数/装饰器之前添加这段代码:

import discord
from discord.ext import commands
import requests

# 初始化必要的意图:命令触发需要读取消息内容,遍历成员需要服务器成员权限
intents = discord.Intents.default()
intents.message_content = True  # 必须开启,否则无法读取命令消息
intents.members = True          # 必须开启,否则无法获取服务器成员列表

# 设置命令前缀(比如用'!'触发命令),传入意图
client = commands.Bot(command_prefix='!', intents=intents)
2. on_ready不触发的其他原因
  • 用户Token的限制:Discord严格禁止用普通用户账号(bot=False)运行自动化工具,很可能会触发人机验证、登录拦截,导致客户端无法完全连接,on_ready自然不会触发。强烈建议改用官方机器人账号(在Discord开发者后台创建机器人应用后获取Token),并设置bot=True登录。
  • 过时的Token验证API:你用的https://canary.discordapp.com/api/v6/users/@me是废弃的v6版本API,现在Discord API已经更新到v10,这个端点可能失效。可以替换成v10的验证端点:https://discord.com/api/v10/users/@me。
3. dmall命令的故障修复
  • 错误的消息删除时机:你在遍历成员的循环里每次都执行await ctx.message.delete(),这会导致第一次循环就删掉了命令消息,后续循环会因为ctx.message不存在而报错。把删除操作移到循环外,只执行一次:
@client.command()
async def dmall(ctx,*,message):
    await ctx.message.delete()  # 移到循环前,仅删除一次命令消息
    for member in ctx.guild.members:
        try:
            await member.send(message)
            print(f'Sent dm to: {member.name}')
        except Exception as e:
            # 打印具体错误,方便排查失败原因
            print(f'Failed to send message to {member.name}: {str(e)}')
  • 特权意图未开启:除了代码里设置intents.members = True,你还需要在Discord开发者后台的「Bot」页面,找到「Privileged Gateway Intents」,手动开启Server Members Intent和Message Content Intent,否则机器人无法获取成员列表或读取命令。
完整修复后的代码示例
import discord
from discord.ext import commands
import requests

# 初始化Bot并配置意图
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)

def Bot_run():
    Token = input("DISCORD TOKEN : ")
    # 使用v10 API验证Token,机器人Token需要加'Bot '前缀
    auth_header = f'Bot {Token}' if client.bot else Token
    request_url = "https://discord.com/api/v10/users/@me"
    req = requests.get(request_url, headers={'authorization': auth_header})
    
    if req.status_code == 401:
        print(f"The token : {Token} is invalid")
        input("press enter to close")
        exit()
    if req.status_code == 200:
        print(f"The token : {Token} is valid")
        # 建议使用机器人账号,设置bot=True
        client.run(Token, bot=True)

@client.event
async def on_ready():
    print(f'status : online, logged in as {client.user}')

@client.command()
async def dmall(ctx,*,message):
    await ctx.message.delete()
    for member in ctx.guild.members:
        try:
            await member.send(message)
            print(f'Sent dm to: {member.name}')
        except Exception as e:
            print(f'Failed to send message to {member.name}: {str(e)}')

# 启动机器人
Bot_run()
额外提醒

用普通用户账号运行自动化工具违反Discord服务条款,大概率会导致账号被封禁,务必改用官方机器人账号!

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

相关产品推荐
方舟 Agent Plan

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

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