-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_wizard.py
executable file
·144 lines (113 loc) · 4.97 KB
/
setup_wizard.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
import subprocess
import os
import sys
sys.path.append(os.path.realpath('.'))
import inquirer
import argparse
import xml.etree.ElementTree as ET
def main():
try:
os.setuid(0)
service_file_path = '/etc/systemd/system/discord_bot_script.service'
sh_file_path = f'{os.path.dirname(os.path.abspath(__file__))}/discord_bot.sh'
relative_path = f'{os.path.dirname(os.path.abspath(__file__))}'
global sequence
discord_token, lavalink_host, lavalink_port, lavalink_pass = config()
if sequence == 0 or sequence == 2:
configure_service(sh_file_path, relative_path, service_file_path)
print('Finished successfully, please reboot your system.')
else:
print('Finished successfully.')
except Exception as e:
print(f'Error occurred in main(), please consult the maintainer: {e}')
def configure_service(sh_file_path, relative_path, service_file_path):
genShFile(sh_file_path, relative_path)
genServiceFile(service_file_path, relative_path)
subprocess.run(['chmod', '+x', sh_file_path])
subprocess.run(['chown', 'root', sh_file_path])
subprocess.run(['chmod', '+x', service_file_path])
subprocess.run(['systemctl', 'daemon-reload'])
subprocess.run(['systemctl', 'start', 'discord_bot_script.service'])
subprocess.run(['systemctl', 'enable', 'discord_bot_script.service'])
def genShFile(sh_file_path, relative_path):
sh_file_content = f'''#!/bin/bash
set -x
if screen -list | grep -qw \'discord_bot\'; then
echo \'Discord bot is already running.\'
else
cd {relative_path}
# Start the Discord bot in a detached screen session
/usr/bin/screen -dmS discord_bot /bin/bash -c \'python3.10 ./bot_init.py; exec /bin/bash\'
echo \'Discord bot started.\'
fi'''
os.chdir(relative_path)
with open(sh_file_path, 'w') as file:
file.write(sh_file_content)
def genServiceFile(service_file_path, relative_path):
service_file_content = f'''[Unit]
Description=discord-bot-script
[Unit]
Description=Lavalink Service
After=lavalink.target
[Service]
Type=oneshot
ExecStart=/bin/su - {os.getlogin()} -c \'{relative_path}/discord_bot.sh\'
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target'''
os.chdir('/etc/systemd/system')
with open(service_file_path, 'w') as file:
file.write(service_file_content)
def get_config():
try:
tree = ET.parse('config.xml')
root = tree.getroot()
discord_token = root.find('.//discord_token').text
lavalink_host = root.find('.//lavalink_host').text
lavalink_port = root.find('.//lavalink_port').text
lavalink_pass = root.find('.//lavalink_pass').text
return discord_token, lavalink_host, lavalink_port, lavalink_pass
except Exception as e:
print(f'Error while trying to read the config file: {e}')
def config():
global sequence
setup_choice = [
inquirer.List(
'choice',
message='What should be setup??',
choices=[('Setup discord token, lavalink and startup.', 0),
('Setup token and lavalink.', 1),
('Setup startup.', 2)],
),
]
setup = inquirer.prompt(setup_choice)
sequence = setup['choice']
if sequence == 2:
return None, None, None, None
elif sequence != 2 :
try:
returner = [
inquirer.Text('discord_token', message='Input your discord token'),
inquirer.Text('lavalink_host', message='Input your lavalink host <default is localhost>'),
inquirer.Text('lavalink_port', message='Input your lavalink port <default is 2333>'),
inquirer.Text('lavalink_pass', message='Input your lavalink password <default is youshallnotpass>'),
]
setup = inquirer.prompt(returner)
root = ET.Element('Config')
cl = ET.Element('Configurations')
ET.SubElement(cl, 'discord_token').text = str(setup['discord_token'])
ET.SubElement(cl, 'lavalink_host').text = str(setup['lavalink_host']) if str(setup['lavalink_host']) != '' else 'localhost'
ET.SubElement(cl, 'lavalink_port').text = str(setup['lavalink_port']) if str(setup['lavalink_port']) != '' else '2333'
ET.SubElement(cl, 'lavalink_pass').text = str(setup['lavalink_pass']) if str(setup['lavalink_pass']) != '' else 'youshallnotpass'
root.append(cl)
tree = ET.ElementTree(root)
with open('config.xml', 'wb') as files:
tree.write(files)
return setup['discord_token'], setup['lavalink_host'], setup['lavalink_port'], setup['lavalink_pass']
except Exception as e:
print(f'Error occurred in config(), please consult the maintainer: {e}')
else:
print(f'How did we get here?')
if __name__ == '__main__':
main()