Last active
August 24, 2020 09:33
-
-
Save lxfly2000/9c2ba9599c9786ebf6cddb7b7b827292 to your computer and use it in GitHub Desktop.
Lua机器人示例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--发送信息: | |
--send_message(group_id,discuss_id,user_id,msg) | |
--该函数返回发送信息文本的长度 | |
--收到信息时的回调函数 | |
--如果成功返回0,其他值表示出错 | |
function on_receive_message(group_id,discuss_id,user_id,msg) | |
if #msg==0 then | |
msg="[未输入]" | |
end | |
msg="用户:"..user_id.."<"..msg | |
if discuss_id~=0 then | |
msg="讨论组:"..discuss_id.." "..msg | |
end | |
if group_id~=0 then | |
msg="群:"..group_id.." "..msg | |
end | |
send_message(group_id,discuss_id,user_id,msg) | |
return 0 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include "lua.hpp" | |
#pragma comment(lib,"lua54.lib") | |
#define PushFunctionToLua(lua_state,lua_cfunction) lua_register(lua_state,_CRT_STRINGIZE(lua_cfunction),lua_cfunction) | |
#define LUA_CFUNCTION(function_name,lua_state) int function_name(lua_State* lua_state) | |
lua_State* luaState = nullptr; | |
//调用Lua函数 | |
//on_receive_message(group_id,discuss_id,user_id,msg) | |
lua_Integer lua_call_on_receive_message(int64_t group_id,int64_t discuss_id,int64_t user_id,std::string msg) | |
{ | |
lua_getglobal(luaState, "on_receive_message"); | |
//从左到右push参数 | |
lua_pushinteger(luaState, group_id); | |
lua_pushinteger(luaState, discuss_id); | |
lua_pushinteger(luaState, user_id); | |
lua_pushstring(luaState, msg.c_str()); | |
if (lua_pcall(luaState, 4, 1, 0))//调用完成后会自动把函数和参数从栈中清除,因此下方的-1改成1也可以 | |
{ | |
puts(lua_tostring(luaState, -1));//lua_pcall出错时会返回一个错误对象 | |
return -1; | |
} | |
return lua_tointeger(luaState, -1); | |
} | |
//在Lua中调用的C函数 | |
//send_message(group_id,discuss_id,user_id,msg) | |
LUA_CFUNCTION(send_message,L) | |
{ | |
//获取参数 | |
int64_t group_id = lua_tointeger(L, 1); | |
int64_t discuss_id = lua_tointeger(L, 2); | |
int64_t user_id = lua_tointeger(L, 3); | |
std::string msg = lua_tostring(L, 4); | |
//函数实际代码 | |
std::cout << msg << std::endl; | |
//push返回值 | |
lua_pushinteger(L, msg.length());//被调用时不用清栈,直接放入返回值就行 | |
return 1;//这个函数返回1个值 | |
} | |
int InitLua() | |
{ | |
int e = 0; | |
luaState = luaL_newstate();//创建Lua环境 | |
if (!luaState) | |
{ | |
std::cerr << "创建Lua失败。\n"; | |
return -1; | |
} | |
luaL_openlibs(luaState);//加载Lua自带库 | |
if (LUA_OK != (e = luaL_dofile(luaState, "bot.lua")))//执行Lua文件 | |
{ | |
std::cerr << "执行文件失败,错误:" << e << std::endl; | |
return -2; | |
} | |
PushFunctionToLua(luaState, send_message);//将回调函数推至Lua | |
return 0; | |
} | |
void UninitLua() | |
{ | |
lua_close(luaState); | |
} | |
int main() | |
{ | |
if (InitLua()) | |
{ | |
std::cerr << "错误 InitLua."; | |
return -1; | |
} | |
std::cout << "Lua 交互示例\n输入文字后按 Enter.\n"; | |
std::string input; | |
lua_Integer e = 0; | |
do | |
{ | |
std::cout << ">"; | |
std::getline(std::cin, input); | |
if (e = lua_call_on_receive_message(0, 0, 0, input)) | |
{ | |
std::cout << "错误 on_receive_message:" << e; | |
break; | |
} | |
} while (input.length()); | |
UninitLua(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment