[英]discord.py How to check if user is on server?
You can search a guild by ID for a member by ID using discord.Guild.get_member()
, which returns a discord.Member
object if found, and None
if not found.您可以使用discord.Guild.get_member()
按 ID 按 ID 搜索成员的行会,如果找到,则返回discord.Member
object,如果找不到,则返回None
。
@bot.event
async def on_ready():
guild = bot.get_guild(ID_OF_GUILD) # find ID by right clicking on server icon and choosing "copy id" at the bottom
if guild.get_member(ID_OF_MEMBER) is not None: # find ID by right clicking on a user and choosing "copy id" at the bottom
# the member is in the server, do something #
else:
# the member is not in the server, do something #
First of all, you need to define a guild by name or id, then fetch all members on a defined guild首先,您需要通过名称或id定义一个公会,然后获取定义的公会上的所有成员
Here's your code:这是你的代码:
@bot.event
async def on_ready():
guild = await bot.get_guild(ID) #fetch by ID
guild = discord.utils.get(bot.guilds, name="Foo") # fetch by name
for i in guild.members:
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.