【问题标题】:discord.py give role on game activitydiscord.py 在游戏活动中扮演角色
【发布时间】:2021-05-17 02:06:04
【问题描述】:

我正在尝试制作一个机器人,它可以在有人玩游戏时提供角色。 例如有人玩 CSGO,机器人会在玩游戏时给他 CSGO 角色。 我好像搞不懂。

@client.event
async def on_member_update(before, after):
    if after.activity != before.activity:
        role = discord.utils.get(after.guild.roles, name=after.activity)
        if role == None:
            await create_role(name=after.activity, hoist=True)
        await after.add_roles(role)
    elif before.activity != after.activity and not after.activity:
        role = discord.utils.get(after.guild.roles, name=after.activity)
        if role in after.roles: 
            await after.remove_roles(role)

【问题讨论】:

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


    【解决方案1】:

    您可以在on_member_update 活动中查看会员正在玩的游戏。为此,您需要检查Member.activity 参数。

    应该是这样的:

    @client.event
    async def on_member_update(before, after):
        games = ["game1", "game2", "game3"]
        if after.activity and after.activity.name.lower() in games:
                # Assuming the role name is the same as the game
                role = discord.utils.get(after.guild.roles, name=after.activity)
                await after.add_roles(role)
        elif before.activity and before.activity.name.lower() in games and not after.activity:
                role = discord.utils.get(after.guild.roles, name=after.activity)
                if role in after.roles: 
                    await after.remove_roles(role)
    

    参考资料:

    【讨论】:

    • 我希望机器人创建角色,然后在没有成员活动后将其删除
    • 我更新了代码,添加了必要的elif 语句,以便在活动为无时检查和删除角色。
    • 有没有办法做到这一点而不必声明游戏变量?所以机器人会看到游戏活动名称并声明它自己
    • 您可以让机器人直接分配与activity 对应的角色,或者如果它不存在则创建它,但我不建议这样做,因为人们倾向于创建自定义活动并且您会结束最多有数百个无用的角色。
    • 我该怎么做呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2017-06-12
    • 2011-01-08
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多