-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvite_bot.py
More file actions
126 lines (97 loc) · 3.81 KB
/
invite_bot.py
File metadata and controls
126 lines (97 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import requests
import time
import json
import os
def goodUsers(users, min_level):
# Filter users based on min_level and find the highest level user.
invited_users = []
max_level = 0
max_level_user = None
for user in users:
if user["stats"]["lvl"] >= min_level:
invited_users.append(user["_id"])
if user["stats"]["lvl"] > max_level:
max_level = user["stats"]["lvl"]
max_level_user = user
return invited_users, max_level_user
def inviteUser(userId, groupId, headers):
# Invite a user to the group.
inviteURL = f'https://habitica.com/api/v3/groups/{groupId}/invite'
payload = {'uuids': [userId]}
response = requests.post(inviteURL, json=payload, headers=headers)
if response.status_code == 200:
print(f"User {userId} invited successfully.")
else:
print(f"Failed to invite user {userId}. Error: {response.text}")
def create_default_config(config_file):
# Create a default config file.
default_config = {
"userAPI": "your-user-api-key",
"tokenAPI": "your-token-api-key",
"groupId": "your-group-id",
"delay": 60,
"min_level": 10
}
with open(config_file, 'w') as file:
json.dump(default_config, file, indent=4)
print(f"Default config file created: {config_file}")
print("Please update the file with your actual configuration.")
def load_config(config_file):
# Load config from JSON file.
if not os.path.exists(config_file):
create_default_config(config_file)
raise SystemExit("Please update the config file and restart the script.")
with open(config_file, 'r') as file:
config = json.load(file)
# Check if the config is valid.
required_keys = ["userAPI", "tokenAPI", "groupId", "delay", "min_level"]
if any(config.get(key) in [None, "", f"your-{key.lower()}"] for key in required_keys):
print("Config file contains placeholder values. Please update it.")
raise SystemExit("Update the config file and restart the script.")
return config
def main():
# Main function to run the Habitica invite bot.
config_file = 'config.json'
config = load_config(config_file)
userAPI = config['userAPI']
tokenAPI = config['tokenAPI']
groupId = config['groupId']
delay = config['delay']
min_level = config['min_level']
sessionAmount = 1
headers = {
'x-api-user': userAPI,
'x-api-key': tokenAPI,
}
while True:
print("―――――")
print(f"Session: {time.time()} | {sessionAmount}")
sessionAmount += 1
print()
userRequest = requests.get('https://habitica.com/api/v3/user', headers=headers)
print("Got user data!")
userData = userRequest.json()
partyId = userData['data']['party']['_id']
print(f"Party ID: {partyId}")
partyUserURL = 'https://habitica.com/api/v3/looking-for-party'
lookingParty = requests.get(partyUserURL, headers=headers)
print("Got people looking for party!")
print(f"Users looking for party: {len(lookingParty.json()['data'])}")
usersList, max_level_user = goodUsers(lookingParty.json()["data"], min_level)
print(f"Users to invite: {len(usersList)}")
if max_level_user:
print(f"Best user: {max_level_user['profile']['name']} (Level: {max_level_user['stats']['lvl']})")
else:
print("There is no best user.")
print()
if len(usersList) == 0:
print(f"No users to invite. Waiting {delay} seconds...")
time.sleep(delay)
continue
for userId in usersList:
inviteUser(userId, partyId, headers)
print()
print(f"Waiting {delay} seconds...")
time.sleep(delay)
if __name__ == "__main__":
main()