-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlauncher_astrbot_en.bat
More file actions
265 lines (220 loc) · 7.77 KB
/
launcher_astrbot_en.bat
File metadata and controls
265 lines (220 loc) · 7.77 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
@echo off
chcp 936 >nul
setlocal
:: Clear screen and output Banner
cls
echo.
echo =========================
echo AstrBot Launcher v0.2.0
echo =========================
echo.
set PYTHON_CMD=python
set MIRROR_URL=https://mirrors.aliyun.com/pypi/simple
set FALLBACK_MIRROR_URL=https://pypi.org/simple
set ASTRBOT_EXIT_CODE=0
set DOWNLOAD_MAX_RETRIES=3
set DOWNLOAD_RETRY_DELAY_SEC=3
set PROJECT_DIR_CHANGED=0
:: Check if Python is installed
%PYTHON_CMD% --version >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Python is not installed. Please install Python 3.10 or higher.
set ASTRBOT_EXIT_CODE=1
goto end
)
:: Get Python version
for /f "tokens=2 delims= " %%a in ('%PYTHON_CMD% --version 2^>^&1') do (
set PYTHON_VERSION=%%a
)
:: Extract major and minor version numbers
for /f "tokens=1,2 delims=." %%a in ("%PYTHON_VERSION%") do (
set PYTHON_MAJOR=%%a
set PYTHON_MINOR=%%b
)
:: Check if Python version is less than 3.10
if %PYTHON_MAJOR% lss 3 (
echo [ERROR] Python 3.10 or higher is required. Current version is %PYTHON_VERSION%.
set ASTRBOT_EXIT_CODE=1
goto end
)
if %PYTHON_MAJOR%==3 if %PYTHON_MINOR% lss 10 (
echo [ERROR] Python 3.10 or higher is required. Current version is %PYTHON_VERSION%.
set ASTRBOT_EXIT_CODE=1
goto end
)
:: Python version meets the requirement
echo [INFO] Python version meets the requirement. Current version is %PYTHON_VERSION%.
echo.
:: Check if AstrBot or QQChannelChatGPT folder exists
if not exist AstrBot (
if not exist QQChannelChatGPT (
echo [INFO] AstrBot folder not found. Downloading the latest version from GitHub...
call :downloadLatestRelease
if errorlevel 1 (
set ASTRBOT_EXIT_CODE=1
goto end
)
)
)
echo [INFO] AstrBot or QQChannelChatGPT folder already exists. No need to download.
echo.
goto SetupAndRun
:downloadLatestRelease
:: Clean stale download metadata/files from previous failed runs
if exist latest.txt del /q latest.txt >nul 2>&1
if exist latest.zip del /q latest.zip >nul 2>&1
:: Call GitHub API to get the latest release information
powershell -NoProfile -Command "$ErrorActionPreference = 'Stop'; $release = Invoke-RestMethod -Uri 'https://api.github.com/repos/AstrBotDevs/AstrBot/releases/latest'; if (-not $release.zipball_url) { throw 'zipball_url is empty' }; Set-Content -Path 'latest.txt' -Value $release.zipball_url -NoNewline -Encoding ASCII"
if errorlevel 1 (
echo [ERROR] Failed to obtain the latest version information from GitHub API.
exit /b 1
)
:: Check if the download URL was successfully obtained
if not exist latest.txt (
echo [ERROR] Failed to obtain the latest version information.
exit /b 1
)
:: Read the download link from latest.txt
set /p download_url=<latest.txt
if "%download_url%"=="" (
echo [ERROR] Failed to obtain a valid download URL from GitHub API.
exit /b 1
)
set "ASTRBOT_DOWNLOAD_URL=%download_url%"
echo [INFO] Downloading the latest version of AstrBot...
:download
set DOWNLOAD_ATTEMPT=1
:download_retry
echo [INFO] Download attempt %DOWNLOAD_ATTEMPT%/%DOWNLOAD_MAX_RETRIES%...
if exist latest.zip del /q latest.zip >nul 2>&1
:: Download the latest zipball version
powershell -NoProfile -Command "$ErrorActionPreference = 'Stop'; Invoke-WebRequest -Uri $env:ASTRBOT_DOWNLOAD_URL -OutFile 'latest.zip'"
if not errorlevel 1 goto download_success
if %DOWNLOAD_ATTEMPT% geq %DOWNLOAD_MAX_RETRIES% (
echo [ERROR] Failed to download the latest version file after %DOWNLOAD_MAX_RETRIES% attempts. Please check your network and try again.
if exist latest.zip del /q latest.zip >nul 2>&1
exit /b 1
)
set /a DOWNLOAD_WAIT_SEC=%DOWNLOAD_RETRY_DELAY_SEC%*%DOWNLOAD_ATTEMPT%
echo [WARN] Download attempt %DOWNLOAD_ATTEMPT% failed. Retrying in %DOWNLOAD_WAIT_SEC% seconds...
timeout /t %DOWNLOAD_WAIT_SEC% /nobreak >nul
set /a DOWNLOAD_ATTEMPT=%DOWNLOAD_ATTEMPT%+1
goto download_retry
:download_success
:: Check if the download was successful
if not exist latest.zip (
echo [ERROR] Failed to download the latest version file. You can manually download the zip from https://github.com/AstrBotDevs/AstrBot/releases/latest, then extract the folder inside the zip to the current directory and rename it to AstrBot.
exit /b 1
)
:: Clear screen
cls
echo [INFO] The file has been downloaded to latest.zip.
:: Extract the latest version files
echo [INFO] Extracting the latest version files...
echo.
powershell -NoProfile -Command "$ErrorActionPreference = 'Stop'; Expand-Archive -LiteralPath 'latest.zip' -DestinationPath '.' -Force"
:: Check if the extraction was successful
if errorlevel 1 (
echo [ERROR] An error occurred while extracting the latest version files. You can manually download the zip from https://github.com/AstrBotDevs/AstrBot/releases/latest, then extract the folder inside the zip to the current directory and rename it to AstrBot.
exit /b 1
)
:: Rename the extracted folder to AstrBot
for /d %%I in ("AstrBotDevs-AstrBot-*") do (
if exist "%%I" (
ren "%%I" AstrBot
)
)
echo.
echo [INFO] AstrBot download is complete.
echo.
:: Delete the downloaded zip file and latest.txt
del latest.zip
del latest.txt
exit /b 0
:SetupAndRun
:: Change to AstrBot or QQChannelChatGPT directory
if exist AstrBot (
cd AstrBot
set PROJECT_DIR_CHANGED=1
) else (
if exist QQChannelChatGPT (
cd QQChannelChatGPT
set PROJECT_DIR_CHANGED=1
) else (
echo [ERROR] Neither AstrBot nor QQChannelChatGPT folder exists.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
)
:: Set up a virtual environment
echo [INFO] Setting up a virtual environment...
if not exist venv (
%PYTHON_CMD% -m venv venv
if errorlevel 1 (
echo [ERROR] Failed to create virtual environment.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
)
:: Activate the virtual environment
call venv\Scripts\activate.bat
if errorlevel 1 (
echo [ERROR] Failed to activate virtual environment.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
:: Check for dependency updates
echo [INFO] Upgrading pip using index: %MIRROR_URL%
%PYTHON_CMD% -m pip install --upgrade pip -i %MIRROR_URL% >nul 2>&1
if errorlevel 1 (
echo [WARN] pip upgrade failed with %MIRROR_URL%. Retrying with %FALLBACK_MIRROR_URL%...
%PYTHON_CMD% -m pip install --upgrade pip -i %FALLBACK_MIRROR_URL% >nul 2>&1
if errorlevel 1 (
echo [ERROR] Failed to upgrade pip.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
)
echo [INFO] Installing uv using index: %MIRROR_URL%
%PYTHON_CMD% -m pip install uv -i %MIRROR_URL% >nul 2>&1
if errorlevel 1 (
echo [WARN] uv install failed with %MIRROR_URL%. Retrying with %FALLBACK_MIRROR_URL%...
%PYTHON_CMD% -m pip install uv -i %FALLBACK_MIRROR_URL% >nul 2>&1
if errorlevel 1 (
echo [ERROR] Failed to install uv.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
)
echo [INFO] Installing requirements using index: %MIRROR_URL%
%PYTHON_CMD% -m uv pip install -r requirements.txt -i %MIRROR_URL%
if errorlevel 1 (
echo [WARN] requirements install failed with %MIRROR_URL%. Retrying with %FALLBACK_MIRROR_URL%...
%PYTHON_CMD% -m uv pip install -r requirements.txt -i %FALLBACK_MIRROR_URL%
if errorlevel 1 (
echo [ERROR] Failed to install dependencies from requirements.txt.
set ASTRBOT_EXIT_CODE=1
goto cleanup
)
)
:: Run the main script
echo [INFO] Starting AstrBot.
echo.
%PYTHON_CMD% main.py
set "ASTRBOT_EXIT_CODE=%ERRORLEVEL%"
if %ASTRBOT_EXIT_CODE% neq 0 (
echo [ERROR] AstrBot exited with error code %ASTRBOT_EXIT_CODE%.
)
:: Deactivate the virtual environment
call venv\Scripts\deactivate.bat
goto cleanup
:cleanup
if "%PROJECT_DIR_CHANGED%"=="1" (
cd ..
set PROJECT_DIR_CHANGED=0
)
:end
if not "%ASTRBOT_NO_PAUSE%"=="1" (
if %ASTRBOT_EXIT_CODE% neq 0 pause
)
endlocal & exit /b %ASTRBOT_EXIT_CODE%