首页 > 解决方案 > 弄清楚如何让 Discord Bot 在 Python 中发送 DM

问题描述

我正在尝试向我的 Discord 机器人添加一个命令,该命令通过 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')

我不断遇到的问题是“未定义名称'成员'”。我试过用会员、用户和用户替换会员,但我没有通过。我什至试图让机器人只用这个发送一个 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)

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

标签: pythondiscordbots

解决方案


CTX 具有 message 属性,具有 author 属性,可用于直接发送消息。

所以触发命令的人将是 ctx.message.author。

另外我不认为 pass_context=True 是必要的,但我可能会弄错。

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

推荐阅读