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

discord.py通过ID向多频道批量发消息 404 Unknown User错误如何解决

问题解答

多频道批量发消息实现说明

  • 可以通过频道ID实现同时向多个频道发送消息,核心实现逻辑为整理目标频道ID列表,遍历调用发送接口即可,参考实现代码如下:
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

@bot.event
async def on_ready():
    # 替换为你实际要发送的目标频道ID列表
    target_channel_ids = [100000000000000001, 100000000000000002, 100000000000000003]
    send_content = "要批量发送的消息内容"
    
    for channel_id in target_channel_ids:
        channel = bot.get_channel(channel_id)
        # 校验频道有效性与bot权限
        if channel and channel.permissions_for(channel.guild.me).send_messages:
            await channel.send(send_content)
        else:
            print(f"频道ID {channel_id} 无法访问,跳过发送")
    print("批量消息发送完成")

bot.run("你的bot令牌")
  • 注意需提前开启bot对应的消息权限、服务器成员权限等必要网关意图,且保证bot在对应频道拥有发送消息权限。

现有报错问题排查

你遇到的Unknown User报错触发自main.py第17行的await bot.fetch_user(i)代码,报错原因及修复方案如下:

  • 报错原因:传入fetch_user方法的用户IDi无效,可能是ID输入错误、对应用户已删除账号,或该用户不在bot可访问的任意服务器范围内
  • 修复方案:
    1. 打印变量i的实际取值,校验是否为合法的18位左右纯数字Discord用户ID
    2. 增加异常捕获逻辑,避免单条无效ID打断整体流程:
try:
    member = await bot.fetch_user(i)
    # 原有后续业务逻辑
except discord.errors.NotFound:
    print(f"用户ID {i} 不存在,已跳过处理")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 23:00:03