python - 弄清楚如何让 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)
但它仍然会引发同样的错误。我究竟做错了什么?
解决方案
CTX 具有 message 属性,具有 author 属性,可用于直接发送消息。
所以触发命令的人将是 ctx.message.author。
另外我不认为 pass_context=True 是必要的,但我可能会弄错。
@bot.command()
async def DM(ctx):
return await ctx.message.author.send("Henlo Werld!")
推荐阅读
- python - 更改数据框中的特定单元格 - 复制错误
- python - 在新的 DataFrame() 中更快地重新组织我的数据
- android - 如何在静音选项上修复通知 setSound
- html - 浏览器大小更改时图像无法与文本正确对齐 - 有时会从屏幕上消失
- python - 有没有一种方法可以将整个 jupyter 笔记本的输出保存到 txt 文件或类似文件中,而不使用命令行,而是从笔记本内部?
- selenium - 如何在运行中从 Perfecto 中选择可用设备
- angular - 创建 observable 以返回 csv 值
- rest - 如何使用 softlayer Rest API 在 IBM 云中使用数据磁盘启动计算实例
- installation - Sas Add-in for Microsoft office, installation error in office 365 / windows10 : Culture is not supported
- javascript - 您如何在 cucumber-js 测试之间重用 selenium webdriver 浏览器实例?