From 9eb7da745514284d7635aabba3d5a767577e08e8 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 12 Apr 2025 16:56:15 +0500 Subject: feat: twitch_get_users function for lua --- bot/src/commands/lua.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'bot') diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp index df14181..a3ec57f 100644 --- a/bot/src/commands/lua.cpp +++ b/bot/src/commands/lua.cpp @@ -507,9 +507,58 @@ namespace bot::command::lua { o.add(u); }); - - return o; }); + + state->set_function( + "twitch_get_users", [state, &bundle](const sol::table &names) { + std::vector ids; + std::vector logins; + + for (auto &[k, v] : names) { + if (!v.is() || !k.is()) { + continue; + } + + sol::table t = v.as(); + std::string name = k.as(); + + if (name == "logins") { + for (auto &[_, x] : t) { + if (x.is()) { + logins.push_back(x.as()); + } + } + } else if (name == "ids") { + for (auto &[_, x] : t) { + if (x.is()) { + ids.push_back(x.as()); + } + } + } else { + throw std::runtime_error("Unknown key: " + name); + } + } + + if (ids.empty() && logins.empty()) { + throw std::runtime_error("No IDs or logins to search for."); + } + + auto users = bundle.helix_client.get_users(ids, logins); + + sol::table o = state->create_table(); + + std::for_each(users.begin(), users.end(), + [state, &o](const api::twitch::schemas::User &x) { + sol::table u = state->create_table(); + + u["id"] = x.id; + u["login"] = x.login; + + o.add(u); + }); + + return o; + }); } } -- cgit v1.2.3