【问题标题】:Discord.py know if user exists in serverDiscord.py 知道用户是否存在于服务器中
【发布时间】:2021-07-24 13:06:11
【问题描述】:

很抱歉打扰大家,我知道这是重复的,但我没有评论的等级,所以我不得不再问一次,我看过其他一些关于这个的帖子,他们都说你手动获取公会 ID并在 on_ready() 中定义它:

@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 #

但是如何在 on_message() 中自动获取公会的 id,我希望我的机器人可以在各种服务器上运行,正如你所理解的那样,这不会真正起作用。

如果您需要更多上下文,它是一个您拥有货币并且可以在用户之间转移它们的机器人,我想检查 message.author 想要转移资金的用户是否真的存在。

提前致谢, 诺埃尔。

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    on_message(message),你可以通过message.guild获得公会:

    @client.event #Maybe you use client or maybe bot, then you would have to change this to whatever of them you have
    async def on_message(message):
        guild = message.guild
        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 #
    

    为了能够使用guild.get_member,您必须在bot 下的应用程序中的developer dashboard 中启用成员意图: 在代码顶部定义client,将client = ... 更改为:

    intents = discord.Intents.default()
    intents.members = True
    client = commands.Bot(... whatever you have here, intents=intents) #this line can be different for you!
    

    参考资料:

    【讨论】:

    • 我使用@client.event,有什么区别? ?
    • 没什么,只是插入client 而不是bot @noelpuig
    • 好像总是打印None,不管用户在不在服务器,我一直在用id,应该输入什么,id还是名字?
    • 您只需要输入会员的id。 @noelpuig
    • 好吧测试一下我一直在手动输入用户确实存在的代码guild = message.guildprint(guild.get_member(546937413684166676))并输出None
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2020-09-12
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多