This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
176 lines (146 loc) · 5.95 KB
/
run.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import asyncio
import datetime
import json
import logging
import os
import ssl
import time
import requests
import urllib3
import websockets
import websockets.client
from endpoints import Endpoints
from freeGPT import freeGPT
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
# Function to print colored output
def print_colored(message, color):
colors = {
"info": "\033[94m", # Blue
"error": "\033[91m", # Red
"success": "\033[92m", # Green
"reset": "\033[0m", # Reset color
}
print(colors[color] + message + colors["reset"])
config = json.load(open(r"config.json", encoding="utf8"))
id_seen = []
avoidList = config["players_to_avoid"]
avoidList.append(config["in_game_name"])
webhook_url = config["discord_webhook_url"]
prompt_path = config["prompt"]
update_frequency = config["providers-update-frequency"]
fg = freeGPT()
# Check if provider.json exists
if os.path.exists("providers.json"):
with open("providers.json", "r") as json_file:
existing_data = json.load(json_file)
last_updated = existing_data.get("updated-on", "")
today = datetime.datetime.now().strftime("%Y-%m-%d")
if last_updated != today:
# The file was last updated on a different date, so update it
print_colored("Checking available free providers...", "info")
fg.update_working_providers()
print_colored("Done", "success")
data = {
"updated-on": today,
"providers": [_provider.__name__ for _provider in fg.WORKING_PROVIDERS],
}
providers = fg.WORKING_PROVIDERS
with open("providers.json", "w") as json_file:
json.dump(data, json_file, indent=4)
print_colored("Data updated in providers.json", "success")
else:
# The file was updated today, so read the providers array
providers = existing_data.get("providers", [])
fg.update_working_providers_from_name(providers)
print_colored("Providers: " + str(providers), "info")
else:
# If provider.json doesn't exist, create it with today's date
print_colored("Checking available free providers...", "info")
fg.update_working_providers()
print_colored("Done", "success")
data = {
"updated-on": datetime.datetime.now().strftime("%Y-%m-%d"),
"providers": [_provider.__name__ for _provider in fg.WORKING_PROVIDERS],
}
with open("providers.json", "w") as json_file:
json.dump(data, json_file, indent=4)
async def reconnect_to_websocket():
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
endpoint = Endpoints()
headers = endpoint.headers
port = endpoint.port
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
url = f"wss://127.0.0.1:{port}"
websocket_client = websockets.connect(url, ssl=ssl_context, extra_headers=headers)
async with websocket_client as websocket:
await websocket.send('[5, "OnJsonApiEvent_chat_v6_messages"]')
while True:
response = await websocket.recv()
h = handle(response, endpoint)
if h is not None:
await websocket.close()
return h
def read_init_prompt(file_path):
with open(file_path, "r") as f:
content = f.read()
return content
def send_to_discord(webhook_url, message):
data = {"content": message}
headers = {"Content-Type": "application/json"}
response = requests.post(webhook_url, data=json.dumps(data), headers=headers)
if response.status_code == 204:
print_colored("Webhook sent successfully", "success")
else:
print_colored(
f"Failed to send webhook. Status code: {response.status_code}", "error"
)
def handle(response, endpoint):
if len(response) > 10:
resp_json = json.loads(response)
message = resp_json[2]["data"]["messages"][0]
if "ares-coregame" in message["cid"]:
if message["id"] not in id_seen:
sent_msg = f"{message['game_name']} : {message['body']}"
if message["game_name"] not in avoidList:
time.sleep(3)
content = read_init_prompt(prompt_path)
content_prompt = content + sent_msg
res = fg.try_all_working_providers(content_prompt)
if len(res) > 200:
if webhook_url:
send_to_discord(
webhook_url,
f"```!!THIS MESSAGE WAS NOT SENT FOR BEING TOO LONG!! {sent_msg}\nchatGPT: {res}```",
)
print_colored(
f"!!THIS MESSAGE WAS NOT SENT FOR BEING TOO LONG!! - > chatGPT: {res}",
"error",
)
else:
try:
endpoint.postNewChatMessage(message["cid"], res)
except Exception as e:
print_colored(
"Failed To Send Message in Valorant ...", "error"
)
if webhook_url:
send_to_discord(
webhook_url, f"```{sent_msg}\nchatGPT: {res}```"
)
print_colored(f"chatGPT: {res}", "info")
else:
if webhook_url:
send_to_discord(webhook_url, f"```{sent_msg}```")
print_colored(f"chatGPT: {res}", "info")
id_seen.append(message["id"])
print_colored("...........websocket_client starting.........", "info")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(reconnect_to_websocket())
loop.close()