【问题标题】:discord.py sending a new server member a direct messagediscord.py 向新的服务器成员发送直接消息
【发布时间】:2021-08-03 00:26:28
【问题描述】:

我的代码是:

import discord

class MyClient(discord.Client):

    async def on_ready(self):
        print("Bot ready")

    async def on_member_join(member):
        await member.send("nice to see u on the server")

client = MyClient()
client.run(My token)

但是消息没有发送。也许有人可以向我发送正确且有效的代码。感谢您的宝贵时间。

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

这是一个很好的问题,但是您可以通过查找找到更简单的结果。一个很好的入门来源是https://realpython.com/how-to-make-a-discord-bot-python/,它还可以回答您的问题。

#@bot or #@client
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, #message'
    )

【讨论】:

  • 这不是真正的问题,Member.send 内部调用 Member.create_dmMember.dm_channel.send
  • 除了@ŁukaszKwieciński 所说的之外,最好使用 try ,除非因为某些成员不允许 DM 会出错,如果您使用错误处理程序,它会捕获它。
猜你喜欢
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2021-01-28
相关资源
最近更新 更多