无法在成员加入时发送消息(discord.py)

时间:2020-06-10 08:10:25

标签: python-3.x discord discord.py

async def on_member_join(member):
    await bot.send_message(member, "Welcome !")

这是我的代码。当我的成员加入服务器时,我得到了这个信息:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Users\pauls\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/pauls/OneDrive/Bureau/Discord UHC/PandaBot/main.py", line 19, in on_member_join
    await bot.send_message(member, "Welcome !")
AttributeError: 'Bot' object has no attribute 'send_message'

有人可以帮我吗?

3 个答案:

答案 0 :(得分:0)

这是一个简单的代码,当有人加入服务器时,它将在“系统消息通道”上发送自定义消息!或者,如果没有系统消息通道,则需要将特定的通道ID放入channel_id_here

@client.event
async def on_member_join(member):
    try:
        channel = member.guild.system_channel
        await channel.send("welcome {} to {} ".format(str(member.mention),str(member.guild)))
    except:
        channel = member.guild.get_channel(channel_id_here)
        await channel.send("welcome {} to {} ".format(str(member.mention),str(member.guild)))

答案 1 :(得分:0)

如果您想直接向连接的用户发送消息,我认为这对您有帮助:

async def on_member_join(member):
    await member.send("Welcome!")

答案 2 :(得分:-1)

该代码有很多错误。首先,(member, "Welcome !")应该是(member + "Welcome !")。其次,bot.send_message是无效的;如果您使用的是重写版本,则应为bot.send,但即使这样,它也不起作用,因为它不知道将其发送到哪个渠道。

mguild = bot.get_guild(<id>)
if member.guild is not None and member.guild == mguild:
    channel = mguild.get_channel(<id>)
    if channel is not None:
        await channel.send(member + "Welcome!")

这将起作用,只需将<id>占位符替换为所需的ID。干杯!