英文:
Why isnt discord.py member.join working for me?
问题
You're facing an issue where member.guild
is returning None
when the on_member_join()
event fires. This is likely due to your bot not having the necessary intents enabled to access the guild information.
You've already enabled the intents for members and messages, but you also need to enable the GUILD_MEMBERS
intent specifically. You can update your code like this:
intents = discord.Intents.default()
intents.members = True
intents.messages = True
intents.guilds = True # Add this line to enable the GUILD_MEMBERS intent
bot = commands.Bot(command_prefix='your_prefix_here', intents=intents)
By adding intents.guilds = True
, you should be able to access member.guild
correctly in the on_member_join()
event, and it should no longer return None
.
英文:
i want to get the guild that a member just joined so i know where to send the message to.
ex:
-member joins guild: spaceDruids
-bot sends member a message on guild spaceDruids
the problem is that when the on_member_join()
event fires, member.guild
does not return a guild. its blank.
heres my code to take a look:
import discord
from discord.ext import commands
TOKEN = 'token here'
intents = discord.Intents()
intents.members = True
intents.messages = True
bot = commands.Bot(command_prefix='🧀', intents = intents)
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to discord!!!')
@bot.event
async def on_member_join(member):
print('called')
print(member)
print(member.guild)
##await guild.text_channels[0].send('Welcome @{member.name}!!!')
bot.run(TOKEN)
i've looked at the discord documentation and i've done everything it wants!
i have the required intents in my code: intents.members = True
which discord says i need:
so why doesnt member.guild return anything like it says it should? it just returns blank. no error though
答案1
得分: 1
Discord要求 intents.guilds = True
才能返回 member.guild
。
在文档中,我没有看到这个要求,但我找到了解决方法。
文档说明 on_member_join(member)
需要 intents.members = True
才能返回成员。但它没有提到,要从 member
中获取 member.guild
在 on_member_join(member)
中,你还需要 intents.guilds
。
事后看来,我猜这可能有点显而易见,但文档中没有明确提到。
英文:
Apparently, Discord requires intents.guilds = True
to return member.guild
I don't see that as a requirement anywhere in the documentation but I figured it out.
Documentation says on_member_join(member)
requires intents.members = True
to return the member. what it does not say is that to get member.guild
from member
in on_member_join(member)
that you also need intents.guilds
In hindsight, I guess it's a little obvious it might need that but it doesn't explicitly say that anywhere in documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论