mirror of
https://gitlab.com/niansa/SomeBot.git
synced 2025-03-06 20:48:26 +01:00
Further Lua binding additions
This commit is contained in:
parent
a5eaf53f05
commit
c228ce8b44
1 changed files with 66 additions and 5 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <exception>
|
||||
#include <thread>
|
||||
#include <memory>
|
||||
#include <ctime>
|
||||
|
@ -39,6 +40,9 @@ public:
|
|||
// Get state
|
||||
auto state = this->state.get();
|
||||
|
||||
// Enable exceptions
|
||||
luabridge::LuaException::enableExceptions(state);
|
||||
|
||||
// Load initial libraries
|
||||
luaL_openlibs(state);
|
||||
|
||||
|
@ -310,6 +314,13 @@ public:
|
|||
return {};
|
||||
}
|
||||
})
|
||||
.addFunction("edit", [cluster = &baseContext.bot->cluster] (const channel *o) -> std::optional<dpp::channel> {
|
||||
try {
|
||||
return cluster->channel_edit_sync(*o);
|
||||
} catch (...) {
|
||||
return {};
|
||||
}
|
||||
})
|
||||
.endClass()
|
||||
.beginClass<user>("User")
|
||||
.addProperty("id", &user::id, false)
|
||||
|
@ -361,6 +372,29 @@ public:
|
|||
.addProperty("user", &guild_member::get_user)
|
||||
.addFunction("get_avatar_url", &guild_member::get_avatar_url)
|
||||
.addFunction("mention", &guild_member::get_mention)
|
||||
.addFunction("kick", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild_member *o) -> bool {
|
||||
try {
|
||||
cluster->guild_member_kick_sync(o->user_id, guild_id);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addFunction("ban", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild_member *o, unsigned delete_message_settings) -> bool {
|
||||
try {
|
||||
cluster->guild_ban_add_sync(guild_id, o->user_id, delete_message_settings);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addFunction("edit", [cluster = &baseContext.bot->cluster] (const guild_member *o) -> std::optional<guild_member> {
|
||||
try {
|
||||
return cluster->guild_edit_member_sync(*o);
|
||||
} catch (...) {
|
||||
return {};
|
||||
}
|
||||
})
|
||||
.endClass()
|
||||
.beginClass<guild>("Guild")
|
||||
.addProperty("id", &guild::id, false)
|
||||
|
@ -439,6 +473,29 @@ public:
|
|||
.addFunction("get_base_user_permissions", luabridge::overload<const user*>(&guild::base_permissions))
|
||||
.addFunction("get_base_member_permissions", luabridge::overload<const guild_member &>(&guild::base_permissions))
|
||||
.addFunction("get_permission_overwrites", luabridge::overload<const guild_member &, const channel &>(&guild::permission_overwrites))
|
||||
.addFunction("kick", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild *, snowflake user_id) -> bool {
|
||||
try {
|
||||
cluster->guild_member_kick_sync(user_id, guild_id);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addFunction("ban", [guild_id = baseContext.guild_id, cluster = &baseContext.bot->cluster] (const guild *, snowflake user_id, unsigned delete_message_settings) -> bool {
|
||||
try {
|
||||
cluster->guild_ban_add_sync(guild_id, user_id, delete_message_settings);
|
||||
return true;
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.addFunction("edit", [cluster = &baseContext.bot->cluster] (const guild *o) -> std::optional<guild> {
|
||||
try {
|
||||
return cluster->guild_edit_sync(*o);
|
||||
} catch (...) {
|
||||
return {};
|
||||
}
|
||||
})
|
||||
.endClass()
|
||||
.beginClass<utility::iconhash>("IconHash")
|
||||
.addProperty("first", &utility::iconhash::first, true)
|
||||
|
@ -525,13 +582,13 @@ public:
|
|||
|
||||
// Get error string
|
||||
if (status == LUA_YIELD) {
|
||||
error = "thread yielded unexpectedly";
|
||||
error = "Thread yielded unexpectedly";
|
||||
} else if (const char* str = lua_tostring(state, -1)) {
|
||||
error = str;
|
||||
}
|
||||
|
||||
// Append backtrace
|
||||
error += "\nstack backtrace:\n";
|
||||
error += "\nStack backtrace:\n";
|
||||
error += lua_debugtrace(state);
|
||||
|
||||
// Clean up and return
|
||||
|
@ -544,10 +601,14 @@ public:
|
|||
if (!on_call.isCallable()) {
|
||||
return "'on_call' is not callable";
|
||||
}
|
||||
luabridge::LuaResult result = on_call(Context{baseContext, msg});
|
||||
try {
|
||||
luabridge::LuaResult result = on_call(Context{baseContext, msg});
|
||||
} catch (const luabridge::LuaException& e) {
|
||||
return e.what();
|
||||
}
|
||||
|
||||
// Finally, we need to return the result
|
||||
return result.errorMessage();
|
||||
// Finally, everything seems alright
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue