Skip to content

Instantly share code, notes, and snippets.

@Zzz9194
Last active June 5, 2020 19:53
Show Gist options
  • Save Zzz9194/598a53bbac27c7f5631eddf5bdc9cc8d to your computer and use it in GitHub Desktop.
Save Zzz9194/598a53bbac27c7f5631eddf5bdc9cc8d to your computer and use it in GitHub Desktop.
Gist to allow you to get updates on group demotions, promotions, removals and acceptances. By running `python3 database-update.py` when you wish for an update. You can run `python3 database-update.py view (rolename)` to view members of that role, or remove rolename to view all group members
# REQUIREMENTS -> data.json FILE
# REQUIREMENTS -> REQUESTS MODULE
# REQUIREMENTS -> JSON MODULE
# REQUIREMENTS -> SYS MODULE
# REQUIREMENTS (OPTIONAL) -> COLORAMA MODULE
# REQUIREMENT -> A ROBLOX GROUP ID
GROUP_IDENTIFIER_ID = #Insert integer group ID here
import requests
import json
from sys import argv
from colorama import Fore, Style, Back
def main():
if len(argv) == 2:
if argv[1].lower() in ['check','update']:
compare_data()
update_data()
elif argv[1].lower() in ['view']:
view_data()
elif len(argv) == 3:
if argv[1].lower() in ['view']:
view_data(rank=argv[2])
else:
compare_data()
update_data()
elif len(argv) == 1:
compare_data()
update_data()
def compare_data():
# Make a fresh request for comparison with stored data
check_req = get_group_members(GROUP_IDENTIFIER_ID)
with open('data.json','r') as doc:
info = str(doc.read())
prev_data = json.loads(info)
newUsernames = [username for username in check_req.keys()]
oldUsernames = [username for username in prev_data.keys()]
for username in newUsernames:
if username not in oldUsernames:
rank = check_req[username]['RoleName']
print(f"{Style.BRIGHT}[{Fore.GREEN}+{Fore.RESET}]{Style.BRIGHT} {username} has been added to the group at {rank}{Style.RESET_ALL}")
for username in oldUsernames:
if username not in newUsernames:
rank = prev_data[username]['RoleName']
print(f"{Style.BRIGHT}[{Fore.RED}-{Fore.RESET}]{Style.BRIGHT} {username} has been removed from the group at rank {rank}{Style.RESET_ALL}")
try:
for username in newUsernames:
if check_req[username]['Role'] != prev_data[username]['Role']:
if check_req[username]['Role'] > prev_data[username]['Role']:
print(f"{Style.BRIGHT}{Fore.GREEN}[{Fore.YELLOW}+{Fore.GREEN}]{Fore.RESET}{Style.BRIGHT} {username}" + " has been promoted from {} -> {}".format(prev_data[username]['RoleName'],check_req[username]['RoleName']))
else:
print(f"{Style.BRIGHT}{Fore.GREEN}[{Fore.YELLOW}-{Fore.GREEN}]{Fore.RESET}{Style.BRIGHT} {username}" + " has been demoted from {} -> {}".format(prev_data[username]['RoleName'],check_req[username]['RoleName']))
except:
pass
print(f"{Fore.GREEN}{Style.BRIGHT}The State Police has a total of {str((len(check_req.keys())) - 1)} members.{Style.RESET_ALL}")
def update_data():
data = get_group_members(GROUP_IDENTIFIER_ID)
data = json.dumps(data)
# Change data as dict --> JSON
with open('data.json','w') as doc:
doc.write(data)
# Write it into the .json file (Write automatically clears the file upon opening)
def view_data(rank=None):
with open('data.json','r') as doc:
info = str(doc.read())
prev_data = json.loads(info)
# Load the json file as .json --> dict
# Print the username (Key) and then the user ID and rank (Value)
if rank != None:
for user in prev_data.keys():
givenRole = prev_data[user]['RoleName']
if rank.lower() in givenRole.lower():
print(f"{Fore.GREEN}[{Fore.YELLOW}*{Fore.GREEN}]{Fore.RESET}{Style.BRIGHT} Username: " + user.ljust(30,' ') + "User ID: " + str(prev_data[user]['UserId']).ljust(30, ' ') + "Rank: " + prev_data[user]['RoleName'].ljust(30, ' '))
elif rank.lower() in user.lower():
print(f"{Fore.GREEN}[{Fore.YELLOW}*{Fore.GREEN}]{Fore.RESET}{Style.BRIGHT} Username: " + user.ljust(30,' ') + "User ID: " + str(prev_data[user]['UserId']).ljust(30, ' ') + "Rank: " + prev_data[user]['RoleName'].ljust(30, ' '))
else:
for user in prev_data.keys():
print(f"{Fore.GREEN}[{Fore.YELLOW}*{Fore.GREEN}]{Fore.RESET}{Style.BRIGHT} Username: " + user.ljust(30,' ') + "User ID: " + str(prev_data[user]['UserId']).ljust(30, ' ') + "Rank: " + prev_data[user]['RoleName'].ljust(30, ' '))
def get_group_members(group_id):
ENDPOINT = f"https://groups.roblox.com/v1/groups/{group_id}/users?sortOrder=Asc&limit=100"
data = {}
cursor = ""
while cursor != None: # Until a cursor is not found you keep making requests to the group data changing the cursor each time
r = requests.get(f"{ENDPOINT}&cursor={cursor}").json()
cursor = r["nextPageCursor"]
for user in r["data"]:
userinfo = user['user']
username = userinfo['username']
userId = userinfo['userId']
userrank = user['role']['name']
userRoleNumber = user['role']['rank']
data[username] = {"UserId":userId,"RoleName":userrank,"Role":userRoleNumber}
# The data key will be the username and the value will be a list containing [UserId,RankName,RankNumber]
return data
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment