Skip to content

Commit

Permalink
fix: ChattyBridge ForwardMsg Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Controllerdestiny committed Jan 20, 2025
1 parent 48ada35 commit 9fc6968
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/ChattyBridge/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace ChattyBridge;
[Config]
public class Config : JsonConfigBase<Config>
{
[LocalizedPropertyName(CultureType.English, "debug")]
[LocalizedPropertyName(CultureType.Chinese, "debug")]
public bool Debug { get; set; } = false;

[LocalizedPropertyName(CultureType.English, "forward_command")]
[LocalizedPropertyName(CultureType.Chinese, "转发指令")]
public bool ForwardCommand { get; set; } = false;
Expand Down
50 changes: 23 additions & 27 deletions src/ChattyBridge/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using LazyAPI;
using Newtonsoft.Json.Linq;
using Rests;
using System.Net;
using System.Reflection;
using System.Text;
using System.Web;
using Terraria;
using TerrariaApi.Server;
Expand All @@ -18,7 +16,7 @@ public class Plugin : LazyPlugin
public override string Author => "少司命";
public override string Description => Assembly.GetExecutingAssembly().GetName().Name!;
public override string Name => Assembly.GetExecutingAssembly().GetName().Name!;
public override Version Version => new Version(1, 0, 1, 2);
public override Version Version => new Version(1, 0, 1, 3);

private readonly HttpClient _client = new ();

Expand Down Expand Up @@ -53,15 +51,14 @@ protected override void Dispose(bool disposing)
private static object HandleMsg(RestRequestArgs args)
{
var msg = args.Parameters["msg"];
var isVer = args.Parameters["verify"] == Config.Instance.Verify;
if (!isVer)
TShock.Log.ConsoleDebug($"ChattyBridge Receive: {msg}");
if (args.Parameters["verify"] != Config.Instance.Verify)
{
return new RestObject("403");
return new RestObject("403") { Response = "ChattyBridge Token Verify Error!" };
}
try
{
var sourceMsg = Encoding.UTF8.GetString(Convert.FromBase64String(msg));
var json = JObject.Parse(sourceMsg);
var json = JObject.Parse(msg);
if (json.TryGetValue("type", out var type))
{
switch (type.ToString())
Expand Down Expand Up @@ -93,25 +90,23 @@ private static object HandleMsg(RestRequestArgs args)
}
catch (Exception ex)
{
TShock.Log.ConsoleError(ex.ToString());
return new RestObject("500");
return new RestObject("500") { Response = $"An error occurred in the processing of the message: {ex.Message}" };
}
return new RestObject("200");
return new RestObject("200") { Response = "Message Send Successfully!" };
}

private void SendMsg(string msg)
{
Task.Run(() =>
Task.Run(async () =>
{
var baseStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(msg));
foreach (var host in Config.Instance.RestHost)
{
try
{
var url = $"http://{host}/chat";
this.HttpGet(url, new Dictionary<string, string>
await this.HttpGet(url, new Dictionary<string, string>
{
{ "msg", baseStr },
{ "msg", msg },
{ "verify", Config.Instance.Verify }
});
}
Expand All @@ -123,7 +118,7 @@ private void SendMsg(string msg)
});
}

private void HttpGet(string url, Dictionary<string, string> payload)
private async Task HttpGet(string url, Dictionary<string, string> payload)
{
var urlBuilder = new UriBuilder(url);
var param = HttpUtility.ParseQueryString(urlBuilder.Query);
Expand All @@ -132,18 +127,19 @@ private void HttpGet(string url, Dictionary<string, string> payload)
param[key] = value;
}
urlBuilder.Query = param.ToString();
var response = this._client.Send(new HttpRequestMessage(HttpMethod.Get, urlBuilder.ToString()));
switch (response.StatusCode)
var response = await this._client.GetAsync(urlBuilder.ToString());
try
{
TShock.Log.ConsoleDebug($"ChattyBridge Send: {payload["msg"]}");
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
TShock.Log.ConsoleError($"[ChattyBridge] Error: {e.Message}");
}
finally
{
case HttpStatusCode.OK:
break;
case HttpStatusCode.Unauthorized:
TShock.Log.ConsoleError(GetString($"[聊天桥] 访问目标服务器验证失败:{url},请检查你的令牌是否配置正确!"));
break;
case HttpStatusCode.InternalServerError:
TShock.Log.ConsoleError(GetString($"[聊天桥] 目标服务器处理请求出错:{url}!"));
break;

TShock.Log.ConsoleDebug($"ChattyBridage Response: {await response.Content.ReadAsStringAsync()}");
}
}

Expand Down

0 comments on commit 9fc6968

Please sign in to comment.