Is This code correct?

trying to write a simple addon that adds messages to a chat pane named “AH”

this doesnt fire,i need to know why

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION",OnEvent)
f:SetScript("OnEvent", OnEvent)

thisa is entire script

local function getAuctionTab
  local tabName = "AH"
  for _,name in pairs(CHAT_FRAMES) do
    local frame = _G[name]
    if frame.name == tabName then
      return frame
    end
  end
end

local function OnEvent(notification, msg, auctionID) 
//0 	BidPlaced 	
//1 	AuctionRemoved 	
//2 	AuctionWon 	
//3 	AuctionOutbid 	
//4 	AuctionSold 	
//5 	AuctionExpired 	
    local color = ChatTypeInfo["SYSTEM"]
	local auctionTab == getAuctionTab
	//if auctionID == (0 || 1 || 2 || 3 || 4 || 5 || 6) then
    if auctionID == 1 then 	
       auctionTab:AddMessage(msg,color.r,color.g,color.b) 
	   print(msg)
    end 
	if msg == ERR_AUCTION_REMOVED_S then -- "Your auction of %s has expired."
       auctionTab:AddMessage(msg,color.r,color.g,color.b)
	   print(msg)
	   print("test")
    end 
	print(msg)
	print(auctionID)
	print(notification)
end

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION",OnEvent)
f:SetScript("OnEvent", OnEvent)

You have some code that is not lua so if it’s what you are actually using it will be throwing errors. Install:
https://www.curseforge.com/wow/addons/bug-grabber
and:
https://www.curseforge.com/wow/addons/bugsack
To make your addon coding life easier

The other major problem is that an OnEvent handler has two inital arguments that the Wiki event descriptions don’t list (because thay are the same for all events).

  1. The frame processing the evet (self)
  2. The event received (event)
    and then the rest of the payload if there is one.
local function getAuctionTab()
    local tabName = "AH"
    for _,name in pairs(CHAT_FRAMES) do
        local frame = _G[name]
        if frame.name == tabName then
            return frame
        end
    end
end

local function OnEvent(self, event, notification, msg, auctionID)-- self and event are always the first 2 arguments of OnEvent handler
-- No idea what these are supposed to be, comments?
-- //0 	BidPlaced 	
-- //1 	AuctionRemoved 	
-- //2 	AuctionWon 	
-- //3 	AuctionOutbid 	
-- //4 	AuctionSold 	
-- //5 	AuctionExpired 	

    local color = ChatTypeInfo["SYSTEM"]
    local auctionTab = getAuctionTab()
    if not auctionTab then -- No AH tab
    	print("No tab named AH found!")
    	self:UnregisterAllEvent() -- Stop Processing the event
        return
    end
    if auctionID > 6 then -- I think that's what your code was trying to do?
        return
    end
    if auctionID == 1 then 	
        auctionTab:AddMessage(msg,  color.r,  color.g,  color.b) 
        print("Test 1:", msg)
        -- maybe need a return here?
    end
    if msg == ERR_AUCTION_REMOVED_S then -- "Your auction of %s has expired."
        auctionTab:AddMessage(msg, color.r, color.g, color.b)
        print("Test 2:", msg)
        -- maybe need a return here also?
    end 
    print("Test 3:", msg, auctionID, notification)
end
1 Like

thanks again, been a big help,this is what i have now,does everything cept the message,i wonder if it is correct?

local function getAuctionTab()
    local tabName = "AH"
    for _,name in pairs(CHAT_FRAMES) do
        local frame = _G[name]
        if frame.name == tabName then
            return frame
        end
    end
end

local function OnEvent(self, event, notification, text, auctionID)-- self and event are always the first 2 arguments of OnEvent handler
--0 BidPlaced 	
--1 AuctionRemoved 	
--2 AuctionWon 	
--3 AuctionOutbid 	
--4 AuctionSold 	
--5 AuctionExpired 	

    local color = ChatTypeInfo["SYSTEM"]
    local auctionTab = getAuctionTab()
    if notification >= 0 and notification <= 5 then 	
        auctionTab:AddMessage(text,  color.r,  color.g,  color.b) 
        --print("Test 1:", notification)
        --return
    end
    print("Ah Text:", text)
end

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION", notification, text, auctionID)
f:SetScript("OnEvent", OnEvent)

