mirror of
https://gitlab.com/niansa/SomeBot.git
synced 2025-03-06 20:48:26 +01:00
Initial working code, bad error handling
This commit is contained in:
parent
db47c8c985
commit
2f6f68ddba
1 changed files with 63 additions and 10 deletions
|
@ -61,6 +61,7 @@ public:
|
|||
.addStaticFunction("new", [guild_id = baseContext.guild_id] () {
|
||||
dpp::message o;
|
||||
o.guild_id = guild_id;
|
||||
return o;
|
||||
})
|
||||
.addProperty("id", &message::id, false)
|
||||
.addProperty("channel_id", &message::channel_id, true)
|
||||
|
@ -102,8 +103,12 @@ public:
|
|||
.addProperty("is_dm", &message::is_dm)
|
||||
.addFunction("add_component", &message::add_component)
|
||||
.addFunction("add_embed", &message::add_embed)
|
||||
.addFunction("send", [cluster = &baseContext.bot->cluster] (const message *o) {
|
||||
return cluster->message_create_sync(*o);
|
||||
.addFunction("send", [cluster = &baseContext.bot->cluster] (const message *o) -> std::optional<dpp::message> {
|
||||
try {
|
||||
return cluster->message_create_sync(*o);
|
||||
} catch (...) {
|
||||
return {};
|
||||
}
|
||||
})
|
||||
.endClass()
|
||||
.beginClass<message::message_ref>("MessageRef")
|
||||
|
@ -116,7 +121,7 @@ public:
|
|||
.addProperty("id", &message::message_interaction_struct::id, false)
|
||||
.addProperty("type", &message::message_interaction_struct::type, true)
|
||||
.addProperty("name", &message::message_interaction_struct::name, true)
|
||||
.addProperty("usr", &message::message_interaction_struct::usr, true)
|
||||
.addProperty("user", &message::message_interaction_struct::usr, true)
|
||||
.endClass()
|
||||
.beginClass<message::allowed_ref>("MessageAllowedRef")
|
||||
.addProperty("parse_users", &message::allowed_ref::parse_users, true)
|
||||
|
@ -341,15 +346,9 @@ public:
|
|||
return {};
|
||||
}
|
||||
});
|
||||
|
||||
// Our globals end here
|
||||
lua_pushvalue(state, LUA_GLOBALSINDEX);
|
||||
|
||||
// We can now enable sandboxing
|
||||
luaL_sandbox(state);
|
||||
}
|
||||
|
||||
std::string execute(const std::string& source, const dpp::message *msg) {
|
||||
std::string execute(const std::string& source, const dpp::message *msg) noexcept {
|
||||
// Get state
|
||||
auto state = this->state.get();
|
||||
|
||||
|
@ -367,6 +366,57 @@ public:
|
|||
return error;
|
||||
}
|
||||
|
||||
// Create a new lua thread
|
||||
auto thread = lua_newthread(state);
|
||||
|
||||
// Prepare stack
|
||||
lua_pushvalue(state, -2);
|
||||
lua_remove(state, -3);
|
||||
lua_xmove(state, thread, 1);
|
||||
|
||||
// ... and start executing
|
||||
int status = lua_resume(thread, NULL, 0);
|
||||
|
||||
// Check status
|
||||
if (status == 0) {
|
||||
// No error
|
||||
int n = lua_gettop(thread);
|
||||
|
||||
// Check for result
|
||||
if (n) {
|
||||
// Print that result
|
||||
luaL_checkstack(thread, LUA_MINSTACK, "too many results to print");
|
||||
lua_getglobal(thread, "_PRETTYPRINT");
|
||||
// If _PRETTYPRINT is nil, then use the standard print function instead
|
||||
if (lua_isnil(thread, -1)) {
|
||||
lua_pop(thread, 1);
|
||||
lua_getglobal(thread, "print");
|
||||
}
|
||||
lua_insert(thread, 1);
|
||||
lua_pcall(thread, n, 0, 0);
|
||||
}
|
||||
|
||||
// Keep going
|
||||
} else {
|
||||
// Error!
|
||||
std::string error;
|
||||
|
||||
// Get error string
|
||||
if (status == LUA_YIELD) {
|
||||
error = "thread yielded unexpectedly";
|
||||
} else if (const char* str = lua_tostring(thread, -1)) {
|
||||
error = str;
|
||||
}
|
||||
|
||||
// Append backtrace
|
||||
error += "\nstack backtrace:\n";
|
||||
error += lua_debugtrace(thread);
|
||||
|
||||
// Clean up and return
|
||||
lua_pop(state, 1);
|
||||
return error;
|
||||
}
|
||||
|
||||
// Now, we call into the code
|
||||
luabridge::LuaRef on_call = luabridge::getGlobal(state, "on_call");
|
||||
if (!on_call.isCallable()) {
|
||||
|
@ -374,6 +424,9 @@ public:
|
|||
}
|
||||
luabridge::LuaResult result = on_call(Context{baseContext, msg});
|
||||
|
||||
// Remember to clean up!
|
||||
lua_pop(state, 1);
|
||||
|
||||
// Finally, we need to return the result
|
||||
return result.errorMessage();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue