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

使用Discord.py获取群聊成员为空且无法正常执行群聊指令的问题求助

Discord.py获取群聊成员为空且无法正常执行群聊指令的问题求助

大家好,我最近在用Discord.py开发一个小机器人,遇到了个棘手的问题:每次获取群聊的成员列表时,返回的都是空数组,执行相关指令直接抛出空序列的错误。我已经配置了Intents().all(),应该不是权限的问题?而且我尝试过给群聊发消息之类的操作,也没成功。

下面是我当前的代码:

import os
from random import choice
from discord import Client, Intents, Interaction, app_commands, ChannelType

intents = Intents().all()
app = Client(intents=intents)
tree = app_commands.CommandTree(
    client=app,
    allowed_contexts=app_commands.AppCommandContext(guild=True, dm_channel=True, private_channel=True),
    allowed_installs=app_commands.AppInstallationType(guild=True, user=True)
)

@app.event
async def on_ready():
    print(await tree.sync())
    if not app.user:
        raise Exception("Wrong.")
    print(f"logged in as {app.user.name}")

@tree.command(name="someone")
@app_commands.private_channel_only()
async def someone(interaction: Interaction):
    channel = interaction.channel
    if channel is None:
        return
    members = None
    match channel.type:
        case ChannelType.group:
            members = channel.recipients
        case ChannelType.private:
            members = channel.recipients
        case ChannelType.text:
            members = channel.guild.members
        case _:
            return
    got = choice(members)
    await interaction.response.send_message(got.mention)

app.run(os.environ['key'])

执行指令后,我得到了如下报错信息:

Traceback (most recent call last):
  File "/home/runner/workspace/.pythonlibs/lib/python3.11/site-packages/discord/app_commands/commands.py", line 858, in _do_call
    return await self._callback(interaction, **params) # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/workspace/main.py", line 37, in someone
    got = choice(members)
          ^^^^^^^^^^^^^^^
  File "/nix/store/clx0mcir7qw8zk36zbr4jra789g3knf6-python3-3.11.10/lib/python3.11/random.py", line 373, in choice
    raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/workspace/.pythonlibs/lib/python3.11/site-packages/discord/app_commands/tree.py", line 1310, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "/home/runner/workspace/.pythonlibs/lib/python3.11/site-packages/discord/app_commands/commands.py", line 883, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/workspace/.pythonlibs/lib/python3.11/site-packages/discord/app_commands/commands.py", line 876, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'someone' raised an exception: IndexError: Cannot choose from an empty sequence

我排查了好久,Intents已经开全了,机器人也在目标群聊里,为什么群聊的recipients始终是空的呢?有没有大佬能帮我分析一下问题出在哪,或者给个解决思路?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 11:29:32