简体   繁体   English

DM 上 discord.py 机器人启动

[英]DM on discord.py bot startup

I've got this code for when my bot code begins to be run.当我的机器人代码开始运行时,我得到了这段代码。 However, I'd also like it to DM me directly with a message saying "The bot is online" whenever the code begins running.但是,我也希望它在代码开始运行时直接向我发送一条消息,说“机器人在线”。 How to I do that?我该怎么做?

My Code:我的代码:

@bot.event
async def on_ready():
    activity = discord.Game(name="DM's being sent", type=3)
    await bot.change_presence(
        activity = discord.Activity(
            type = discord.ActivityType.watching,
            name = "Pokemon Go Discord Server!"
        )
    )
    channel = bot.get_channel(MY_CHANNEL_ID)
    await channel.send("Bot ready.")
    print("Ready.")

You can do it this way:你可以这样做:

@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.")

Where MY_ID is your discord ID (and integer value).其中MY_ID是您的 discord ID(和 integer 值)。 Also note that for this to work you'll have to allow server DMs and the bot has to be in the same server as you are.另请注意,要使其正常工作,您必须允许服务器 DM,并且机器人必须与您位于同一服务器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM