28
28
import pathlib
29
29
import platform
30
30
import re
31
- import sys
32
31
import subprocess
33
- from typing import Callable
32
+ import sys
33
+ from collections .abc import Callable
34
34
35
35
import aiohttp
36
36
@@ -91,7 +91,7 @@ async def teardown(bot: Bot) -> None: ...
91
91
MAIN = """"""
92
92
93
93
BOOLS = {
94
- "y" : True ,
94
+ "y" : True ,
95
95
"yes" : True ,
96
96
"n" : False ,
97
97
"no" : False ,
@@ -114,23 +114,23 @@ def bool_validate(inp: str) -> bool:
114
114
115
115
def validate_input (inp : str , check : Callable [[str ], bool ] | None = None , * , error_msg : str | None = None ) -> str :
116
116
error_msg = error_msg or "Invalid input, please try again!"
117
-
117
+
118
118
while True :
119
119
response = input (inp )
120
120
if not check :
121
121
break
122
-
122
+
123
123
try :
124
124
result = check (response )
125
125
except Exception :
126
126
result = False
127
-
127
+
128
128
if result is False :
129
129
print (error_msg , end = "\n \n " )
130
130
continue
131
-
131
+
132
132
break
133
-
133
+
134
134
return response
135
135
136
136
@@ -183,102 +183,108 @@ def version_info() -> None:
183
183
184
184
185
185
def install_packages (exe : pathlib .Path , starlette : bool | None = False ) -> None :
186
- package = "twitchio" if not starlette else "twitchio[starlette] "
186
+ package = "twitchio[starlette] " if starlette else "twitchio"
187
187
subprocess .call ([exe , "-m" , "pip" , "install" , package , "--upgrade" , "--no-cache" ])
188
188
189
189
190
190
def generate_venv () -> None :
191
191
# Create the venv...
192
192
subprocess .call ([sys .executable , "-m" , "venv" , ".venv" ])
193
-
193
+
194
194
system = platform .system ()
195
-
195
+
196
196
if system == "Windows" :
197
197
exe = pathlib .Path (".venv" ) / "Scripts" / "python.exe"
198
198
elif system in ["Darwin" , "Linux" ]:
199
199
exe = pathlib .Path (".venv" ) / "bin" / "python"
200
- else :
200
+ else :
201
201
print ("Unsupported operating system... Skipping package installation. Please manually install required packages." )
202
202
return
203
-
204
- starlette = bool_check (validate_input ("Would you like to install the optional Starlette and Uvicorn packages? (y/N): " , bool_validate ,))
205
- install_packages (exe , starlette )
203
+
204
+ starlette = bool_check (
205
+ validate_input (
206
+ "Would you like to install the optional Starlette and Uvicorn packages? (y/N): " ,
207
+ bool_validate ,
208
+ )
209
+ )
210
+ install_packages (exe , starlette )
206
211
207
212
208
213
def generate_bot () -> ...:
209
214
name = validate_input ("Project name? (Leave blank to generate files in this directory): " )
210
215
if name :
211
- dir = pathlib .Path (name )
212
- dir .mkdir (exist_ok = True )
213
- os .chdir (dir )
216
+ _dir = pathlib .Path (name )
217
+ _dir .mkdir (exist_ok = True )
218
+ os .chdir (_dir )
214
219
else :
215
- dir = pathlib .Path .cwd ()
216
-
220
+ _dir = pathlib .Path .cwd ()
221
+
217
222
if sys .prefix != sys .base_prefix :
218
- resp = bool_check (validate_input ("No virtual environment used. Would you like to create one? (y/N): " , bool_validate ,))
219
-
223
+ resp = bool_check (
224
+ validate_input (
225
+ "No virtual environment used. Would you like to create one? (y/N): " ,
226
+ bool_validate ,
227
+ )
228
+ )
229
+
220
230
if resp :
221
231
generate_venv ()
222
-
223
- components = bool_check (validate_input ("Would you like to setup commands.Components? (y/N): " , bool_validate ))
232
+
233
+ components = bool_check (validate_input ("Would you like to setup commands.Components? (y/N): " , bool_validate ))
224
234
if components :
225
235
comp_dir = pathlib .Path ("components" )
226
236
comp_dir .mkdir (exist_ok = True )
227
-
237
+
228
238
with open (comp_dir / "general.py" , "w" ) as fp :
229
239
fp .write (COMPONENT )
230
-
240
+
231
241
client_id = None
232
242
client_sec = None
233
- config = bool_check (validate_input ("Would you like to create a config? (y/N): " , bool_validate ))
234
-
243
+ config = bool_check (validate_input ("Would you like to create a config? (y/N): " , bool_validate ))
244
+
235
245
if config :
236
-
237
246
while True :
238
-
239
247
client_id = validate_input ("Please enter your Client-ID: " )
240
248
cid_reenter = validate_input ("Please re-enter your Client-ID: " )
241
-
249
+
242
250
if client_id != cid_reenter :
243
251
print ("Client-ID does not match, please try again..." , end = "\n \n " )
244
252
continue
245
-
253
+
246
254
break
247
-
248
- while True :
255
+
256
+ while True :
249
257
client_sec = getpass .getpass ("Please enter your Client-Secret: " )
250
258
csec_reenter = getpass .getpass ("Please re-enter your Client-Secret: " )
251
259
252
260
if client_sec != csec_reenter :
253
261
print ("Client-Secret does not match, please try again..." , end = "\n \n " )
254
262
continue
255
-
263
+
256
264
break
257
-
265
+
258
266
config_data = f"""[secrets]\n client_id = \" { client_id } \" \n client_secret = \" { client_sec } \" """
259
267
with open ("config.toml" , "w" ) as fp :
260
268
fp .write (config_data )
261
-
269
+
262
270
if client_id and client_sec :
263
271
while True :
264
272
owner_name = validate_input ("Please enter the Twitch username of the owner of this Bot (E.g. chillymosh): " )
265
273
bot_name = validate_input ("Please enter the Twitch username of the Bot Account (E.g. chillybot): " )
266
274
names = f"Owner Name: '{ owner_name } '\n Bot Name: '{ bot_name } '"
267
-
275
+
268
276
correct = bool_check (validate_input (f"Is this information correct? (y/N)\n \n { names } \n " , bool_validate ))
269
277
if not correct :
270
278
continue
271
-
279
+
272
280
break
273
-
274
-
275
-
281
+
276
282
# TODO: .env
277
283
# TODO: client details
278
284
# TODO: fetch owner/bot IDs
279
285
# with open(dir / "main.py", "w") as fp:
280
286
# ...
281
-
287
+
282
288
# with open(dir / "bot.py", "w") as fp:
283
289
# ...
284
290
0 commit comments