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

[OneBot] Making API get_group_honor_info returns Onebot11 compliant #679

Merged
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
39 changes: 37 additions & 2 deletions Lagrange.OneBot/Core/Operation/Group/GetGroupHonorInfoOperation.cs
ishkong marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
}