python - 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)
解决方案
如果我是对的,问题是用户在您的机器人启动之前加入。假设您使用备用帐户加入服务器进行测试,您应该尝试在机器人启动后打印一条消息,然后加入服务器。您可以通过以下代码做到这一点。 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')
因此,在打印登录后,您可以加入服务器并查看它是否有效。
我希望这能解决你的问题!
推荐阅读
- python - Py2PDF PdfFileWriter - 拆分 PDF 是附加文件而不是保存自己的文件
- docker - Redis 在 minikube 中不使用配置文件
- powershell - 使用 ARM 模板和 DSC 部署 Azure VM 和用户
- ios - 如何通过 Xcode 在我的应用程序中使用 San Francisco Pro 字体?
- r - 为什么裁剪栅格堆栈会更改图层名称?
- google-sheets - 在谷歌电子表格中减去两列
- tensorflow - Keras 未导入:TypeError:只能将 str(不是“列表”)连接到 str?
- ruby - Ruby 脚本的部署失败
- visual-studio - 如何在 Visual Studio 2019 中将项目模板与项目类型相关联?
- python-3.x - Pandas Df 的 Not In 逻辑表达式