了解如何让Discord Bot在Python中发送DM

了解如何让Discord Bot在Python中发送DM,python,discord,bots,Python,Discord,Bots,我正在尝试向我的Discord bot添加一个命令,该命令通过DM发送机密信息。这就是我目前拥有的: import discord from discord.ext import commands @bot.command(name='password', help='DM's you your password') async def on_message(message): await member.create.dm() await member.dm_channel.s

我正在尝试向我的Discord bot添加一个命令,该命令通过DM发送机密信息。这就是我目前拥有的:

import discord
from discord.ext import commands

@bot.command(name='password', help='DM's you your password')
async def on_message(message):
    await member.create.dm()
    await member.dm_channel.send('password')
我经常遇到的问题是“未定义名称‘成员’”。我试着用member、user和user替换member,但还没有成功。我甚至尝试让bot发送一个DM,其中包含以下内容:

import discord
from discord.ext import commands

@bot.command(pass_context=True)
async def DM(ctx, user: discord.member, *, message=None):
    message = message or "This Message is sent via DM"
    await bot.send_message(user, message)

但它仍然引发了同样的错误。我做错了什么?

CTX有一个message属性,它有一个author属性,可以用来直接发送消息

因此触发命令的人将是ctx.message.author

我也不认为pass_context=True是必要的,但我可能弄错了

@bot.command()
async def DM(ctx):
    return await ctx.message.author.send("Henlo Werld!")

你不应该使用
send_message
,这很不协调。 因此,首先-更新discord库-
pip安装-U discord.py==1.4.2

你的问题至少有答案

对于未来的帮助,我建议加入

快乐编码