[Python] Cannot send messages to group chat using Discord.py

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    This is my current code: Every time I get a group chat's recipients, it returns an empty array. I also have tried sending messages to group chats, and all of the sorts. I have already handled Intents, so that's not the problem.

    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


    This is the traceback, if you are interested.

    Continue reading...

Compartilhe esta Página