将DM发送给新成员 [英] Sending a DM to a new member

查看:62
本文介绍了将DM发送给新成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用discord.py开发一个基本的bot,我需要弄清楚当新成员加入时如何发送DM.当新用户加入时,它将触发一个事件,该事件将获取该成员并将其发送给DM,并将其发送给服务器.我已经尝试了许多代码变体来尝试使其正常工作,但是当我加入alt时什么也没发生.

I'm working on a basic bot with discord.py and I need to figure out how to send a DM when a new member joins. When a new user joins, it should trigger an event which will get the member and send them a DM welcoming them to the server. I've tried quite a few variations of code trying to get it to work but nothing happens when I join with an alt.

这是我的基本代码:

async def on_member_join(member):
    await member.create_dm()
    await member.send(
        f'Hi {member.name}! Welcome to the server!'
    )

我也尝试了没有 member.create_dm()的情况,因为文档说它应该自己做.我使用的是Python 3.7.3和discord.py 1.5.1,所以几年前的旧答案不起作用.我已经尝试了所有可以找到的东西.

I've also tried without the member.create_dm() because the docs said that it should do that on its own. I'm using Python 3.7.3 and discord.py 1.5.1 so old answers from a couple years ago don't work. I've tried everything that I can find.

推荐答案

从discord.py 1.5开始,为了接收成员加入事件,必须启用成员网关意图,您可以阅读有关此处.您必须首先在开发人员门户上启用成员意图,然后在构造客户端/机器人实例时,必须将其意图传递给它.

Starting with discord.py 1.5, in order to receive the member join event, you must enable the members gateway intent, which you can read about here. You must first enable to members intent on the developer portal, then when constructing your client/bot instance, you must pass your intents to it.

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(**other_options, intents=intents)
# Or if using the clinet
client = discord.Client(**other_options, intents=intents)

最后,您还应该确保处理如果成员关闭其DM可能引发的Forbidden异常

Finally, you should also be sure to handle the Forbidden exception that may be raised if the member has their DMs turned off

try:
    await member.send('Some text')
except discord.Forbidden:
    # The user had their DMs disabled, do whatever you need to
    # nothing in this case
    pass

这篇关于将DM发送给新成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