【问题标题】:How to use discord.Member.mobile_status in discord.py如何在 discord.py 中使用 discord.Member.mobile_status
【发布时间】:2023-01-06 21:24:05
【问题描述】:

首先,这可能是一个奇怪的句子,因为我是用翻译器写下来的,但请理解。 我是第一次使用 discord.py 开发个人服务器机器人。 我的问题是,当用户通过移动设备加入服务器时,我想分配一个角色,但我不确定如何分配。 这可能是一个非常愚蠢的故事,但我会很感激任何帮助。 我想知道与discord.Member.is_on_mobile.的区别

这是我预期的代码。我将衷心感谢您的帮助。

async def shutup_mobile(message : discord.Message, member : discord.Member) :
        if discord.Member.mobile_status.online :
            # When someone joins voice_channel on mobile
            # Assign role A
            # After assigning a role, I want to send a sentence to a specific text_channel
            await message.channel.send(f'{discord.Member.name} is don't want playing game with us')

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    The doc's for on is_on_mobile

    我觉得你的整个功能都是错误的 - 如果用户正在加入语音频道,为什么你有一个 message 参数?您应该使用 on_voice_update 客户端事件。显然,您可能正在调用您从那里提供的函数 - 但 message 对象似乎是错误的。

    @client.event
    async def on_voice_update(member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):
        # before and after are voice state objects - can be used to check if a user has joined/left a vc
    
        if before.channel:
            # user is already in a channel
            return
        
        # user has joined a voice channel
        if member.is_on_mobile():
            # user is on mobile - do our thing here
            guild = after.channel.guild  # get the guild object
            role_to_apply = guild.get_role(THE_ROLE_ID_YOU_WANT_GIVE)
            await member.add_roles(role_to_apply)
            the_text_channel = guild.get_channel(THE_CHANNEL_ID_YOU_WANT_TO_SEND_MESSAGE_TO)
            await the_text_channel.send(f"{member.name} is don't want playing game with us")
       
    

    要添加角色 - 您可以在 discord.Member 上使用 add_roles 方法。

    您似乎还试图在尚未实例化的类上调用方法。使用 discord.Member.name 将失败,因为它不是该类的实例 - 您已经在函数参数中有一个它的实例 member,因此您应该直接使用 member.name。我已经修复了这个问题以及我的示例中的类似事件。也许考虑阅读classes in Python

    希望这足以让你继续前进。也可以扩展为在用户离开时删除角色等。

    【讨论】:

      【解决方案2】:

      discord.Member.mobile_status 属性返回 discord.Status。如果用户不在移动设备上/处于离线状态,它会返回 discord.Status.offline。如果他们在移动设备上,它会返回用户的当前状态。

      根据文档,discord.Member.is_on_mobile 返回 boolean

      除了 ESloman 指出的问题之外,discord.Member.mobile_status.online 没有任何意义。反而,

      if member.mobile_status == discord.Status.online:
          #do something
      

      这只会在成员在移动设备上并且状态为discord.Status.online时执行某些操作

      您应该使用 member 而不是 discord.Member,因为您将 member 作为参数,而 discord.Member 只是一个类。

      如果你想检查用户是否在移动设备上,

      if member.is_on_mobile():
          #do something
      

      discord.Member.mobile_status, discord.Member.is_on_mobile

      【讨论】:

      • 感谢您的进一步解释! is_on_mobile 是一种方法而不是属性 - 所以你的最后一个代码块应该是 if member.is_on_mobile():
      猜你喜欢
      • 2021-04-25
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      相关资源
      最近更新 更多