-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_server.py
More file actions
85 lines (71 loc) · 2.43 KB
/
random_server.py
File metadata and controls
85 lines (71 loc) · 2.43 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
import sys
import psutil
import os
from discord import SyncWebhook
import random
import time
from env import WEBHOOK_URL, PROCESS_NAME, STEAM_URL
def is_game_running(process_name=PROCESS_NAME):
"""Check if the game process is running."""
for proc in psutil.process_iter(attrs=['name']):
name = proc.info['name']
if name and process_name in name:
return True
return False
def start_game(steam_url=STEAM_URL):
"""Start the game via Steam URL."""
if sys.platform.startswith('win'):
# Use os.startfile to open the URL
try:
os.startfile(steam_url)
return True
except Exception as e:
print(f"Failed to start game: {e}")
return False
def stop_game(process_name=PROCESS_NAME):
"""Terminate all instances of the game process."""
killed = False
for proc in psutil.process_iter(attrs=['pid', 'name']):
name = proc.info['name']
if name and process_name in name:
try:
p = psutil.Process(proc.info['pid'])
p.terminate()
p.wait(timeout=15)
print(f"Terminated {name} (PID {proc.info['pid']})")
killed = True
except psutil.NoSuchProcess:
pass
except psutil.TimeoutExpired:
print(f"Process {proc.info['pid']} did not terminate in time; killing")
p.kill()
killed = True
if not killed:
print("No running process found to terminate.")
return killed
def send_discord_message(url, message="Hello World"):
"""Send a message to a Discord webhook."""
# Create a SyncWebhook object using the webhook URL
webhook = SyncWebhook.from_url(url)
# Send a message to the webhook
webhook.send(message)
# randomly choose to start or stop the game
def main():
if random.choice([True, False]):
if is_game_running():
print("Game is already running.")
else:
print("Starting game...")
if start_game():
send_discord_message(WEBHOOK_URL, "Game started")
else:
if is_game_running():
print("Stopping game...")
send_discord_message(WEBHOOK_URL, "Stopping game in 2 minutes")
time.sleep(120)
stop_game()
else:
print("Game is not running.")
if __name__ == "__main__":
# Run the main function
main()