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

新手上路,请多包涵

我希望我的机器人在 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 ')

原文由 Sébastien 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 645
1 个回答

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

 channel = client.get_channel(12324234183172)
await channel.send('hello')

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

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

How to get all text channels using discord.py?

如何向特定频道发送消息? 在 discord python FAQ 中。

原文由 Erty Seidohl 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题