简体   繁体   English

Discord.py 私信

[英]Discord.py private messages

I am trying to make a discord bot that sends a DM to someone with a command like:我正在尝试制作一个 discord 机器人,它使用以下命令向某人发送 DM:

!messagesteve hello world

and it would send a message directly to the person I want.它会直接向我想要的人发送消息。

I've tried the following, but to no avail:我尝试了以下方法,但无济于事:

@client.command(pass_context=True)
async def dm(ctx):
    user=await client.get_user_info("381870129706958")
    await client.send_message(user, "test")

Any help is appreciated.任何帮助表示赞赏。

It seems as though you're using the old docs of d.py (v0.16.x).好像您正在使用 d.py (v0.16.x) 的旧文档。 The most recent version is on rewrite (v1.x).最新版本正在重写 (v1.x)。

One of the changes, amongst others , is that context is automatically implied (you don't need pass_context=True ) and the syntax for sending messages has changed, as you'll see in the example:其中一项更改是自动隐含上下文(您不需要pass_context=True )并且发送消息的语法已更改,您将在示例中看到:

@client.command()
async def dm(ctx, member: discord.Member, *, message):
    try:
        await member.send(message)
    except: # this will error if the user has blocked the bot or has server dms disabled
        await ctx.send("Sorry, that user had DMs disabled!")
    else:
        await ctx.send(f"Successfully sent {member} a DM!")

The usage, assuming a prefix of !用法,假设前缀为! , the usage of the command will be: ,该命令的用法将是:
,dm @Skyonimous Hey there, I'm DMing you from a bot!

* in the arguments "consumes rest", which means that the argument that succeeds it ( message ) will act as one whole argument, no matter the number of spaces, so you can send a whole paragraph to a user if you want! *在 arguments “consumes rest”中,这意味着它后面的参数( message )将作为一个完整的参数,不管空格的数量,所以如果你愿意,你可以将整个段落发送给用户!

If there's anything that you want me to clarify, I'll be more than happy to!如果您有什么需要我澄清的,我将非常乐意!


References:参考:

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

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