簡體   English   中英

識別某人正在玩而不聊天的游戲(discord bot python)

[英]Recognizing a game that someone is playing without chatting(discord bot 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)

您需要查看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)

參考:

每當成員更改其活動時都會觸發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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM