1
0
Fork 0
mirror of https://gitlab.com/niansa/SomeBot.git synced 2025-03-06 20:48:26 +01:00

Bound get_*_sync functions to Lua

This commit is contained in:
niansa 2023-03-02 19:11:48 +01:00
parent 9ed6fafe70
commit 0c985f4bed

View file

@ -40,14 +40,12 @@ public:
// Load initial libraries
luaL_openlibs(state);
// Do bindings
using namespace dpp;
luabridge::getGlobalNamespace(state)
.beginClass<Context>("Context")
.addProperty("guild_id", &Context::guild_id, false)
.addProperty("bot", &Context::bot, false)
.endClass()
.beginClass<Bot>("Bot")
//.addFunction()
.addProperty("message", &Context::msg, true)
.endClass()
.beginClass<snowflake>("Snowflake")
.addConstructor<void (const uint64_t&), void (const std::string&)>()
@ -301,7 +299,39 @@ public:
.addProperty("user", &guild_member::get_user)
.addFunction("get_avatar_url", &guild_member::get_avatar_url)
.addFunction("mention", &guild_member::get_mention)
.endClass();
.endClass()
.addFunction("fetch_guild", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] () -> std::optional<dpp::guild> {
try {
return cluster->guild_get_sync(guild_id);
} catch (...) {
return {};
}
})
.addFunction("fetch_channel", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const dpp::snowflake id) -> std::optional<dpp::channel> {
try {
auto channel = cluster->channel_get_sync(id);
if (channel.guild_id != guild_id) {
return {};
}
return channel;
} catch (...) {
return {};
}
})
.addFunction("fetch_user", [cluster = &baseContext.bot->cluster] (const dpp::snowflake id) -> std::optional<dpp::user> {
try {
return cluster->user_get_sync(id);
} catch (...) {
return {};
}
})
.addFunction("fetch_member", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const dpp::snowflake user_id) -> std::optional<dpp::guild_member> {
try {
return cluster->guild_get_member_sync(guild_id, user_id);
} catch (...) {
return {};
}
});
// Our globals end here
lua_pushvalue(state, LUA_GLOBALSINDEX);