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

discord.py编写全服发消息命令时get_channel参数报错如何解决

问题修复方案

报错根因

discord.py 中的 bot.get_channel() 方法仅支持传入单个频道ID作为参数,你同时传入了2个频道ID,加上方法隐含的 self 参数,相当于总共传入了3个参数,因此触发参数数量不匹配的报错。

另外你的原始代码还存在逻辑不匹配问题:外层已经遍历所有服务器,但内层硬写固定频道ID,完全没用到遍历到的服务器对象,即使修复参数问题也会出现重复发送、其他服务器找不到对应频道的问题。

你可以根据实际需求选择以下两种修复方案:


方案1:仅给指定的两个固定ID频道发广播

如果你的需求就是给ID为 891652546853212161、805527975612252180 的两个频道发消息,不需要遍历所有服务器,代码如下:

@bot.command(pass_context=True)
async def brd(ctx, *, msg):
    # 把目标频道ID存入列表逐个查询
    target_channel_ids = [891652546853212161, 805527975612252180]
    for channel_id in target_channel_ids:
        channel = bot.get_channel(channel_id)
        if not channel:
            continue
        try:
            await channel.send(msg)
        except Exception:
            continue

方案2:给所有加入的服务器分别发广播

如果你的需求是给机器人加入的每个服务器都发广播,自动找每个服务器里第一个有权限发送消息的频道,代码如下:

@bot.command(pass_context=True)
async def brd(ctx, *, msg):
    # 遍历所有加入的服务器
    for guild in bot.guilds:
        # 优先取服务器默认系统通知频道
        target_channel = guild.system_channel
        # 没有系统频道就遍历所有文字频道找第一个有权限发消息的
        if not target_channel:
            for channel in guild.text_channels:
                if channel.permissions_for(guild.me).send_messages:
                    target_channel = channel
                    break
        if not target_channel:
            continue
        try:
            await target_channel.send(msg)
        except Exception:
            continue

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 08:24:02