首页 > 解决方案 > Discord py bot 未触发 on_member_join 函数

问题描述

我的不和谐 py 机器人不会触发“on_member_join”事件。其他一切正常。我还有其他可以正常触发的事件方法。我在这里做错了什么?当人们加入时,它甚至不会打印控制台语句。

# Libs
import discord # Version 1.2.5
from discord.ext import commands

# set discord API token
token = "MASKED"


# Initiate used objects
bot = commands.Bot(command_prefix="!") 

@bot.event
async def on_member_join(member):
    print(f"{member} has joined the server.")
    for channel in member.server.channels:
        if str(channel) == "general":
            await bot.send_message(f"""Welcome to the server
                                   {member.mention}""")

bot.run(token)

标签: pythondiscorddiscord.py

解决方案


如果我是对的,问题是用户在您的机器人启动之前加入。假设您使用备用帐户加入服务器进行测试,您应该尝试在机器人启动后打印一条消息,然后加入服务器。您可以通过以下代码做到这一点。 https://discordpy.readthedocs.io/en/latest/api.html#discord.on_ready

#prints 'logged on' when bot is ready
@bot.event
async def on_ready():
    print('logged on')

因此,在打印登录后,您可以加入服务器并查看它是否有效。

我希望这能解决你的问题!


推荐阅读