【问题标题】:Recognizing a game that someone is playing without chatting(discord bot python)在不聊天的情况下识别某人正在玩的游戏(discord bot python)
【发布时间】:2020-09-03 10:57:41
【问题描述】:

(不和谐机器人python)

代码是,如果有人聊天,如果这个人在玩守望先锋,他或她将被提升为玩家角色,如果没有,他或她将被删除或什么都不会发生。但我正在寻找一种方法来识别正在玩的游戏而无需聊天。有人可以帮我吗?

@client.event
async def on_message(message):
    man = message.author.activity.name
    role = discord.utils.get(message.guild.roles, name="Gamer")
    if man == "Overwatch":
        await message.author.add_roles(role)
    else:
        await message.author.remove_roles(role)

【问题讨论】:

    标签: python discord


    【解决方案1】:

    每当成员更改其活动时,都会触发on_member_update 事件:

    @client.event
    async def on_member_update(before, after)
        role = discord.utils.get(after.guild.roles, name="Gamer")
        if before.activities != after.activities:
            name = "Overwatch"
            before_name = any(activity.name for activity in before.activities)
            after_name = any(activity.name for activity in after.activities)
            if not before_name and after_name:
                await after.add_roles(role)
            elif before_name and not after_name:
                await after.remove_roles(role)
    

    【讨论】:

    • 我应该使用什么导入?
    【解决方案2】:

    您需要查看on_member_update() 事件。像这样:

    @client.event
    async def on_member_update(prev, cur):
        role = discord.utils.get(cur.guild.roles, name="Gamer")
        games = ["overwatch", "rocket league", "minecraft"]
        # make sure game titles are lowercase
    
        if cur.activity and cur.activity.name.lower() in games:
                await cur.add_roles(role)
    
        # only add the rest if you want them to have the role for the duration of them
        # playing a game
        elif prev.activity and prev.activity.name.lower() in games and not cur.activity:
                if role in cur.roles: # check they already have the role, as to not throw an error
                    await cur.remove_roles(role)
    

    参考资料:

    【讨论】:

    • 我应该使用什么导入?
    • 除了您当前拥有的之外,不再需要其他导入。只要你安装了 discord.py 就可以了。
    • 当我更改我的状态```忽略 on_member_update Traceback 中的异常(最近一次调用最后一次)时发生这样的错误:文件“C:\Users\Godster\AppData\Local\Programs\Python\Python37 \lib\site-packages\discord\client.py”,第 270 行,在 _run_event 中等待 coro(*args, **kwargs) 文件“C:/Users/Godster/Desktop/sadas/sadas.py”,第 164 行,在 on_member_update if cur.activity.name.lower() in games: AttributeError: 'NoneType' object has no attribute 'name' ```我该如何解决这个问题?
    • @g0dst3r 编辑了我的答案
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2021-01-10
    • 2021-06-28
    • 2020-07-28
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多