python - Discord.py 机器人不会 dm 新成员
问题描述
我正在使用事件 on_member_join 来尝试 dm 新成员,但是当我使用我的 alt 进行测试时,它没有发送消息。
@bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
channel = bot.get_channel(803703488499810326)
userDM = member.create_dm()
await userDM.send(embed=embed)
解决方案
您不需要这样做,member.create_dm()
因为当您 DM 用户时不需要这样做。您也不需要定义description
为 None,您甚至不需要指定描述。
为了让机器人甚至可以找到成员,您需要在机器人中打开成员意图。您可以使用下图来做到这一点。
现在,您只需要在机器人中提供代码,以便机器人可以使用意图。这是您需要添加到主机器人文件的代码。
intents = discord.Intents.default() # Gets the default intents from discord.
intents.members = True # enables members intents on the bot.
bot = commands.Bot(command_prefix=prefix, intents=intents)
毕竟这里是你的固定代码。
@bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
await member.send(embed=embed)
我希望这会有所帮助,祝你有美好的一天,祝你的机器人好运。这是面向初学者的 Discord 服务器。
Discord.py 初学者:https ://discord.gg/C8zFM3D2rn
推荐阅读
- python - 如何找到匹配类型的套装并找到总成本而忽略最便宜套装的成本
- scala - 在 Scala 中对集合执行元素明智操作的并行函数
- r - effects::effect 无法识别 purrr::map 中的数据
- android - 在不同的按钮上播放不同的声音
- reactjs - 在下次按下时反应本机焦点文本输入
- javacc - 用于单行注释的 JavaCC 正则表达式
- gitlab - 如果删除构建目录,Gitlab 作业将失败
- python - 从 C++ 嵌入式解释器捕获 python 窗口输出
- docker - 使用 Ubuntu 和 Mac 的 Docker Swarm 模式
- django - django 中的模板标签与自定义模板标签和过滤器