Skip to content

Commit e1cd06b

Browse files
author
ishkong
authored
[OneBot] Making API get_group_honor_info returns Onebot11 compliant (#679)
* [OneBot] Making API `get_group_honor_info` returns Onebot11 compliant * [Chore] Restore Comments
1 parent a5e8e10 commit e1cd06b

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

Lagrange.OneBot/Core/Operation/Group/GetGroupHonorInfoOperation.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public partial class GetGroupHonorInfoOperation(TicketService ticket) : IOperati
2525
{
2626
{ "talkative", 1 }, { "performer", 2 }, { "legend", 3 }, { "strong_newbie", 5 }, { "emotion", 6 },
2727
};
28+
29+
private static readonly Dictionary<string, string> KeyToOnebot11 = new()
30+
{
31+
{"uin", "user_id"}, {"name", "nickname"}, {"nick", "nickname"}, {"desc", "description"}
32+
};
2833

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

43+
result.TryAdd("group_id", honor.GroupId);
44+
3845
foreach (string honorRaw in honors)
3946
{
4047
string url = $"https://qun.qq.com/interactive/honorlist?gc={honor.GroupId}&type={HonorToType[honorRaw]}";
4148
var response = await ticket.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));
4249
string raw = await response.Content.ReadAsStringAsync();
4350
var match = HonorRegex().Match(raw);
4451

45-
if (JsonSerializer.Deserialize<JsonObject>(match.Groups[1].Value) is { } json)
52+
if (JsonSerializer.Deserialize<JsonObject>(match.Groups[1].Value) is { } json) // 神经病
4653
{
4754
foreach (var (key, value) in Keys)
4855
{
49-
if (json[value] is { } node) result.TryAdd(key, node.Deserialize<JsonNode>()); // 神经病
56+
if (json.Remove(value, out var node))
57+
{
58+
if (node is JsonObject jsonObject)
59+
{
60+
ProcessJsonObject(jsonObject);
61+
}
62+
else if (node is JsonArray jsonArray)
63+
{
64+
foreach (var subNode in jsonArray)
65+
{
66+
if (subNode is JsonObject subObject)
67+
{
68+
ProcessJsonObject(subObject);
69+
}
70+
}
71+
}
72+
if (key.Contains(honorRaw)) result.TryAdd(key, node);
73+
}
5074
}
5175
}
5276
}
@@ -56,4 +80,15 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
5680

5781
throw new Exception();
5882
}
83+
84+
private static void ProcessJsonObject(JsonObject jsonObject)
85+
{
86+
foreach (var oldKey in KeyToOnebot11.Keys)
87+
{
88+
if (jsonObject.Remove(oldKey, out var nodeValue))
89+
{
90+
jsonObject[KeyToOnebot11[oldKey]] = nodeValue;
91+
}
92+
}
93+
}
5994
}

0 commit comments

Comments
 (0)