【问题标题】:Smiles bot in Lua: matching textLua 中的微笑机器人:匹配文本
【发布时间】:2022-01-17 22:27:40
【问题描述】:

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

它复制的微笑保存在名为“表情”的表格中。如果有人写“:)”,机器人会写“:)”等等

循环内的代码是这样的:如果有人写,例如,>:)”,你必须复制脚本“>:)”,而不仅仅是“:)”

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)
    

有什么办法可以改善吗?例如,如果有人写

“^^^^^^”

机器人复制

“^^^^^”

这将与存储在表中的“^”少一个相同

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

【问题讨论】:

  • 还有为什么你有 ":D :D :D" ?简单地回应三个单曲似乎没有区别:D。这无缘无故地使事情复杂化
  • 如何处理消息中的多个表情符号?你似乎回应了最后一场比赛

标签: string lua string-matching


【解决方案1】:

你最好学习如何使用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) -- prints ':D:D:D'
end

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

【讨论】:

    猜你喜欢
    • 2011-06-12
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    相关资源
    最近更新 更多