在 on_ready 中发送消息?Python不和谐机器人

Séb*_*ien 4 python bots python-asyncio discord

我希望我的机器人在 on_ready 事件中上线时发送一条消息。该行在 (on_message) 中工作,但我无法使其在 (on_ready) 中发送某些内容

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
    await message.channel.send('The bot is online ')
Run Code Online (Sandbox Code Playgroud)

Ert*_*ohl 6

您没有选择要向其发送消息的频道。首先,您需要选择一个频道,然后您可以向该频道发送消息。

channel = client.get_channel(12324234183172)
await channel.send('hello')
Run Code Online (Sandbox Code Playgroud)

您可以通过遍历连接服务器中的频道列表来获取频道 ID:

text_channel_list = []
for server in Client.servers:
    for channel in server.channels:
        if channel.type == 'Text':
            text_channel_list.append(channel)
Run Code Online (Sandbox Code Playgroud)

如何使用 discord.py 获取所有文本频道?

如何向特定频道发送消息?在不和谐的python常见问题解答中。