diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-12 16:56:15 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-12 16:56:15 +0500 |
| commit | 9eb7da745514284d7635aabba3d5a767577e08e8 (patch) | |
| tree | 058075a36bb6447b7995965685907b35df680c01 /bot/src/commands/lua.cpp | |
| parent | 9fc268e10ec78830df8ebb6b60f201790e5d5d6f (diff) | |
feat: twitch_get_users function for lua
Diffstat (limited to 'bot/src/commands/lua.cpp')
| -rw-r--r-- | bot/src/commands/lua.cpp | 53 |
1 files changed, 51 insertions, 2 deletions
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<int> ids; + std::vector<std::string> logins; + + for (auto &[k, v] : names) { + if (!v.is<sol::table>() || !k.is<std::string>()) { + continue; + } + + sol::table t = v.as<sol::table>(); + std::string name = k.as<std::string>(); + + if (name == "logins") { + for (auto &[_, x] : t) { + if (x.is<std::string>()) { + logins.push_back(x.as<std::string>()); + } + } + } else if (name == "ids") { + for (auto &[_, x] : t) { + if (x.is<std::string>()) { + ids.push_back(x.as<int>()); + } + } + } 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; + }); } } |
