【问题标题】:Sending a DM to a new member向新成员发送 DM
【发布时间】:2020-12-18 04:08:44
【问题描述】:

我正在使用 discord.py 开发一个基本机器人,我需要弄清楚当新成员加入时如何发送 DM。当一个新用户加入时,它应该触发一个事件,该事件将获取成员并向他们发送一个欢迎他们到服务器的 DM。我已经尝试了很多代码变体以使其正常工作,但是当我使用 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,所以几年前的旧答案不起作用。我已经尝试了所有我能找到的东西。

【问题讨论】:

  • 这可能是因为某些成员关闭了直接 DM。在这种情况下,他们需要将您添加到他们的朋友列表中,您才能向他们发送 DM。
  • 您是否启用了intents.members

标签: discord.py


【解决方案1】:

你需要启用intents.members,我也看到了一些类似的问题,所以你研究的时候没有做好。

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

# If you're using commands.Bot
bot = commands.Bot(..., intents=intents)
# If you're using discord.Client
client = discord.Client(intents=intents)

还要确保在developer portal 中启用它们

【讨论】:

    【解决方案2】:

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

    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 异常

    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
    

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2022-01-10
      • 1970-01-01
      • 2021-04-30
      相关资源
      最近更新 更多