提问者:小点点

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


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

共3个答案

匿名用户

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

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)

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

从 如何向特定渠道发送消息?在 Discord Python FAQ 中。

匿名用户

discord.py似乎没有把变量作为通道

@client.event
async def on_ready():
   await client.get_channel("enter channel id here").send("bot is online")

我刚试过,效果很好

匿名用户

复制您的user_id并粘贴而不加引号

    @client.event
    async def on_ready():
        print("The bot is ready")
        print("------------------------")
        user = await client.fetch_user("User id")
        await user.send("hello")