-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (64 loc) · 2.69 KB
/
main.py
File metadata and controls
81 lines (64 loc) · 2.69 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
import logging
import sys
from datetime import datetime
from logging.handlers import RotatingFileHandler
import a2s
import discord
from discord.ext import tasks
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Finished importing the specified libraries.")
TOKEN = ""
SERVER_IPv4 = "0.0.0.0"
SERVER_PORT = 27015
OFFLINE_STATUS = "Server unreachable"
UPDATE_INTERVAL_SECONDS = 1
discord_log_handler = RotatingFileHandler(
filename="log.log",
encoding="utf-8",
maxBytes=1024 * 1024,
backupCount=3,
)
discord_log_fmt = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s", datefmt="%Y-%m-%d %I:%M:%S")
discord_log_handler.setFormatter(discord_log_fmt)
logger = logging.getLogger("Python.App")
logger.addHandler(discord_log_handler)
logger.setLevel(logging.INFO)
application = discord.Client(intents=discord.Intents.default())
@application.event
async def on_ready():
await application.wait_until_ready()
if not loop_renew_status.is_running():
await loop_renew_status.start()
@tasks.loop(seconds=UPDATE_INTERVAL_SECONDS)
async def loop_renew_status():
await set_app_status(generate_status())
async def set_app_status(custom_status):
try:
await application.change_presence(status=discord.Status.online, activity=discord.Game(custom_status))
except TypeError as status_change_error:
logger.error(f"Exception on change presence {status_change_error}")
def generate_status():
discord_status = ""
try:
server_query = a2s.info((SERVER_IPv4, SERVER_PORT))
server_players = server_query.player_count
server_max_players = server_query.max_players
server_map = server_query.map_name
discord_status = f"{server_players}/{server_max_players} on {server_map}"
except Exception as server_scan_error:
discord_status = OFFLINE_STATUS
logger.error(f"Exception while trying to query {SERVER_IPv4}:{SERVER_PORT}: {server_scan_error}")
return discord_status
if UPDATE_INTERVAL_SECONDS < 60:
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} The value for UPDATE_INTERVAL_SECONDS must be greater or equal to 60.")
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Exiting with code 1...")
logger.error("The value for UPDATE_INTERVAL_SECONDS must be greater or equal to 60.")
logger.error("Exiting with code 1...")
sys.exit(1)
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Starting the discord application...")
try:
application.run(TOKEN, log_handler=discord_log_handler, log_formatter=discord_log_fmt)
except Exception as app_startup_error:
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} The discord application failed to start. Check the logs.")
logger.error(f"The discord application failed to start: {app_startup_error}")
logger.error("Exiting with code 1...")
sys.exit(1)