-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_chat_to_discord.py
More file actions
116 lines (106 loc) · 3.57 KB
/
send_chat_to_discord.py
File metadata and controls
116 lines (106 loc) · 3.57 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
import os
import time
import requests
import threading
CONFIG_FILE = 'config.txt'
DEFAULT_CONFIG = '''link = "https://discord.com/api/webhooks/id/xxxxxxxxxxx"
#REPLACE WITH YOUR WEBHOOK LINK
mcpath = "C:\\Users\\admin\\Desktop\\MultiMC\\instances\\1.21.4\\.minecraft"
#REPLACE WITH YOUR MC PATH TO YOUR FABRIC INSTANCE
cooldown_sec = 1.5
#REPLACE WITH YOUR PREFERRED COOLDOWN
'''
def load_config():
if not os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
f.write(DEFAULT_CONFIG)
print(f"Please edit {CONFIG_FILE} with your settings.")
# Open config.txt automatically
try:
if os.name == 'nt':
os.startfile(CONFIG_FILE)
elif os.name == 'posix':
os.system(f'xdg-open {CONFIG_FILE}')
except Exception:
pass
exit(1)
config = {}
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
for line in f:
if '=' in line and not line.strip().startswith('#'):
k, v = line.split('=', 1)
config[k.strip()] = v.strip().strip('"')
return config
def check_webhook(link):
try:
r = requests.get(link)
return r.status_code == 200
except Exception:
return False
def check_mcpath(mcpath):
return os.path.exists(os.path.join(mcpath, 'chatlogs.txt'))
def send_discord(link, content):
data = {"content": content}
try:
r = requests.post(link, json=data)
return r.status_code == 204 or r.status_code == 200
except Exception:
return False
def format_message(line):
if ' > ' in line:
username, text = line.split(' > ', 1)
return f"**{username.strip()}** > `{text.strip()}`"
elif 'joined from' in line or 'left the game' in line:
return f"**[SERVER]** > {line.strip()}"
return None
def tail_file(path, callback, cooldown):
sent = set()
with open(path, 'r', encoding='utf-8') as f:
lines = f.readlines()
# Only send the last 15 lines on startup
recent = lines[-15:] if len(lines) > 15 else lines
for line in recent:
line = line.strip()
if not line or line in sent:
continue
msg = format_message(line)
if msg:
callback(msg)
sent.add(line)
time.sleep(cooldown)
# Now tail for new lines
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
line = line.strip()
if not line or line in sent:
continue
msg = format_message(line)
if msg:
callback(msg)
sent.add(line)
time.sleep(cooldown)
def main():
config = load_config()
link = config.get('link')
mcpath = config.get('mcpath')
cooldown = float(config.get('cooldown_sec', 1.5))
if not link or not mcpath:
print('Missing link or mcpath in config.txt. Please check your config.')
exit(1)
chatlog_path = os.path.join(str(mcpath), 'chatlogs.txt')
if not check_webhook(link):
print('Invalid Discord webhook. Please check your config.txt.')
exit(1)
if not check_mcpath(mcpath):
print('Invalid Minecraft path or chatlogs.txt not found. Please check your config.txt.')
exit(1)
print('Monitoring chatlogs.txt...')
def send(msg):
print('Sending:', msg)
send_discord(link, msg)
tail_file(chatlog_path, send, cooldown)
if __name__ == '__main__':
main()