PriceAnswer is working, much and many thanks to the help in this forumn and on the WowDev Discord. I just got a request to accept the following syntax. The person making the request changed the trigger from "price" to "?", which is fine.
Code:
?[ItemLink] (doesn't work)
? [ItemLink] (works)
The question is about removing the space for itemLinks, not itemIDs or item names, in a non-breaking way. If I can figure out how to support some or all of these without breaking the match pattern, that would be great.
The best idea I had was adding trim() to the end of the pattern.
Code:
local pattern = "^" .. (L[db.trigger]:gsub("(%W)", "%%%1")) .. ("%s*(%d*)%s*(.*)$"):trim()
Full function code as it stands without further changes:
Lua Code:
function PriceAnswer:GetOutgoingMessage(incomingMessage)
-- pattern for "price N item" incoming chat messages
local pattern = "^" .. (L[db.trigger]:gsub("(%W)", "%%%1")) .. "%s*(%d*)%s*(.*)$"
local itemCount, tail = strmatch(incomingMessage, pattern)
itemCount = itemCount and itemCount:trim()
tail = tail and tail:trim()
-- get the itemID
local itemID, retOK, ret1 -- use pcall() to validate GetItemInfoInstant()
if not itemID then
retOK, ret1 = pcall(GetItemInfoInstant, tail)
if retOK then
itemID = ret1
else
retOK, ret1 = pcall(GetItemInfoInstant, tonumber(tail))
if retOK then
itemID = ret1
end
end
end
-- the above did not get an itemID
if not itemID then
retOK, ret1 = pcall(GetItemInfoInstant, itemCount)
if retOK then
itemID = ret1
else
retOK, ret1 = pcall(GetItemInfoInstant, tonumber(itemCount))
if retOK then
itemID = ret1
end
end
end -- at this point it does not matter if there is no itemID
-- convert to a TSM item string "i:12345"
local itemString
if TSM_API and TSM_API.ToItemString then
itemString = TSM_API.ToItemString(tostring(tail))
if not itemString then
itemString = TSM_API.ToItemString(tostring(itemCount))
if itemString then
itemCount = 1
end
end
if not itemString then
if itemID then -- check if there is an itemID from the pcall()
itemString = TSM_API.ToItemString(tostring(itemID))
if not itemString then
itemString = "i:" .. tostring(itemID)
end
end
end
end
itemCount = tonumber(itemCount) or 1
if not itemCount or itemCount < 1 then
itemCount = 1
end
-- unrelated rest of the function code continues
end