So long as the auctionTab exists, has a size (width/height), is positioned on-screen and is shown/visible, it should be ok.

You don’t need the line

if notification >= 0 and notification <= 5 then
    ...
end

as notification can’t be nil and is always going to be between 0 and 5

thank you once again

everything cept for the the text input works,it is adding time stamp w/o any strings

local function getAuctionTab()
    local tabName = "AH"
    for _,name in pairs(CHAT_FRAMES) do
        local frame = _G[name]
        if frame.name == tabName then
            return frame
        end
    end
end

local function OnEvent(self, event, notification, text, auctionID)-- self and event are always the first 2 arguments of OnEvent handler
    local color = ChatTypeInfo["SYSTEM"]
    local auctionTab = getAuctionTab()
    auctionTab:AddMessage(text,  color.r,  color.g,  color.b)
    print("Ah Text:", text)
end

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION", notification, text, auctionID)
f:SetScript("OnEvent", OnEvent)

Try:

auctionTab:AddMessage(text,  color.r,  color.g,  color.b, color.id)

You could also add:

DEFAULT_CHAT_FRAME:AddMessage(text,  color.r,  color.g,  color.b, color.id)

to see if it gets to the default chat frame intact.

this is the debug

https://ibb.co/6ttMkYQ

and added to AH tab

https://ibb.co/c8hDBvM

i think the bliz function is bugged

ps:sorry it wont let me upload or link to a pic

Wrap the links in code tags and you won’t need to butcher them :wink:

local function getAuctionTab()
    local tabName = "AH"
    for _,name in pairs(CHAT_FRAMES) do
        local frame = _G[name]
        if frame.name == tabName then
            return frame
        end
    end
end

local function OnEvent(self, event, ...)-- self and event are always the first 2 arguments of OnEvent handler
    local color = ChatTypeInfo["SYSTEM"]
    local auctionTab = getAuctionTab()
    if event == "AUCTION_HOUSE_AUCTION_CREATED" then
        auctionTab:AddMessage(ERR_AUCTION_STARTED, color.r,  color.g,  color.b, color.id)
    else
        local auctionHouseNotification, formatArg = ...
        auctionTab:AddMessage(ChatFrameUtil.GetAuctionHouseNotificationText(auctionHouseNotification, formatArg), color.r,  color.g,  color.b, color.id)
    end
end

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION")
f:RegisterEvent("AUCTION_HOUSE_AUCTION_CREATED")
f:SetScript("OnEvent", OnEvent)

tytytytytyt!

edit :

works perfect now thanks dude,once again,this is finnal code

local function getAuctionTab()
    local tabName = "AH"
    for _,name in pairs(CHAT_FRAMES) do
        local frame = _G[name]
        if frame.name == tabName then
            return frame
        end
    end
end

local function OnEvent(self, event, ...) -- self and event are always the first 2 arguments of OnEvent handler
    local color = ChatTypeInfo["SYSTEM"]
    local auctionTab = getAuctionTab()
	if event == "AUCTION_HOUSE_SHOW_COMMODITY_WON_NOTIFICATION" then
	   local commodityName, commodityQuantity = ...
	   local message = "You won an auction for "..commodityName.." (x"..commodityQuantity..")" 
	   auctionTab:AddMessage(message, color.r,  color.g,  color.b, color.id)
       --print(message) 
	end 
	if event == "AUCTION_HOUSE_AUCTION_CREATED" then
       auctionTab:AddMessage(ERR_AUCTION_STARTED, color.r,  color.g,  color.b, color.id)
	   --print("Create Fired") 
	end
	if event == "AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION" then
	   local auctionHouseNotification, formatArg = ...
	   auctionTab:AddMessage(ChatFrameUtil.GetAuctionHouseNotificationText(auctionHouseNotification, formatArg), color.r,  color.g,  color.b, color.id)
       --print("Notify Fired", auctionHouseNotification, formatArg) 
	end 
	--print("Onevent Fired") 
end

local f = CreateFrame("Frame")
f:RegisterEvent("AUCTION_HOUSE_SHOW_FORMATTED_NOTIFICATION")
f:RegisterEvent("AUCTION_HOUSE_AUCTION_CREATED")
f:RegisterEvent("AUCTION_HOUSE_SHOW_COMMODITY_WON_NOTIFICATION")
f:SetScript("OnEvent", OnEvent)