-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ps1
400 lines (340 loc) · 14.2 KB
/
main.ps1
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator"))
{
$arguments ="& '" + $myinvocation.mycommand.definition +"'"+"$args[0]"
Start-Process powershell -Verb runAs -ArgumentList $arguments
exit
}
set-ExecutionPolicy RemoteSigned
$Host.UI.RawUI.WindowTitle = 'PallasHelper'
Write-Host "欢迎使用PallasBotHelper"
Write-Host "正在检测 PallasBoty 和 Pallas-Bot 必须的依赖项"
$PythonURL = "https://repo.huaweicloud.com/python/3.9.9/python-3.9.9.exe"
$GitURL = "https://repo.huaweicloud.com/git-for-windows/v2.37.1.windows.1/MinGit-2.37.1-32-bit.zip"
#$MongodbURL = "https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-6.0.0-signed.msi"
#$CppURL = "https://myvs.download.prss.microsoft.com/dbazure/mu_visual_cpp_build_tools_2015_update_3_x64_dvd_dfd9a39c.iso?t=2d9c8bc8-eb35-4d1f-a0e2-962fc2463acc&e=1660583046&h=95cab42736b0cd8ed7679c6ee95d6b00c939abf58473188a97e3cc339535f81b&su=1"
$FfmpegURL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full.7z"
$ZipURL = "https://www.7-zip.org/a/7zr.exe"
$PYTHON = ""
$GIT = ""
$FFMPEG = ""
$7ZR = ""
$DB = 0
# 尝试开启Tls1.2
if (-Not [System.Net.SecurityProtocolType]::Tls12)
{
$TLSSource = @"
using System.Net;
public static class SecurityProtocolTypeExtensions
{
public const SecurityProtocolType EnableTls12 = (SecurityProtocolType)4032;
}
"@
Add-Type -TypeDefinition $TlSSource
Try
{
[System.Net.ServicePointManager]::SecurityProtocol = [SecurityProtocolTypeExtensions]::EnableTls12
}
Catch
{
<#
开启失败,暂时不报错,因为当前切换到了支持Tls1.0的下载地址
if ($PSVersionTable.PSVersion.Major -Le 2)
{
Write-Host "当前系统配置不支持TLS1.2,程序可能无法正常运行。请确保你正在使用Win7SP1或者Win2008R2SP1或更高版本,并打开Windows Update安装有关.Net Framework的更新后重试。"
}
else
{
Write-Host "当前系统配置不支持TLS1.2,程序可能无法正常运行。请更新至.Net Framework 4.5或更高版本后重试。"
}
Read-Host -Prompt "按回车键继续执行,但程序可能无法正常运行 ----->"
#>
}
}
else
{
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12
}
#低版本PowerShell手动获取路径
if (!$PSScriptRoot)
{
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
cd -LiteralPath "$PSScriptRoot"
# PowerShell multi-threaded download by 小透明・宸
function PartiallyDownload-File([String]$Uri, [String]$OutFile, [Int64]$Start, [Int64]$End = 0, [String]$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36') {
[Net.ServicePointManager]::DefaultConnectionLimit = [Int32]::MaxValue
$Request = [Net.WebRequest]::Create($Uri)
if ($End) {
$Request.AddRange($Start, $End)
}
else {
$Request.AddRange($Start)
}
$Request.UserAgent = $UserAgent
$Request.Proxy = $null
$Response = $Request.GetResponse()
$Stream = $Response.GetResponseStream()
$File = [IO.File]::Create($OutFile)
$Stream.CopyTo($File)
$File.Close()
$Stream.Close()
$Response.Close()
}
function Merge-File([String[]]$Source, [String]$Destination) {
$Source = $Source.Clone()
for ($i = 0; $i -lt $Source.Length; $i++) {
$Source[$i] = '"' + $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Source[$i]) + '"'
}
cmd /c copy /b /y ($Source -join '+') $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination) | Out-Null
}
function MultiThreadDownload-File([String]$Uri, [String]$OutFile, [Int32]$ThreadCount = 4, [Int32]$MinSliceSize = 256KB, [String]$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36') {
[Net.ServicePointManager]::DefaultConnectionLimit = [Int32]::MaxValue
[Int64]$Length = (Invoke-WebRequest $Uri -Method Head -UseBasicParsing -Proxy $null).Headers.'Content-Length'
[String[]]$Part = @()
[Int64[]]$Start = @()
[Int64[]]$End = @()
[Management.Automation.PowerShell[]]$Job = @()
[Object[]]$Handle = @()
if (($MinSliceSize * $ThreadCount) -gt $Length) { $ThreadCount = [Math]::Floor($Length / $MinSliceSize) }
for ($i = 0; $i -lt $ThreadCount; $i++) {
$Start += $End[$i - 1] + [Int64](!!$i)
$End += [Math]::Round($Length / $ThreadCount * ($i + 1))
$Part += $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath([GUID]::NewGuid().ToString('N') + '.bin')
$Job += [PowerShell]::Create().AddScript(${Function:PartiallyDownload-File}).AddParameter('Uri', $Uri).AddParameter('OutFile', $Part[$i]).AddParameter('Start', $Start[$i]).AddParameter('End', $End[$i]).AddParameter('UserAgent', $UserAgent)
$Handle += $Job[$i].BeginInvoke()
}
[Double]$Progress = 0
[Int32]$Interval = 200
[Boolean]$Complete = $false
while (!$Complete) {
Start-Sleep -Milliseconds $Interval
$Complete = $true
for ($i = 0; $i -lt $ThreadCount; $i++) {
if (!$Handle[$i].IsCompleted) {
$Complete = $false
break
}
}
for ($i = 0; $i -lt $ThreadCount; $i++) {
if (!(Test-Path $Part[$i])) { continue }
$Progress = (Get-Item $Part[$i]).Length / ($End[$i] - $Start[$i] + 1) * 100
Write-Progress -Id $i -Activity ('Thread #{0} {1} - {2}' -f $i, $Start[$i], $End[$i]) -Status ('{0} / {1} {2:f2}%' -f (Get-Item $Part[$i]).Length, ($End[$i] - $Start[$i] + 1), $Progress) -PercentComplete $Progress
}
}
for ($i = 0; $i -lt $ThreadCount; $i++) {
Write-Progress -Id $i -Activity ('Thread {0} - {1}' -f $Start[$i], $End[$i]) -Completed
$Job[$i].EndInvoke($Handle[$i])
$Job[$i].Runspace.Close()
$Job[$i].Dispose()
}
Merge-File -Source $Part -Destination $OutFile
foreach ($p in $Part) { Remove-Item $p }
}
#下载文件
function DownloadFile($url, $targetFile)
{
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
$buffer = new-object byte[] 256KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "正在下载文件 '$($url.split('/') | Select -Last 1)'" -Status "已下载 ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "文件 '$($url.split('/') | Select -Last 1)' 下载已完成" -Status "下载已完成" -Completed
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
Write-Host "PallasBot启动脚本"
Write-Host "初始化"
#检查安装
if (-Not (Test-Path -LiteralPath "$PSScriptRoot\nonebotPallas\.git" -PathType Container))
{
Write-Host "警告:.git文件夹不存在" -ForegroundColor red
Write-Host "这可能代表你之前可能没没有按照特定的方式安装" -ForegroundColor red
Write-Host "虽然PallasBot仍将正常工作,但PallasInstaller将无法正常使用" -ForegroundColor red
Write-Host "你可以通过尝试使用脚本重新安装以解决此问题" -ForegroundColor red
}
#解压文件
Try
{
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
function Unzip
{
param([string]$zipfile, [string]$outpath)
Try{
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}Catch{
if (-Not (Test-Path -Path "$PSScriptRoot\app\7zr.exe" -PathType Leaf))
{
Write-Host "正在下载7zr"
DownloadFile $ZipURL "$PSScriptRoot\app\7zr.exe"
}
if (-Not (Test-Path -Path "$PSScriptRoot\app\7zr.exe" -PathType Leaf))
{
Write-Host "无法加载7zr" -ForegroundColor red
Exit
}
$7ZR = "$PSScriptRoot\app\7zr.exe"
& $7ZR x $zipfile -o"$outpath"
}
}
}
Catch
{
Write-Host "出现了未知的异常"
}
Write-Host "检测Python"
Try
{
$Command = Get-Command -Name pip -ErrorAction Stop
$PYTHON = "python"
}
Catch {}
if ($PYTHON -eq "")
{
DownloadFile $PythonURL "$PSScriptRoot\app\python-setup.exe"
$Command = .\app\python-setup.exe /quiet TargetDir=C:\PYTHON PrependPath=1
Try
{
$Command = Get-Command -Name python -ErrorAction Stop
$PYTHON = "python"
}
Catch
{
Write-Host "出现了意料外的错误,请稍后尝试重新启动!" -ForegroundColor red
Exit
}
}
Write-Host "Python: $PYTHON" -ForegroundColor green
Write-Host "检测Git"
Try{
$Command = Get-Command -Name git -ErrorAction Stop
$GIT = "git"
}Catch {}
Try{
$Command = Get-Command -Name "$PSScriptRoot\app\git\cmd\git" -ErrorAction Stop
$GIT = "$PSScriptRoot\app\git\cmd\git"
}
Catch {}
if ($GIT -eq "")
{
DownloadFile $GitURL "$PSScriptRoot\app\git.zip"
Remove-Item -LiteralPath "$PSScriptRoot\app\git\" -Recurse -ErrorAction SilentlyContinue
Unzip "$PSScriptRoot\git.zip" "$PSScriptRoot\app\git\"
Remove-Item -LiteralPath "$PSScriptRoot\app\git.zip" -ErrorAction SilentlyContinue
Try {
$Command = Get-Command -Name "$PSScriptRoot\app\git\cmd\git" -ErrorAction Stop
$GIT = "$PSScriptRoot\app\git\cmd\git"
}Catch {
Write-Host "无法加载Git!" -ForegroundColor red
Exit
}
}
#检测Pallas-Bot
if (-Not (Test-Path -Path ".\nonebotPallas\bot.py" -PathType Leaf))
{
Write-Host "没有找到Pallas-Bot,正在尝试从gitee仓库clong"
& "$GIT" clone --depth=5 https://gitee.com/craun/Pallas-Bot nonebotPallas
}
Write-Host "Git: $GIT" -ForegroundColor green
cd "$PSScriptRoot\nonebotPallas"
#启动向导参数
if (($args[0] -eq "--update") -or ($args[0] -eq "-u"))
{
& "$GIT" fetch --depth=1
& "$GIT" reset --hard origin/master
Write-Host "更新操作已执行完毕" -ForegroundColor green
}
elseif (($args[0] -eq "--revert") -or ($args[0] -eq "-r"))
{
& "$GIT" reset --hard "HEAD@{1}"
Write-Host "回滚操作已执行完毕" -ForegroundColor green
}
elseif (($args[0] -eq "--revert") -or ($args[0] -eq "-t"))
{
Write-Host "检测ffmpeg"
Try{
$Command = Get-Command -Name ffmpeg -ErrorAction Stop
$FFMPEG = "ffmpeg"
}Catch {}
Try{
$Command = Get-Command -Name "$PSScriptRoot\app\ffmpeg\bin\ffmpeg" -ErrorAction Stop
$FFMPEG = "$PSScriptRoot\app\ffmpeg\bin\ffmpeg"
}
Catch {}
if ($FFMPEG -eq "")
{
Write-Host "Pallas-Bot将会使用ffmpeg发送语音(如果你不希望Pallas-Bot发送语音,可以不装这个)"
$value = Read-Host -Prompt "请问是否需要Pallasbot-helper为博士安装ffmpeg?([yes]/no)"
if( -Not ($value -match '^n')){
Try {
$Command = Get-Command -Name ffmpeg -ErrorAction Stop
Write-Host "已在系统环境中找到ffmpeg"
}Catch {
Write-Host "开始下载ffmpeg,请耐心等待"
mkdir "$PSScriptRoot\app\"
curl -o "$PSScriptRoot\app\ffmpeg.7z" "$FfmpegURL"
Remove-Item -LiteralPath "$PSScriptRoot\app\ffmpeg-5.1-full_build\" -Recurse -ErrorAction SilentlyContinue
Unzip "$PSScriptRoot\app\ffmpeg.7z" "$PSScriptRoot\app\"
[Environment]::SetEnvironmentVariable("PATH", $Env:PATH + ";$PSScriptRoot\app\ffmpeg-5.1-full_build\bin", [EnvironmentVariableTarget]::Machine)
Write-Host "尝试将ffmpeg写入PATH"
}
}else{
Write-Host "跳过ffmpeg安装"
}
}
cd ..
mkdir cache
cd cache
Write-Host "即将下载Microsoft Visual C++ Build Tools 14.0"
Write-Host "温馨提示:需要安装的是 “构建工具” 不是 “运行库” !" -ForegroundColor red
$value = Read-Host -Prompt "如果你已经安装过了Microsoft Visual C++ Build Tools 14.0 ,可以输入already跳过安装(输入其他内容则将开始下载)"
if( -Not ($value -match '^already')){
Write-Host "开始下载Microsoft Visual C++ Build Tools,请耐心等待(这个过程大约需要3-15分钟,依网络状况而定)"
MultiThreadDownload-File -Uri 'https://myvs.download.prss.microsoft.com/dbazure/mu_visual_cpp_build_tools_2015_update_3_x64_dvd_dfd9a39c.iso?t=2d9c8bc8-eb35-4d1f-a0e2-962fc2463acc&e=1660583046&h=95cab42736b0cd8ed7679c6ee95d6b00c939abf58473188a97e3cc339535f81b&su=1' -OutFile '$PSScriptRoot\visual_cpp_build_tools_2015_update_3_x64_dvd.iso' -ThreadCount 8 -MinSliceSize 128MB
Write-Host "Microsoft Visual C++ Build Tools 14.0下载已完成,请按照目录内的“食用说明.pdf”所示,完成安装"
}
Write-Host "Pallas-Bot将会使用MongoDB存储数据(请务必安装MongoDB)"
$value = Read-Host -Prompt "请问是否需要Pallasbot-helper为博士下载MongoDB?(yes/[no])"
if($value -match '^y'){
Write-Host "开始下载MongoDB,请耐心等待(这个过程大约需要3-15分钟,依网络状况而定)"
Write-Host "如果需要手动下载并安装MongoDB请参考以下链接"
Write-Host "https://www.runoob.com/mongodb/mongodb-window-install.html"
MultiThreadDownload-File -Uri 'https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-6.0.0-signed.msi' -OutFile '$PSScriptRoot\Mongodb-6.0.0-windows-amd64-setup.msi' -ThreadCount 4 -MinSliceSize 100MB
Write-Host "MongoDB下载已完成,请按照目录内的“食用说明.pdf”所示,完成MongoDB的安装"
}else{
Write-Host "跳过MongoDB安装"
}
$DB = 1
Write-Host "已完成对运行环境的检查"
cd ..
}
else
{
net start MongoDB
# Pallas-Bot自带的requirements有问题,暂时还不清楚怎么修
cd ..
python -m pip install --upgrade pip -i https://mirror.baidu.com/pypi/simple
pip install -i https://mirror.baidu.com/pypi/simple -r requirements.txt
cd "$PSScriptRoot\nonebotPallas"
nb plugin install nonebot_plugin_apscheduler
nb plugin install gocqhttp
Write-Host "正在加载$PSScriptRoot"
nb run
}
if ($DB -eq 0) {
net stop MongoDB
}
pause