使用Lua编写的Smiles机器人:文本匹配

这是我的机器人,在游戏聊天中复制表情。

它复制的笑脸保存在名为“emoticons”的表中。如果有人写“:)”,机器人就会写“:)”,以此类推

循环内的代码是为了应对这种情况:如果有人写了“>:)”,你必须让脚本复制“>:)”而不是仅仅是“:)”

CreateFrameRegisterEventSetScriptSendChatMessage都是游戏内置的Lua API

local emoticons = {
    ":)", "(:", ":))", ">:)", "0:)", ":D", ":]", ":)))", "=]", "?_?", "+.+", ":P", ":3", "^^", "roar", ":V", "D:", ":C", ".D", ".)", "o_o",
    "^-^", ":PPP", ":DDD", ":D:D:D", ":DDDD", ":D:D:D:D", ":DDDDD", ":d", ":L", "<O>_<O>", "o/", "+_+", "?_?", "*0*", ":}", ";)", ":))))", "o.o", "<.<''", ":|",
    ":-)", "^^^^", ":D:D:D:D:D:D", ":D :D :D", "^^^", ":c", ";]", ":9", ">:|", ">.<", ";3", ";P", "T_T", ":3c", ":)))))",
    "^^^^^" }

local f = CreateFrame("Frame")
f:RegisterEvent("CHAT_MSG_GUILD")
f:SetScript("OnEvent", function(self, event, text, playerName, _, channelName, playerName2, _, _, channelIndex)
    local msg
    local n = 0
        for x, key in ipairs(emoticons) do
            local l = string.len(emoticons[x])
                if (string.sub(text, -l) == emoticons[x]) then
                    if (l > n) then
                        msg = emoticons[x]
                        n = l
                    end
                end
        end
    if (msg) and (playerName ~= UnitName("player")) then
        if (event == "CHAT_MSG_GUILD") then SendChatMessage(msg, "GUILD", nil, channelIndex) end
    end
end)

有没有改善的方法?例如,如果有人写了

"^^^^^^"

机器人会复制

"^^^^^"

因为它存储在表中时只有一个“^”

我的目标是:如果有人写了“^^^^^^”,并且它没有在表中注册,脚本将不会回应

原文链接 https://stackoverflow.com/questions/70351775

点赞
stackoverflow用户12135804
stackoverflow用户12135804

你最好学习如何使用 string.gmatch

举个例子,假设你的表情符号表中只存储了一个 ':D' 实例。你可以遍历所有匹配项并作出相应的回应。以下是一个小示例:

local text = ':D:D:D'
local count = 0
for w in string.gmatch(text, ':D') do
   count = count + 1
end
if count > 0 then
    local response = ''
    for i = 1, count do
        response = response .. ':D'
    end
    print(response) -- 打印 ':D:D:D'
end

这并不能处理所有情况,但希望它能帮助你入门。 :D

2021-12-14 16:33:40