Пример #1
0
async def updateRoles(client=None,
                      member=None,
                      server_default=None,
                      refresh_perms=True,
                      remove_or_apply_flag=None):
    time_now = int(time.time())

    if server_default != None:
        user_data = EwUser(id_user=member.id, id_server=server_default)
    else:
        user_data = EwUser(member=member)

    id_server = user_data.id_server

    if member == None:
        return ewutils.logMsg("error: member was not supplied for updateRoles")

    #roles_map = ewutils.getRoleMap(member.guild.roles)
    roles_map_user = ewutils.getRoleIdMap(member.roles)

    if user_data.life_state != ewcfg.life_state_kingpin and ewcfg.role_kingpin in roles_map_user:
        # Fix the life_state of kingpins, if somehow it wasn't set.
        user_data.life_state = ewcfg.life_state_kingpin
        user_data.persist()

    elif user_data.life_state != ewcfg.life_state_grandfoe and ewcfg.role_grandfoe in roles_map_user:
        # Fix the life_state of a grand foe.
        user_data.life_state = ewcfg.life_state_grandfoe
        user_data.persist()

    faction_roles_remove = [
        ewcfg.role_juvenile,
        ewcfg.role_juvenile_active,
        ewcfg.role_juvenile_pvp,
        ewcfg.role_rowdyfuckers,
        ewcfg.role_rowdyfuckers_pvp,
        ewcfg.role_rowdyfuckers_active,
        ewcfg.role_copkillers,
        ewcfg.role_copkillers_pvp,
        ewcfg.role_copkillers_active,
        ewcfg.role_corpse,
        ewcfg.role_corpse_pvp,
        ewcfg.role_corpse_active,
        ewcfg.role_kingpin,
        ewcfg.role_grandfoe,
        ewcfg.role_slimecorp,
        ewcfg.role_tutorial,
        ewcfg.role_shambler,
    ]

    # Manage faction roles.
    faction_role = ewutils.get_faction(user_data=user_data)

    faction_roles_remove.remove(faction_role)

    pvp_role = None
    active_role = None
    if faction_role in ewcfg.role_to_pvp_role:

        if user_data.time_expirpvp >= time_now:
            pvp_role = ewcfg.role_to_pvp_role.get(faction_role)
            faction_roles_remove.remove(pvp_role)

        # if ewutils.is_otp(user_data):
        # 	active_role = ewcfg.role_to_active_role.get(faction_role)
        # 	faction_roles_remove.remove(active_role)

    tutorial_role = None
    if user_data.poi in ewcfg.tutorial_pois:
        tutorial_role = ewcfg.role_tutorial
        faction_roles_remove.remove(tutorial_role)

    # Manage location roles.
    user_poi = ewcfg.id_to_poi.get(user_data.poi)
    #print(user_poi.id_poi)
    if user_poi != None:
        # poi_role = user_poi.role
        poi_major_role = user_poi.major_role
        poi_minor_role = user_poi.minor_role
        poi_permissions = user_poi.permissions
    else:
        # poi_role = None
        poi_major_role = None
        poi_minor_role = None
        poi_permissions = None

    poi_permissions_remove = []
    for poi in ewcfg.poi_list:
        if poi.permissions != None and poi.permissions != poi_permissions:
            poi_permissions_remove.append(poi.id_poi)

    poi_roles_remove = []
    for poi in ewcfg.poi_list:
        if poi.major_role != None and poi.major_role != poi_major_role:
            poi_roles_remove.append(poi.major_role)
        if poi.minor_role != None and poi.minor_role != poi_minor_role:
            poi_roles_remove.append(poi.minor_role)

    misc_roles_remove = [ewcfg.role_gellphone, ewcfg.role_slimernalia]

    # Remove user's gellphone role if they don't have a phone
    role_gellphone = None
    gellphones = ewitem.find_item_all(item_search=ewcfg.item_id_gellphone,
                                      id_user=user_data.id_user,
                                      id_server=user_data.id_server,
                                      item_type_filter=ewcfg.it_item)
    gellphone_active = False

    for phone in gellphones:
        phone_data = ewitem.EwItem(id_item=phone.get('id_item'))
        if phone_data.item_props.get('active') == 'true':
            gellphone_active = True
            break

    if gellphone_active == True:
        role_gellphone = ewcfg.role_gellphone
        misc_roles_remove.remove(ewcfg.role_gellphone)

    role_slimernalia = None
    #if user_data.slimernalia_kingpin == True:
    #	role_slimernalia = ewcfg.role_slimernalia
    #	misc_roles_remove.remove(ewcfg.role_slimernalia)

    role_ids = []
    for role_id in roles_map_user:

        try:
            role_data = EwRole(id_server=id_server, id_role=role_id)
            roleName = role_data.name
            if roleName != None and roleName not in faction_roles_remove and roleName not in misc_roles_remove and roleName not in poi_roles_remove and role_data.id_role != '':
                role_ids.append(int(role_data.id_role))
        except:
            ewutils.logMsg(
                'error: couldn\'t find role with id {}'.format(role_id))

    try:
        role_data = EwRole(id_server=id_server, name=faction_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find faction role {}'.format(faction_role))

    try:
        role_data = EwRole(id_server=id_server, name=pvp_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find pvp role {}'.format(pvp_role))

    try:
        role_data = EwRole(id_server=id_server, name=active_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find active role {}'.format(active_role))

    try:
        role_data = EwRole(id_server=id_server, name=tutorial_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find tutorial role {}'.format(tutorial_role))

    try:
        major_role_data = EwRole(id_server=id_server, name=poi_major_role)
        if not major_role_data.id_role in role_ids and major_role_data.id_role != '':
            role_ids.append(int(major_role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find major role {}'.format(poi_major_role))

    try:
        minor_role_data = EwRole(id_server=id_server, name=poi_minor_role)
        if not minor_role_data.id_role in role_ids and minor_role_data.id_role != '':
            role_ids.append(int(minor_role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find minor role {}'.format(poi_minor_role))

    try:
        role_data = EwRole(id_server=id_server, name=role_gellphone)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find gellphone role {}'.format(role_gellphone))

    try:
        role_data = EwRole(id_server=id_server, name=role_slimernalia)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find slimernalia role {}'.format(
            role_slimernalia))

    # if faction_role not in role_names:
    # 	role_names.append(faction_role)
    # if poi_role != None and poi_role not in role_names:
    # 	role_names.append(poi_role)

    #replacement_roles = []
    #for name in role_names:
    #	role = roles_map.get(name)

    #	if role != None:
    #		replacement_roles.append(role)
    #	else:
    #		ewutils.logMsg("error: role missing \"{}\"".format(name))

    #ewutils.logMsg('looking for {} roles to replace'.format(len(role_ids)))
    replacement_roles = []

    for role in member.guild.roles:
        if role.id in role_ids:
            # ewutils.logMsg('found role {} with id {}'.format(role.name, role.id))
            replacement_roles.append(role)

    #ewutils.logMsg('found {} roles to replace'.format(len(replacement_roles)))

    try:
        time_now = int(time.time())
        was_pvp = user_data.time_expirpvp > time_now
        user_is_pvp = False

        role_ids = []
        for pvp_role in ewcfg.role_to_pvp_role.values():
            role = EwRole(id_server=member.guild.id, name=pvp_role)
            role_ids.append(role.id_role)

            for role in member.roles:
                if role.id in role_ids:
                    user_is_pvp = True
                    break

        if remove_or_apply_flag != None:

            if was_pvp:
                if remove_or_apply_flag == 'apply':
                    if not user_is_pvp:
                        # ewutils.logMsg('applying flag...')
                        await member.edit(roles=replacement_roles)
            elif not was_pvp:
                if remove_or_apply_flag == 'remove':
                    # ewutils.logMsg('removing flag...')
                    await member.edit(roles=replacement_roles)
        else:
            await member.edit(roles=replacement_roles)

    except:
        ewutils.logMsg('error: failed to replace roles for {}'.format(
            member.display_name))

    if refresh_perms:
        await refresh_user_perms(client=client,
                                 id_server=id_server,
                                 used_member=member)
Пример #2
0
async def updateRoles(client=None,
                      member=None,
                      server_default=None,
                      refresh_perms=True,
                      remove_or_apply_flag=None):
    time_now = int(time.time())

    if server_default != None:
        user_data = EwUser(id_user=member.id, id_server=server_default)
    else:
        user_data = EwUser(member=member)

    id_server = user_data.id_server

    if member == None:
        return ewutils.logMsg("error: member was not supplied for updateRoles")

    #roles_map = ewutils.getRoleMap(member.guild.roles)
    roles_map_user = ewutils.getRoleIdMap(member.roles)

    user_poi = ewcfg.id_to_poi.get(user_data.poi)

    if user_data.life_state != ewcfg.life_state_kingpin and ewcfg.role_kingpin in roles_map_user:
        # Fix the life_state of kingpins, if somehow it wasn't set.
        user_data.life_state = ewcfg.life_state_kingpin
        user_data.persist()

    elif user_data.life_state != ewcfg.life_state_grandfoe and ewcfg.role_grandfoe in roles_map_user:
        # Fix the life_state of a grand foe.
        user_data.life_state = ewcfg.life_state_grandfoe
        user_data.persist()

    faction_roles_remove = [
        ewcfg.role_juvenile,
        ewcfg.role_juvenile_active,
        ewcfg.role_juvenile_pvp,
        ewcfg.role_rowdyfuckers,
        ewcfg.role_rowdyfuckers_pvp,
        ewcfg.role_rowdyfuckers_active,
        ewcfg.role_slimecorp,
        ewcfg.role_slimecorp_pvp,
        ewcfg.role_slimecorp_active,
        ewcfg.role_copkillers,
        ewcfg.role_copkillers_pvp,
        ewcfg.role_copkillers_active,
        ewcfg.role_corpse,
        ewcfg.role_corpse_pvp,
        ewcfg.role_corpse_active,
        ewcfg.role_kingpin,
        ewcfg.role_grandfoe,
        ewcfg.role_executive,
        ewcfg.role_tutorial,
        ewcfg.role_shambler,
    ]

    # Manage faction roles.
    faction_role = ewutils.get_faction(user_data=user_data)

    faction_roles_remove.remove(faction_role)

    non_wanted_pois = [
        ewcfg.poi_id_copkilltown, ewcfg.poi_id_rowdyroughhouse,
        ewcfg.poi_id_juviesrow, ewcfg.poi_id_thebreakroom, ewcfg.poi_id_mine,
        ewcfg.poi_id_mine_bubble, ewcfg.poi_id_mine_sweeper,
        ewcfg.poi_id_juviesrow_pier, ewcfg.poi_id_jr_farms
    ]

    pvp_role = None
    active_role = None
    if faction_role in ewcfg.role_to_pvp_role:

        if not user_poi.is_apartment and \
        user_poi.id_poi not in non_wanted_pois and \
        (user_data.life_state != ewcfg.life_state_juvenile or user_data.slimelevel > ewcfg.max_safe_level):
            pvp_role = ewcfg.role_to_pvp_role.get(faction_role)
            faction_roles_remove.remove(pvp_role)

        # if ewutils.is_otp(user_data):
        # 	active_role = ewcfg.role_to_active_role.get(faction_role)
        # 	faction_roles_remove.remove(active_role)

    tutorial_role = None
    if user_data.poi in ewcfg.tutorial_pois:
        tutorial_role = ewcfg.role_tutorial
        faction_roles_remove.remove(tutorial_role)

    # Manage location roles.
    if user_poi != None:
        # poi_role = user_poi.role
        poi_major_role = user_poi.major_role
        poi_minor_role = user_poi.minor_role
        poi_permissions = user_poi.permissions
    else:
        # poi_role = None
        poi_major_role = None
        poi_minor_role = None
        poi_permissions = None

    poi_permissions_remove = []
    for poi in ewcfg.poi_list:
        if poi.permissions != None and poi.permissions != poi_permissions:
            poi_permissions_remove.append(poi.id_poi)

    poi_roles_remove = []
    for poi in ewcfg.poi_list:
        if poi.major_role != None and poi.major_role != poi_major_role:
            poi_roles_remove.append(poi.major_role)
        #if poi.minor_role != None and poi.minor_role != poi_minor_role:
        poi_roles_remove.append(poi.minor_role)

    misc_roles_remove = [ewcfg.role_gellphone, ewcfg.role_slimernalia]

    # Remove user's gellphone role if they don't have a phone
    role_gellphone = None

    if user_data.has_gellphone():
        role_gellphone = ewcfg.role_gellphone
        misc_roles_remove.remove(ewcfg.role_gellphone)

    role_slimernalia = None
    if user_data.slimernalia_kingpin == True:
        role_slimernalia = ewcfg.role_slimernalia
        misc_roles_remove.remove(ewcfg.role_slimernalia)

    role_ids = []
    for role_id in roles_map_user:

        try:
            role_data = EwRole(id_server=id_server, id_role=role_id)
            roleName = role_data.name
            if roleName != None and roleName not in faction_roles_remove and roleName not in misc_roles_remove and roleName not in poi_roles_remove and role_data.id_role != '':
                role_ids.append(int(role_data.id_role))
        except:
            ewutils.logMsg(
                'error: couldn\'t find role with id {}'.format(role_id))

    try:
        role_data = EwRole(id_server=id_server, name=faction_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find faction role {}'.format(faction_role))

    try:
        role_data = EwRole(id_server=id_server, name=pvp_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find pvp role {}'.format(pvp_role))

    try:
        role_data = EwRole(id_server=id_server, name=active_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find active role {}'.format(active_role))

    try:
        role_data = EwRole(id_server=id_server, name=tutorial_role)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find tutorial role {}'.format(tutorial_role))

    # poi roles are disabled
    try:
        major_role_data = EwRole(id_server=id_server, name=poi_major_role)
        if not major_role_data.id_role in role_ids and major_role_data.id_role != '':
            role_ids.append(int(major_role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find major role {}'.format(poi_major_role))

    #try:
    #	minor_role_data = EwRole(id_server = id_server, name = poi_minor_role)
    #	if not minor_role_data.id_role in role_ids and minor_role_data.id_role != '':
    #		role_ids.append(int(minor_role_data.id_role))
    #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    #except:
    #	ewutils.logMsg('error: couldn\'t find minor role {}'.format(poi_minor_role))

    try:
        role_data = EwRole(id_server=id_server, name=role_gellphone)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg(
            'error: couldn\'t find gellphone role {}'.format(role_gellphone))

    try:
        role_data = EwRole(id_server=id_server, name=role_slimernalia)
        if not role_data.id_role in role_ids and role_data.id_role != '':
            role_ids.append(int(role_data.id_role))
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find slimernalia role {}'.format(
            role_slimernalia))

    # if faction_role not in role_names:
    # 	role_names.append(faction_role)
    # if poi_role != None and poi_role not in role_names:
    # 	role_names.append(poi_role)

    #replacement_roles = []
    #for name in role_names:
    #	role = roles_map.get(name)

    #	if role != None:
    #		replacement_roles.append(role)
    #	else:
    #		ewutils.logMsg("error: role missing \"{}\"".format(name))

    #ewutils.logMsg('looking for {} roles to replace'.format(len(role_ids)))
    replacement_roles = []

    for role in member.guild.roles:
        if role.id in role_ids:
            # ewutils.logMsg('found role {} with id {}'.format(role.name, role.id))
            replacement_roles.append(role)

    #ewutils.logMsg('found {} roles to replace'.format(len(replacement_roles)))

    try:
        await member.edit(roles=replacement_roles)
    except Exception as e:
        ewutils.logMsg('error: failed to replace roles for {}:{}'.format(
            member.display_name, str(e)))

    if refresh_perms:
        await refresh_user_perms(client=client,
                                 id_server=id_server,
                                 used_member=member)
Пример #3
0
async def updateRoles(client=None, member=None, server_default=None):
    time_now = int(time.time())

    if server_default != None:
        user_data = EwUser(id_user=member.id, id_server=server_default)
    else:
        user_data = EwUser(member=member)

    id_server = user_data.id_server

    if member == None:
        return ewutils.logMsg("error: member was not supplied for updateRoles")

    #roles_map = ewutils.getRoleMap(member.server.roles)
    roles_map_user = ewutils.getRoleIdMap(member.roles)

    if user_data.life_state != ewcfg.life_state_kingpin and ewcfg.role_kingpin in roles_map_user:
        # Fix the life_state of kingpins, if somehow it wasn't set.
        user_data.life_state = ewcfg.life_state_kingpin
        user_data.persist()

    elif user_data.life_state != ewcfg.life_state_grandfoe and ewcfg.role_grandfoe in roles_map_user:
        # Fix the life_state of a grand foe.
        user_data.life_state = ewcfg.life_state_grandfoe
        user_data.persist()

    faction_roles_remove = [
        ewcfg.role_juvenile,
        ewcfg.role_juvenile_active,
        ewcfg.role_juvenile_pvp,
        ewcfg.role_rowdyfuckers,
        ewcfg.role_rowdyfuckers_pvp,
        ewcfg.role_rowdyfuckers_active,
        ewcfg.role_copkillers,
        ewcfg.role_copkillers_pvp,
        ewcfg.role_copkillers_active,
        ewcfg.role_corpse,
        ewcfg.role_corpse_pvp,
        ewcfg.role_corpse_active,
        ewcfg.role_kingpin,
        ewcfg.role_grandfoe,
        ewcfg.role_slimecorp,
    ]

    # Manage faction roles.
    faction_role = ewutils.get_faction(user_data=user_data)

    faction_roles_remove.remove(faction_role)

    pvp_role = None
    active_role = None
    if faction_role in ewcfg.role_to_pvp_role:

        if user_data.time_expirpvp >= time_now:
            pvp_role = ewcfg.role_to_pvp_role.get(faction_role)
            faction_roles_remove.remove(pvp_role)

        if ewutils.is_otp(user_data):
            active_role = ewcfg.role_to_active_role.get(faction_role)
            faction_roles_remove.remove(active_role)

    # Manage location roles.
    poi = ewcfg.id_to_poi.get(user_data.poi)
    if poi != None:
        poi_role = poi.role
    else:
        poi_role = None

    poi_roles_remove = []
    for poi in ewcfg.poi_list:
        if poi.role != None and poi.role != poi_role:
            poi_roles_remove.append(poi.role)

    role_ids = []
    for role_id in roles_map_user:

        try:
            role_data = EwRole(id_server=id_server, id_role=role_id)
            roleName = role_data.name
            if roleName != None and roleName not in faction_roles_remove and roleName not in poi_roles_remove:
                role_ids.append(role_data.id_role)
        except:
            ewutils.logMsg(
                'error: couldn\'t find role with id {}'.format(role_id))

    try:
        role_data = EwRole(id_server=id_server, name=faction_role)
        if not role_data.id_role in role_ids:
            role_ids.append(role_data.id_role)
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find role {}'.format(faction_role))

    try:
        role_data = EwRole(id_server=id_server, name=pvp_role)
        if not role_data.id_role in role_ids:
            role_ids.append(role_data.id_role)
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find role {}'.format(pvp_role))

    try:
        role_data = EwRole(id_server=id_server, name=active_role)
        if not role_data.id_role in role_ids:
            role_ids.append(role_data.id_role)
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find role {}'.format(active_role))

    try:
        role_data = EwRole(id_server=id_server, name=poi_role)
        if not role_data.id_role in role_ids:
            role_ids.append(role_data.id_role)
            #ewutils.logMsg('found role {} with id {}'.format(role_data.name, role_data.id_role))
    except:
        ewutils.logMsg('error: couldn\'t find role {}'.format(poi_role))

    #if faction_role not in role_names:
    #	role_names.append(faction_role)
    #if poi_role != None and poi_role not in role_names:
    #	role_names.append(poi_role)

    #replacement_roles = []
    #for name in role_names:
    #	role = roles_map.get(name)

    #	if role != None:
    #		replacement_roles.append(role)
    #	else:
    #		ewutils.logMsg("error: role missing \"{}\"".format(name))

    #ewutils.logMsg('looking for {} roles to replace'.format(len(role_ids)))
    replacement_roles = []

    for role in member.server.roles:
        if role.id in role_ids:
            #ewutils.logMsg('found role {} with id {}'.format(role.name, role.id))
            replacement_roles.append(role)

    #ewutils.logMsg('found {} roles to replace'.format(len(replacement_roles)))

    try:
        await client.replace_roles(member, *replacement_roles)
    except:
        ewutils.logMsg('error: failed to replace roles for {}'.format(
            member.display_name))