Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新: 修改CaiBot端口号 添加被动绑定CaiBot #367

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions CaiBot/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
//恋恋的TShock插件模板,有改动(为了配合章节名)
//来自棱镜的插件教程

using System.Net;
using System.Net.WebSockets;
using System.Reflection;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Terraria;
using Terraria.Localization;
using TerrariaApi.Server;
Expand All @@ -20,7 +22,7 @@ public class Plugin : TerrariaPlugin
public override string Author => "Cai,羽学";
public override string Description => "CaiBot机器人的适配插件";
public override string Name => "CaiBotPlugin";
public static readonly Version VersionNum = new(2024, 8, 1, 1); //日期+版本号(0,1,2...)
public static readonly Version VersionNum = new(2024, 8, 2, 1); //日期+版本号(0,1,2...)
public override Version Version => VersionNum;

//插件的构造器
Expand Down Expand Up @@ -65,13 +67,33 @@ public override void Initialize()
try
{
WebSocket = new ClientWebSocket();
while (Config.config.Token == "") await Task.Delay(TimeSpan.FromSeconds(5));
while (Config.config.Token == "")
{
await Task.Delay(TimeSpan.FromSeconds(10));
HttpClient client = new();
HttpResponseMessage? response;
client.Timeout = TimeSpan.FromSeconds(5.0);
response = client.GetAsync($"http://api.terraria.ink:22334/bot/get_token?" +
$"code={InitCode}")
.Result;
//TShock.Log.ConsoleInfo($"[CaiAPI]尝试被动绑定,状态码:{response.StatusCode}");
if (response.StatusCode == HttpStatusCode.OK && Config.config.Token == "")
{
string responseBody = await response.Content.ReadAsStringAsync();
JObject json = JObject.Parse(responseBody);
string token = json["token"]!.ToString();
Config.config.Token = token;
Config.config.Write();
TShock.Log.ConsoleInfo($"[CaiAPI]被动绑定成功!");
}

}

if (Terraria.Program.LaunchParameters.ContainsKey("-cailocalbot"))
await WebSocket.ConnectAsync(new Uri("ws://127.0.0.1:22333/bot/" + Config.config.Token),
await WebSocket.ConnectAsync(new Uri("ws://127.0.0.1:22334/bot/" + Config.config.Token),
CancellationToken.None);
else
await WebSocket.ConnectAsync(new Uri("ws://api.terraria.ink:22333/bot/" + Config.config.Token),
await WebSocket.ConnectAsync(new Uri("ws://api.terraria.ink:22334/bot/" + Config.config.Token),
CancellationToken.None);
while (true)
{
Expand Down Expand Up @@ -136,6 +158,7 @@ private bool MessageBuffer_InvokeGetData(On.OTAPI.Hooks.MessageBuffer.orig_Invok
if (!string.IsNullOrEmpty(Config.config.Token))
{
NetMessage.SendData(2, instance.whoAmI, -1, NetworkText.FromFormattable("exist"));
TShock.Log.ConsoleInfo($"[CaiAPI]试图绑定已绑定服务器!");
return false;
}

Expand All @@ -148,6 +171,8 @@ private bool MessageBuffer_InvokeGetData(On.OTAPI.Hooks.MessageBuffer.orig_Invok
NetMessage.SendData(2, instance.whoAmI, -1, NetworkText.FromFormattable(token));
Config.config.Token = token;
Config.config.Write();
TShock.Log.ConsoleInfo($"[CaiAPI]主动绑定成功!");
return false;
}
else
{
Expand All @@ -171,4 +196,4 @@ protected override void Dispose(bool disposing)

base.Dispose(disposing);
}
}
}
2 changes: 2 additions & 0 deletions CaiBot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ v2024.7.24.1 优化代码、发送心跳包、修复进度显示错误
v2024.7.24.2 修复心跳包错误
v2024.7.25.1 更新: CaiBot并行处理数据包,以防止API调用超时
v2024.8.1.1 删除不必要的依赖
v2024.8.1.2 修改Caibot端口
v2024.8.2.1 添加被动添加BOT
```

## 指令
Expand Down
Loading