繁体   English   中英

弄清楚如何让 Discord Bot 在 Python 中发送 DM

[英]Figuring out how to make a Discord Bot send a DM in Python

我正在尝试向我的 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)

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

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 ,它是send_message旧版本 所以,首先 - 更新你的不和谐库 - pip install -U discord.py==1.4.2

您的问题在常见问题中有答案

为了将来的帮助,我建议加入Discord 支持服务器

快乐编码!

暂无
暂无

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

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