Skip to content

Commit

Permalink
[OneBot] Making API get_group_honor_info returns Onebot11 compliant (
Browse files Browse the repository at this point in the history
…#679)

* [OneBot] Making API `get_group_honor_info` returns Onebot11 compliant

* [Chore] Restore Comments
  • Loading branch information
ishkong authored Nov 12, 2024
1 parent a5e8e10 commit e1cd06b
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Lagrange.OneBot/Core/Operation/Group/GetGroupHonorInfoOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public partial class GetGroupHonorInfoOperation(TicketService ticket) : IOperati
{
{ "talkative", 1 }, { "performer", 2 }, { "legend", 3 }, { "strong_newbie", 5 }, { "emotion", 6 },
};

private static readonly Dictionary<string, string> KeyToOnebot11 = new()
{
{"uin", "user_id"}, {"name", "nickname"}, {"nick", "nickname"}, {"desc", "description"}
};

public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
Expand All @@ -35,18 +40,37 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
? HonorToType.Select(x => x.Key).ToArray()
: [honor.Type];

result.TryAdd("group_id", honor.GroupId);

foreach (string honorRaw in honors)
{
string url = $"https://qun.qq.com/interactive/honorlist?gc={honor.GroupId}&type={HonorToType[honorRaw]}";
var response = await ticket.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));
string raw = await response.Content.ReadAsStringAsync();
var match = HonorRegex().Match(raw);

if (JsonSerializer.Deserialize<JsonObject>(match.Groups[1].Value) is { } json)
if (JsonSerializer.Deserialize<JsonObject>(match.Groups[1].Value) is { } json) // 神经病
{
foreach (var (key, value) in Keys)
{
if (json[value] is { } node) result.TryAdd(key, node.Deserialize<JsonNode>()); // 神经病
if (json.Remove(value, out var node))
{
if (node is JsonObject jsonObject)
{
ProcessJsonObject(jsonObject);
}
else if (node is JsonArray jsonArray)
{
foreach (var subNode in jsonArray)
{
if (subNode is JsonObject subObject)
{
ProcessJsonObject(subObject);
}
}
}
if (key.Contains(honorRaw)) result.TryAdd(key, node);
}
}
}
}
Expand All @@ -56,4 +80,15 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa

throw new Exception();
}

private static void ProcessJsonObject(JsonObject jsonObject)
{
foreach (var oldKey in KeyToOnebot11.Keys)
{
if (jsonObject.Remove(oldKey, out var nodeValue))
{
jsonObject[KeyToOnebot11[oldKey]] = nodeValue;
}
}
}
}

0 comments on commit e1cd06b

Please sign in to comment.