aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore5
-rw-r--r--Dockerfile14
-rw-r--r--FunnyPineappleBot.sample.service18
-rw-r--r--config.py1
-rw-r--r--docker-compose.yaml10
-rw-r--r--main.py20
-rw-r--r--requirements.txt (renamed from dependencies)0
-rw-r--r--samples.txt0
-rw-r--r--setup.py243
-rw-r--r--shared/config.sample.py3
-rw-r--r--shared/instances.py3
11 files changed, 29 insertions, 288 deletions
diff --git a/.gitignore b/.gitignore
index 29385a3..a1bdc9b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -133,6 +133,5 @@ dmypy.json
133.vscode 133.vscode
134 134
135FunnyPineappleBot.service 135FunnyPineappleBot.service
136config.py 136tmp/*
137samples.txt 137data/*
138tmp/* \ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..f66d997
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
1FROM python:bullseye
2
3RUN apt-get update
4RUN apt-get upgrade
5RUN pip install pip -U
6
7WORKDIR /app
8COPY requirements.txt .
9RUN pip install -r requirements.txt
10
11COPY . .
12COPY data/config.py .
13
14CMD ["python", "main.py"]
diff --git a/FunnyPineappleBot.sample.service b/FunnyPineappleBot.sample.service
deleted file mode 100644
index 210b267..0000000
--- a/FunnyPineappleBot.sample.service
+++ /dev/null
@@ -1,18 +0,0 @@
1[Unit]
2Description=Telegram bot from FunnyPineappleChat
3After=syslog.target
4After=network.target
5
6[Service]
7Type=simple
8User={username}
9WorkingDirectory={path}
10ExecStart={py_path} main.py -m
11KillMode=control-group
12KillSignal=15
13SendSIGKILL={send_kill}
14RestartSec=5
15Restart=always
16
17[Install]
18WantedBy=multi-user.target \ No newline at end of file
diff --git a/config.py b/config.py
new file mode 100644
index 0000000..5608add
--- /dev/null
+++ b/config.py
@@ -0,0 +1 @@
token="{}"
diff --git a/docker-compose.yaml b/docker-compose.yaml
new file mode 100644
index 0000000..7383133
--- /dev/null
+++ b/docker-compose.yaml
@@ -0,0 +1,10 @@
1version: "3"
2
3services:
4 pineapple-bot:
5 build: "."
6 restart: "always"
7 volumes:
8 - "./requirements.txt:/app/requirements.txt"
9 - "./data/samples.txt:/app/samples.txt"
10
diff --git a/main.py b/main.py
index 4ed98a7..64aad14 100644
--- a/main.py
+++ b/main.py
@@ -2,29 +2,9 @@ import optparse
2 2
3from aiogram import executor, types as t, Dispatcher 3from aiogram import executor, types as t, Dispatcher
4import logging 4import logging
5from shared import config
6 5
7logging.basicConfig(level=logging.INFO) 6logging.basicConfig(level=logging.INFO)
8 7
9parser = optparse.OptionParser(conflict_handler="resolve") # Делает прикол с аргументами
10parser.add_option('-t', '--test',
11 action="store_true",
12 dest='test',
13 help='test token')
14parser.add_option('-m', '--main',
15 action="store_true",
16 dest='main',
17 help='main token')
18values, args = parser.parse_args() # Либа хуйня
19
20if values.test:
21 config.token = config.test_token
22elif values.main:
23 config.token = config.main_token
24else:
25 config.token = config.test_token
26
27
28async def on_start(dp: Dispatcher): 8async def on_start(dp: Dispatcher):
29 from shared.commands import commands 9 from shared.commands import commands
30 for scope, cmd in commands.items(): 10 for scope, cmd in commands.items():
diff --git a/dependencies b/requirements.txt
index 888ef8a..888ef8a 100644
--- a/dependencies
+++ b/requirements.txt
diff --git a/samples.txt b/samples.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/samples.txt
diff --git a/setup.py b/setup.py
deleted file mode 100644
index b6fdfc1..0000000
--- a/setup.py
+++ /dev/null
@@ -1,243 +0,0 @@
1import os
2import platform
3import shutil
4import sys
5import traceback
6import typing as p
7
8term = shutil.get_terminal_size()
9sep = "=" * term.columns
10
11venv = False
12default = 1
13
14if os.name == "nt":
15 print(f"Setup not supported on {platform.system()}")
16 exit()
17
18
19def _opt_selector(options: p.Dict[str, p.Any], default: p.Optional[int] = None, pmt: str = "") -> p.Tuple[int, p.Any]:
20 _options = {}
21 print("Choose variant")
22 print(sep)
23
24 num = 1
25 for opt in options:
26 if options[opt] is None:
27 print()
28 continue
29
30 d = "*" if default == num else " "
31
32 print(f" {d}{num} - {opt}")
33 _options[num] = options[opt]
34 num += 1
35 print(sep)
36
37 while True:
38 opt = input(f"{pmt}-> ")
39 if opt.isdigit():
40 opt = int(opt)
41 if opt in _options:
42 return opt, _options[opt]
43 if opt == "" and default is not None:
44 return default, _options[default]
45
46 print("Incorrect answer")
47 pmt = ""
48
49
50def _yes(default: bool = True) -> bool:
51 while True:
52 if default:
53 it = "[Y|n]"
54 else:
55 it = "[y|N]"
56
57 i = input(f"{it} -> ")
58 if i.lower() in ["y", "n"]:
59 return i.lower() == "y"
60 if i == "":
61 return default
62
63 print("Incorrect answer")
64
65
66def _input(prompt: str, default: str = None, required: bool = True) -> str:
67 if default:
68 print(f"Default {default}")
69 while True:
70 result = input(f"{prompt} -> ")
71 if not result:
72 if default:
73 result = default
74 break
75 elif required:
76 print("Parameter required")
77 else:
78 break
79
80 return result
81
82
83def _cmd(cmd: str, show_cmd: bool = True) -> bool:
84 if show_cmd:
85 input(f"Press enter for execute {cmd}")
86 result = os.system(cmd)
87 return True if result == 0 else False
88
89
90def _clear():
91 print()
92 print('\033c', end="")
93
94
95def _enter():
96 input("Press enter, to continue ...")
97
98
99def create_samples_txt():
100 if not os.path.isfile("samples.txt"):
101 with open("samples.txt", "w"):
102 print("File created")
103 else:
104 print("File exists")
105 _enter()
106 return True
107
108
109def systemd_unit_generator():
110 while True:
111 username = _input("Your username", os.environ["USER"])
112 path = _input("Path to root of project", os.path.dirname(os.path.abspath(__file__)))
113 py_path = _input("Path to python 3.9", sys.executable)
114
115 print("Send kill (if SIGTERM Timeout)")
116 send_kill = "off"
117 if _yes(True):
118 send_kill = "on"
119
120 with open("FunnyPineappleBot.sample.service", "r") as file:
121 sample = file.read().format(username=username, path=path, py_path=py_path, send_kill=send_kill)
122
123 _clear()
124 print(
125 f"{sep}",
126 f"{sample}",
127 f"{sep}",
128 f"Your username - {username}",
129 f"Path to root of project - {path}",
130 f"Path to python 3.9 - {py_path}",
131 f"Send kill - {send_kill}",
132 f"{sep}",
133 "",
134 "All correct ?",
135 sep="\n"
136 )
137 if _yes(False):
138 with open("FunnyPineappleBot.service", "w") as file:
139 file.write(sample)
140 _clear()
141
142 print("Link unit from /etc/systemd/system/ ?")
143 if _yes():
144 _cmd("sudo mv FunnyPineappleBot.service /etc/systemd/system/")
145 _cmd("sudo ln /etc/systemd/system/FunnyPineappleBot.service ./ -s")
146 break
147 else:
148 _clear()
149 print(sep)
150 _enter()
151 return True
152
153
154def config_generator():
155 try:
156 import config
157 except ImportError:
158 config = object()
159 while True:
160 main_token = _input("Main bot token", getattr(config, "main_token", None))
161 test_token = _input("Test bot token", getattr(config, "test_token", None))
162
163 with open("shared/config.sample.py", "r") as file:
164 sample = file.read().format(main_token=main_token, test_token=test_token)
165
166 print(
167 f"{sep}",
168 f"{sample}",
169 f"{sep}",
170 "All correct ?",
171 sep="\n"
172 )
173
174 if _yes(False):
175 with open("shared/config.py", "w") as file:
176 file.write(sample)
177 break
178 else:
179 _clear()
180 return True
181
182
183def install_dependencies():
184 with open("dependencies", "r") as file:
185 dependencies = file.read()
186 print(
187 "Install this ?",
188 f"{sep}",
189 f"{dependencies}",
190 f"{sep}",
191 sep="\n"
192 )
193 _cmd("pip install -U -r dependencies", False)
194 print(f"{sep}\nSuccessfully installed")
195 _enter()
196 return True
197
198
199if __name__ == '__main__':
200 opts = {
201 "Create samples.txt file": create_samples_txt,
202 "Setup systemd unit": systemd_unit_generator,
203 "Setup config.py": config_generator,
204 "Install or Update dependencies": install_dependencies,
205 "Exit": exit,
206 }
207 exit_index = list(opts.keys()).index("Exit") + 1
208 default = 1
209 pmt = ""
210
211 _clear()
212 while True:
213 if default >= exit_index:
214 default = exit_index
215 term = shutil.get_terminal_size()
216 sep = "=" * term.columns
217 try:
218 _clear()
219 num, opt = _opt_selector(opts, default, pmt)
220 _clear()
221 res = opt()
222 _clear()
223
224 if num != default:
225 default = num + 1
226 elif res is True:
227 default += 1
228
229 if res is not True:
230 pmt = f"{res!r} "
231 if num == default:
232 default = num + 1
233 else:
234 pmt = ""
235
236 except KeyboardInterrupt:
237 break
238 except Exception as e:
239 pmt = f"An error has occurred ({e.__class__.__name__}:{e.args[0]})"
240 trc = traceback.format_exc()
241 print(trc)
242 _enter()
243 _clear()
diff --git a/shared/config.sample.py b/shared/config.sample.py
deleted file mode 100644
index f4a3f09..0000000
--- a/shared/config.sample.py
+++ /dev/null
@@ -1,3 +0,0 @@
1TOKEN = ""
2test_token = "{test_token}"
3main_token = "{main_token}"
diff --git a/shared/instances.py b/shared/instances.py
index 25de59f..5351f9c 100644
--- a/shared/instances.py
+++ b/shared/instances.py
@@ -1,6 +1,7 @@
1from aiogram import Bot, Dispatcher 1from aiogram import Bot, Dispatcher
2from .config import token 2from config import token
3 3
4print(token)
4bot = Bot(token=token) 5bot = Bot(token=token)
5dp = Dispatcher(bot) 6dp = Dispatcher(bot)
6gen_chance = 10 7gen_chance = 10