Discord.py on_member_join 和 on_member_leave 不起作用 [英] Discord.py on_member_join and on_member_leave not working
问题描述
我开始使用 discord.py
(不是 discord.ext
命令,只有 import discord
).最近,我做了一个频道,名字显示了公会的成员数,每次有人加入或离开时都会更新.这是我的代码:
I started using discord.py
(not discord.ext
commands, only import discord
). Recently, I made a channel, the name of which shows the member count in the guild, and it updates everytime someone joins or leaves. This is my code:
import discord
client = discord.Client()
@client.event
async def on_ready():
print("Bot is ready")
@client.event
async def on_member_join(member):
channel = client.get_channel('channel id here')
await channel.edit(name = 'Member count: {}'.format(channel.guild.member_count()))
@client.event
async def on_member_leave(member):
channel = client.get_channel('channel id here')
await channel.edit(name = 'Member count: {}'.format(channel.guild.member_count()))
client.run('my token here')
我还添加了 client.on_message
命令,以便机器人将该名称编辑为我输入的任何内容.
I also added client.on_message
command so the bot would edit that name to whatever I typed in.
@client.event
async def on_message(message)
if message.content == 'rename channel':
channel = client.get_channel('channel id here')
await channel.edit(name = 'TEST')
现在,在添加了一些用于调试的打印后,我发现 on_member_join()
和 on_member_leave()
永远不会被调用,但是当我键入命令.那是一个语音频道,显示会员数量.没有任何错误.我读错 API 了吗?请帮忙
Now, after adding some print's for debugging, I found out that on_member_join()
and on_member_leave()
never get called, but the bot edits the channel's name when I type the command. That's a voice channel, which shows member count. There aren't any errors. Did I read the API wrong? Please help
推荐答案
我不完全确定,但你的 on_member_join
和 on_member_leave
不工作的原因可能是因为意图未通过.
I am not entirely sure, but the reason behind your on_member_join
and on_member_leave
not working maybe because of intents not being passed.
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
然后您想在机器人应用程序中启用服务器意图.我在制作机器人时也遇到了这个问题.
and then you want to enable server intents in the bot application. I faced this issue when I made my bot as well.
这篇关于Discord.py on_member_join 和 on_member_leave 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!