while (temp < 5) do temp = temp + 1 print("result:" .. tostring(temp)) --这里体现了类型转换的语法。 end
1 2 3
for i = 1, 3 do print("从1 到 3,当前:" .. tostring(i)) end
1 2 3
for i = 1, 6, 2 do print("高级用法 当前:" .. tostring(i) .. ",我每下次执行将跳动2次,如果当前为1那么为1,下次则为3,i>6跳出循环") end
逻辑分支
1 2 3 4 5 6 7 8 9 10 11 12
if (value1 > 10) then print("值大于10") if (value1 == 80) then print("内循环,等于80") else print("不是80,但是肯定>10") end elseif (value1 == 100) then print("等于100") else print("不知道等于夺嫂了") end
-- ---------------------机器人插件加载区 -- msgitem 消息体 -- aitelist 艾特的用户列表(不会包含自己) -- hasAite 是否艾特了用户 -- hasAiteMe 是否艾特了机器人 --收到一条消息调用此方法,返回true表示 消息由插件处理 --,否则交给机器人自身的逻辑,比如自身有点歌系统之类的。 --本方法不定义将毫无意义。 function onReceiveMsgIsNeedIntercept(msgitem, aiteList, hasAite, hasAiteMe) log("call onReceiveMsgIsNeedIntercept") local text = msgitem:getMessage() --获取收到的消息内容 log("receiveMsg:" .. text) local qq = msgitem:getSenderuin() -- 哪个qq发的消息 log("print toString[" .. msgitem:toString()) --这个方法可以打印昵称,qq,消息内容,发送时间,等等。 if (text == "LUA") then -- chunkname:41 attempt to index ? (a nil value) zhaobu local str="Wellcome Use Lua Plugin! The is QssqRobot" msgitem:setMessage(str) print("item is nil?="..tostring(msgitem:getClass()==nil)..",className:"..tostring(msgitem:getClass())) print("api obj is nil?="..tostring(msgitem:getClass()==nil)..",className:"..tostring(api:getClass())) print(api:toString()) api:showDebugToast("欢迎使用LUA机器人插件"); -- print(); -- print(msgitem:toString()) api:sendMsg(msgitem) --是啥子消息我就回复啥消息 ,操作起来比较简单。 return true --插件自身处理消息 else end info = ",aiteUser=," .. tostring(hasAite) .. ",aiteRobot:" .. tostring(hasAiteMe) --见名知意 log("state:" .. info) return false end
--下面的代码无关紧要,自己酌情修改 ,可以不定义,------------------------------ --插件被加载的时候出发 function onCreate(obj) print("onCreate....[print]") log("onCreate call arg type: " .. type(obj)) end --表示机器人最终处理的消息,即将发送的消息,是否拦截 function onReceiveRobotFinalCallMsgIsNeedIntercept(item, list, aite, haisaiteme) debug("call onReceiveRobotFinalCallMsgIsNeedIntercept.."); return false end --插件作者名字 function getAuthorName() return "情迁2018" end --返回插件名称,再插件管理列表可以显示 function getPluginName() return "test plugin" end
--返回本插件的版本号 function getVersionName() return "1.0" --返回机器人的版本号 end --返回本插件的版本号 数字标识 整数 function getVersionCode() return 1; end --返回编译时间字符串 function getBuildTime( ) -- body return "无" end
--返回插件的描述信息 function getDescript() return "这是一个DEMO" end -- 返回插件demo包名 function getPackageName() return "cn.qssq666.lua.plugindemo" end --即将被销毁调用的方法。 function onDestory() log("destory call") end