mudlet机器人开发除了基本的触发外,还可以使用lua脚本,客户端提供了大量的功能函数,这里列出常用和实用的部分,通过这些函数足以完善多数机器人脚本需求。
提示:函数除了写在lua脚本中,也都可以在命令行使用
lua func()测试,如:lua send("hp")。这里
lua是mudlet客户端内置扩展包提供的一个别名:

指令相关
send
send(command, showOnScreen)
发送指令到游戏窗口,注意这个函数是直接发送command到游戏服务端,不会匹配客户端设置的别名,第二个参数showOnScreen 值为true/false,设置是否回显指令在屏幕上。
示例:
send("hp") --回显指令在屏幕上
send("score", false) --不显示指令在屏幕上
send("hi "..target) --使用变量
sendAll
sendAll(list of things to send, [echo back or not])
发送一系列指令到游戏,如果你是要快速行走,请用speedwalk()
示例:
sendAll("hi", "get all", "sleep") -- 发送3条指令到游戏中
speedwalk("w;s;n;e") --快速移动
denyCurrentSend
denyCurrentSend()
禁止send指令到游戏,可配合sysDataSendRequest事件使用,如下示例:
-- cancels all "eat hambuger" commands
function cancelEatHamburger(event, command)
if command == "eat hamburger" then
denyCurrentSend()
cecho("<red>Denied! Didn't let the command through.\n")
end
end
registerAnonymousEventHandler("sysDataSendRequest", "cancelEatHamburger")
getCommandSeparator
getCommandSeparator()
返回玩家配置文件正在使用的命令分隔符。
sendGMCP
sendGMCP(command)
发送GMCP数据到服务器,对支持GMCP的服务端来说,可以有更多的操作
printCmdLine
printCmdLine([name], text)
把text内容填在输入框中
内容显示相关
echo
echo([miniconsoleName or labelName], text)
此函数在当前行的末尾追加文本。相似函数还有cecho(), decho(), hecho(), insertText(), cinsertText(), 当然lua自带的print也可以使用。
display
display(content)
和echo类似的函数,在屏幕上显示文本,但和echo相比,还可以直接显示table,更适合做脚本调试。
debugc
debugc(content)
这个是调试用的指令,内容不显示在屏幕上,而是显示在错误调试终端界面。
计时器
tempTimer
tempTimer(time, code to do[, repeating])
enableTimer
enableTimer(name)
disableTimer
disableTimer(name)
killTimer
killTimer(id)
-- or disable & enable a tempTimer you've made
timerID = tempTimer(10, [[echo("hi!")]])
-- it won't go off now
disableTimer(timerID)
-- it will continue going off again
enableTimer(timerID)
remainingTime
remainingTime(timer id number or name)
permTimer
permTimer(name, parent, seconds, lua code)
用来创建永久性的计时器,注意计时器创建后默认未激活,需要使用enableTimer(name)激活。另外注意,mudlet中条目允许重名,所以多次运行permTimer会重复创建计时器,你可以使用exists()函数判断条目是否存在。
别名
tempAlias
aliasID = tempAlias(regex, code to do)
创建一个临时别名,此别名在重新启动mudlet后丢失。
permAlias
permAlias(name, parent, regex, lua code)
创建一个持久别名,该别名在重新启动Mudlet后保留,并显示在脚本编辑器中。
enableAlias
enableAlias(name)
disableAlias
disableAlias(name)
killAlias
killAlias(aliasID)
触发
feedTriggers
feedTriggers(text[, dataIsUtf8Encoded = true])
这个函数主要是用来测试触发,因为它发送的内容就像是游戏服务端发过来的内容一样,可以被客户端触发解析。
和Mudlet内置的这个别名效果一样。
模块与扩展包
getModules
getModules()
获取客户端安装的模块,可以用来自动安装或卸载模块。getPackages()是功能类似的函数。
--Check if the module myTabChat is installed and if it isn't install it and enable sync on it
if not table.contains(getModules(),"myTabChat") then
installModule(getMudletHomeDir().."/modules/myTabChat.xml")
enableModuleSync("myTabChat")
end
installModule
installModule(location)
安装模块,只能是本地文件(远程文件可以先下载再安装),可以是XML, zip, mpackage文件。
uninstallModule
uninstallModule(name)
reloadModule
reloadModule("generic-mapper")
getPackages
getPackages()
--Check if the generic_mapper package is installed and if so uninstall it
if table.contains(getPackages(),"generic_mapper") then
uninstallPackage("generic_mapper")
end
installPackage
installPackage(location or url)
安装扩展包,参数可以是本地文件,也可是网址(自动下载安装)
installPackage([[https://mud.ren/storage/mudren.mpackage.zip]])
因为可以在线安装,使用这个函数可以很容易实现一个类似
apt、brew等包管理器的一键安装机器人的平台。玩家可以用mudlet连接炎黄MUD后输入mudren install guofu就会自动安装基础的郭府打工机器人。
uninstallPackage
uninstallPackage(name)
getMudletHomeDir
getMudletHomeDir()
返回当前配置目录
相关指令:getModuleInfo, getPackageInfo, setModuleInfo, setPackageInfo
事件处理相关
关于事件,可直接看这里:mudlet机器人脚本开发基础:事件系统
registerAnonymousEventHandler
registerAnonymousEventHandler(event name, functionReference, [one shot])
注册匿名事件处理程序
killAnonymousEventHandler
killAnonymousEventHandler(handler id)
注销注册的匿名事件处理程序
raiseEvent
raiseEvent(event_name, arg-1, … arg-n)
raiseGlobalEvent
raiseGlobalEvent(event_name, arg-1, … arg-n)
内容处理相关
selectString
selectString([windowName], text, number_of_match)
replace
replace([windowName], with, [keepcolor])
setLink
setLink([windowName], command, tooltip)
如下示例,给移动方向增加可点击链接,玩家可直接点方向移动:

f
testResult = "successful"
-- old way:
echo("The test was "..testResult.."\n")
-- with f:
echo(f("The test was {testResult}\n"))
-- echoes "The test was successful\n"
TTS(Text-to-Speech)
ttsSpeak
ttsSpeak("天啦,mudlet中可以发语音")
把文字转为语音消息。
ttsQueue
ttsQueue("We begin with some text")
ttsQueue("And we continue it without interruption")
相关指令:ttsSkip, ttsPause
其它杂项
alert
alert([seconds])
让Mudlet图标在任务栏上闪烁提醒你
openWebPage
openWebPage(URL)
打开一个网页
openWebPage("https://bbs.mud.ren")
openUrl
openUrl (url)
打开一个网页
openUrl("www.mudlet.org")
connectToServer
connectToServer(host, port, [save])
连接到指定的游戏服务器。
disconnect
disconnect()
断开游戏连接
reconnect
reconnect()
重连游戏
closeMudlet
closeMudlet()
关闭游戏客户端
downloadFile
downloadFile(saveto, url)
下载文件
showColors
showColors([columns], [filterColor], [sort])
显示当前Mudlet中支持的颜色列表,颜色值存在color_table中,格式为: color_table.colorName = {r, g, b}
receiveMSP
receiveMSP(command)
--Play a cow.wav media file stored in the media folder of the current profile. The sound would play twice at a normal volume.
receiveMSP("!!SOUND(cow.wav L=2 V=50)")
--Stop any SOUND media files playing stored in the media folder of the current profile.
receiveMSP("!!SOUND(Off)")
--Play a city.mp3 media file stored in the media folder of the current profile. The music would play once at a low volume.
--The music would continue playing if it was triggered earlier by another room, perhaps in the same area.
receiveMSP([[!!MUSIC(city.mp3 L=1 V=25 C=1)]])
--Stop any MUSIC media files playing stored in the media folder of the current profile.
receiveMSP("!!MUSIC(Off)")
isActive
isActive(name, type)
检测类型为type(触发、别名、定时器、按钮或脚本)的名称name是否是激活状态,如果是返回名称,否则返回0(注意Lua中0不是逻辑假,所有数字都是逻辑真,只有false和nil是逻辑假)
isPrompt
-- make a trigger pattern with 'Lua function', and this will trigger on every prompt!
return isPrompt()
监测是否收到提示行,可以用来做一些自动的触发。注意:不推荐使用此函数,而是推荐直接使用匹配方式为提示的触发。
exists
exists(name, type)
Tells you how many things of the given type exist.The type can be 'alias', 'trigger', 'timer', 'keybind' (Mudlet 3.2+), or 'script' (Mudlet 3.17+).
Note Note: This function is only for objects created in the script editor or via perm functions. You don't need it for temp functions and will not work for them.
如果以上常用函数不能满足你的需求,可以自己去官网查更多函数的文档:
Function Categories
- Basic Essential Functions: These functions are generic functions used in normal scripting. These deal with mainly everyday things, like sending stuff and echoing to the screen.
- Database Functions: A collection of functions for helping deal with the database.
- Date/Time Functions: A collection of functions for handling date & time.
- File System Functions: A collection of functions for interacting with the file system.
- Mapper Functions: A collection of functions that manipulate the mapper and its related features.
- Miscellaneous Functions: Miscellaneous functions.
- Scripting Object Functions: A collection of arrows that manipulate Mudlets scripting objects - triggers, aliases, and so forth.
- Networking Functions: A collection of functions for managing networking.
- String Functions: These functions are used to manipulate strings.
- Table Functions: These functions are used to manipulate tables. Through them you can add to tables, remove values, check if a value is present in the table, check the size of a table, and more.
- Text to Speech Functions: These functions are used to create sound from written words. Check out our Text-To-Speech Manual for more detail on how this all works together.
- UI Functions: These functions are used to construct custom user GUIs. They deal mainly with miniconsole/label/gauge creation and manipulation as well as displaying or formatting information on the screen.
- Discord Functions: These functions are used to customize the information Mudlet displays in Discord's rich presence interface. For an overview on how all of these functions tie in together, see our Discord scripting overview.
- Additionally, more advanced functions are available in the Lua 5.1 manual.