discord.py 机器人启动时的 DM

时间:2021-01-04 09:42:30

标签: python discord discord.py

当我的机器人代码开始运行时,我有这个代码。但是,我也希望它在代码开始运行时直接向我发送一条消息“机器人在线”。我该怎么做?

我的代码:

import os

print(os.getenv('training'))

1 个答案:

答案 0 :(得分:1)

你可以这样做:

@bot.event
async def on_ready():
    await bot.change_presence(
        activity = discord.Activity(
            type = discord.ActivityType.watching,
            name = "Pokemon Go Discord Server!"
        )
    )

    member = await bot.fetch_user(MY_ID)
    await member.send("The bot is online")

    print("Ready.")

其中 MY_ID 是您的不和谐 ID(和整数值)。另请注意,要使其正常工作,您必须允许服务器 DM,并且机器人必须与您在同一台服务器中。