-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcode.py
367 lines (320 loc) · 11.7 KB
/
code.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import keyboard
import re
import requests
from time import sleep
### variables ------ ###
players = list()
commands = list()
raw_console = list()
chars = " !\"#$&'()*-+,./0123456789:;<=>?@[\\]^_{}|~abcdefghijklmnopqrstuvwxyzбвгджзклмнпрстфхцчшщаеёиоуыэюяйьъ"
path = "C:/Program Files (x86)/Steam/steamapps/common/Team Fortress 2"
botnames = list()
botregexnames = list()
botsteamids = list()
output_player = False
output_votekick = True
clear_log = True
online_database = True
sleep_time = 5
query_keybind = 71
execute_keybind = 72
tf2_query_keybind = "KP_HOME"
tf2_execute_keybind = "KP_UPARROW"
auto_update = True
key_convert = {
"LWIN":91,
"RALT":541,
"RCTRL":29,
"SCROLLLOCK":70,
"PGDN":81,
"UPARROW":72,
"LEFTARROW":75,
"DOWNARROW":80,
"RIGHTARROW":77,
"KP_SLASH":53,
"KP_MULTIPLY":55,
"KP_MINUS":74,
"KP_HOME":71,
"KP_UPARROW":72,
"KP_PGUP":73,
"KP_PLUS":78,
"KP_LEFTARROW":75,
"KP_5":76,
"KP_RIGHTARROW":77,
"KP_END":79,
"KP_DOWNARROW":80,
"KP_PGDN":81,
"KP_ENTER":28,
"KP_INS":82,
"KP_DEL":83
}
### ---------------- ###
### util ----------- ###
def time_to_int(time):
try:
total = 0
times = time.split(":")
if len(times) == 2:
total = (int(times[0])*60)+int(times[1])
elif len(times) == 3:
total = (int(times[0])*3600)+(int(times[1])*360)+int(times[2])
return total
except:
return 0
# This is used to find the player with the shortest connectiom time with the name of variable 'name'
def get_newest(name):
global players
duplicates = list()
# We query through every player
for player in players:
#If the player has the name we're searching for, we add it to the list
if player.name == name:
duplicates.append(player)
newestplayer = duplicates[0]
# Next we go through every player, and if the player has a shorter connection time than our current value,
# We set the newestplayer variable to the new player
for duplicate in duplicates:
if duplicate.time <= newestplayer.time:
newestplayer = duplicate
return newestplayer
### classes -------- ###
class PlayerInstance:
userid = 0
name = ""
time = 0
steamid = ""
def __init__(self, userid, name, time, steamid):
self.userid = userid
self.name = name
self.time = time
self.steamid = steamid
def __str__(self):
return "PlayerInstance[userid:" + str(self.userid) + ",name:" + self.name + ",time:" + str(self.time) + ",steamid:" + self.steamid + "]"
### ---------------- ###
### setup ---------- ###
def setup():
read_config()
check_update()
setup_autoexec()
create_bk_autoexec()
create_bk_query()
get_bots()
def read_config():
with open("config.properties", "r",encoding="utf-8") as f:
config = f.readlines()
for line in config:
if not line.startswith("#") and not line.strip() == "":
splitter = line.find("=")
key = line[:splitter]
value = line[splitter+1:].replace("\n","")
if key == "path":
global path
path = value
elif key == "chars":
global chars
chars = value
elif key == "output_player":
global output_player
output_player = value.lower().startswith("t")
elif key == "output_votekick":
global output_votekick
output_votekick = value.lower().startswith("t")
elif key == "clear_log":
global clear_log
clear_log = value.lower().startswith("t")
elif key == "sleep_time":
global sleep_time
sleep_time = int(value)
elif key == "query_keybind" or key == "status_keybind":
global query_keybind
global tf2_query_keybind
query_keybind = key_convert.get(value, value)
tf2_query_keybind = value
elif key == "execute_keybind" or key == "votekick_keybind":
global execute_keybind
global tf2_execute_keybind
execute_keybind = key_convert.get(value, value)
tf2_execute_keybind = value
elif key == "online_database":
global online_database
online_database = value.lower().startswith("t")
elif key == "auto_update":
global auto_update
auto_update = value.lower().startswith("t")
else:
print("Ignoring key '" + key + "'")
def check_update():
global auto_update
if auto_update:
files = list()
files.append(requests.get("https://raw.githubusercontent.com/boyonkgit/tf2-bot-kicker/main/metadata.properties").text.split("\n"))
with open("metadata.properties", "r",encoding="utf-8") as f:
files.append(f.readlines())
for i in range(len(files)):
mapped = dict()
file = files[i]
for line in file:
if not line.startswith("#") and not line.strip() == "":
splitter = line.find("=")
key = line[:splitter]
value = line[splitter+1:].replace("\n","")
if key == "version":
mapped[key] = value
if i == 0:
online_metadata = mapped
else:
metadata = mapped
if not metadata["version"] == online_metadata["version"]:
update()
def update():
new_metadata = requests.get("https://raw.githubusercontent.com/boyonkgit/tf2-bot-kicker/main/metadata.properties").text.replace("\r","")
new_code = requests.get("https://raw.githubusercontent.com/boyonkgit/tf2-bot-kicker/main/code.py").text.replace("\r","")
with open("metadata.properties", "w", encoding="utf-8") as f:
f.write(new_metadata)
with open("code.py", "w", encoding="utf-8") as f:
f.write(new_code)
exec(open("code.py",encoding="utf-8").read())
exit()
def setup_autoexec():
if not os.path.isfile(path + "/tf/cfg/autoexec.cfg"):
with open(path + "/tf/cfg/autoexec.cfg", "w", encoding="utf-8") as f:
f.close()
with open(path + "/tf/cfg/autoexec.cfg", "r", encoding="utf-8") as f:
lines = f.readlines()
bk_autoexec_found = False
for line in lines:
if line.startswith("exec \"bk_autoexec.cfg\""):
bk_autoexec_found = True
break
if not bk_autoexec_found:
with open(path + "/tf/cfg/autoexec.cfg", "a", encoding="utf-8") as f:
f.write("\nexec \"bk_autoexec.cfg\"")
def create_bk_autoexec():
with open(path + "/tf/cfg/bk_autoexec.cfg", "w", encoding="utf-8") as f:
f.write("bind \"" + tf2_query_keybind + "\" \"exec bk_query.cfg\"\n")
f.write("bind \"" + tf2_execute_keybind + "\" \"exec bk_execute.cfg\"\n")
f.write("con_logfile console.log")
def create_bk_query():
with open(path + "/tf/cfg/bk_query.cfg", "w", encoding="utf-8") as f:
f.write("status")
def get_bots():
global botnames
global botregexnames
global botsteamids
global online_database
files = list()
if online_database:
files.append(requests.get("https://raw.githubusercontent.com/boyonkgit/tf2-bot-kicker/main/bots.properties").text.split("\n"))
with open("bots.properties", "r",encoding="utf-8") as f:
files.append(f.readlines())
for file in files:
for line in file:
if not line.startswith("#") and not line.strip() == "":
splitter = line.find("=")
key = line[:splitter]
value = line[splitter+1:].replace("\n","")
if key == "name":
botnames.append(value)
elif key == "steamid":
botsteamids.append(value)
elif key == "regexname":
botregexnames.append(re.compile(value))
elif key == "xname":
try:
botnames.remove(value)
except:
print("Name '" + value + "' was not found in list")
elif key == "xsteamid":
try:
botsteamids.remove(value)
except:
print("SteamID '" + value + "' was not found in list")
else:
print("Ignoring key '" + key + "' in bots.properties")
### ---------------- ###
### tick ----------- ###
def tick():
query()
sleep(1)
get_console()
read_console()
detect()
execute()
def query():
keyboard.send(query_keybind)
def get_console():
global raw_console
with open(path + "/tf/console.log", "r", encoding="utf-8", errors="ignore") as f:
raw_console = f.readlines()
with open(path + "/tf/console.log", "w", encoding="utf-8", errors="ignore") as f:
f.close()
def read_console():
global raw_console
userids = list()
new_players = list()
for line in raw_console:
if line.startswith("#") and not line.startswith("# userid"):
arguments = list()
for part in line.rstrip().split(" "):
if len(part) > 0:
arguments.append(part)
if not arguments[len(arguments)-2] == "BOT":
userid = arguments[1]
if not userid in userids:
userids.append(userid)
original_name = line[line.find("\"")+1:line.rfind("\"")]
name = ""
for c in original_name:
if c.lower() in chars:
name = name + c.lower()
time = time_to_int(arguments[len(arguments)-4])
steamid = arguments[len(arguments)-5]
new_players.append(PlayerInstance(userid,name,time,steamid))
if len(new_players) > 0:
global players
players = new_players
def detect():
global players
global clear_log
checked_names = list()
if clear_log:
os.system("cls")
for player in players:
if output_player:
print(str(player))
if player.name in botnames:
votekick(player)
elif player.steamid in botsteamids:
votekick(player)
elif player.name in checked_names:
votekick(get_newest(player.name))
else:
for regex in botregexnames:
if regex.match(player.name):
votekick(player)
checked_names.append(player.name)
def votekick(player):
global commands
if output_votekick:
print("Votekicking " + str(player))
commands.append("callvote kick " + str(player.userid))
def execute():
if len(commands) > 0:
create_bk_execute()
sleep(1)
keyboard.send(execute_keybind)
def create_bk_execute():
global commands
with open(path + "/tf/cfg/bk_execute.cfg", "w", encoding="utf-8") as f:
for command in commands:
f.write(command + "\n")
commands = list()
### ---------------- ###
### run ----------- ###
def run():
setup()
while True:
sleep(sleep_time)
tick()
run()