Finally got a chance to look at this again and I think I have it working like I want.
I solved the issue with the login spam by starting off watching only PLAYER_ENTERING_WORLD and registering the other events when that went off after adding all the friends to the table (this seemed to be what fixed it moreso than the events). I checked and BNGetFriendInfo() returns nil for client if the person is offline so there wasn't really a need for checking if they're online or anything of that sorts.
As far as the issue with the message not showing game specific login alerts, that was because we were passing in the presence id as arg13 when we actually needed to pass that the toon id instead.
I added an extra check for printing the message to make sure the chat window is supposed to display those toast alerts first (I actually have a custom window I dedicate to whispers only and it was posting there before I did this).
When a player logs off, I think the message works because it still has a player link since they are on the desktop app so you can differentiate it between a real log off and a game log off but I am not sure if I care much for the spam personally of the log off AND log on. I mainly worry about the log on because usually I will know if I'm waiting for someone to log on but it is helpful to see when they start switching too. I think for my indecisiveness it probably calls for a toggle option and personal preference of whether or not offline messages should display.
lua Code:
local events = CreateFrame("Frame")
events:RegisterEvent("PLAYER_ENTERING_WORLD")
local friends = {}
events:SetScript("OnEvent", function(self, event)
if event == "PLAYER_ENTERING_WORLD" then
for x = 1, BNGetNumFriends() do
local id, _, _, _, _, _, client = BNGetFriendInfo(x)
friends[id] = client
end
events:RegisterEvent("BN_TOON_NAME_UPDATED")
events:RegisterEvent("BN_FRIEND_TOON_ONLINE")
events:RegisterEvent("BN_FRIEND_ACCOUNT_OFFLINE")
else
for x = 1, BNGetNumFriends() do
local id, name, _, _, _, toonid, client = BNGetFriendInfo(x)
local prev = friends[id]
if prev and prev ~= client then
local message = prev == "App" and "FRIEND_ONLINE" or client == "App" and "FRIEND_OFFLINE"
if message and toonid then
for i = 1, NUM_CHAT_WINDOWS do
local types = {GetChatWindowMessages(i)}
for k, v in pairs(types) do
if v == "BN_INLINE_TOAST_ALERT" then
ChatFrame_MessageEventHandler(_G["ChatFrame"..i], "CHAT_MSG_BN_INLINE_TOAST_ALERT", message, name, nil, 0, nil, nil, nil, nil, nil, nil, 0, nil, toonid)
break
end
end
end
end
end
friends[id] = client
end
end
end)
Now the only issue I'm having, however is if I am on the app and I log onto a toon, say Rainbowcat again, and I log off WoW and then immediately back onto Rainbowcat, no message is posted. Then, when I log off and back onto Rainbowcat and it posts.
If I started out on Rainbowcat, then logged on Niketa it would post that I logged on Niketa too but it seems that if you log on the same toon twice in a row it doesn't alert the logon.
When I was printing out some of the things, the toonid would be the same for the toon every time so I'm wondering if it's something in the Blizz code that tries to prevent double posts or something?
Anyhow, a quick fix I can think of is just if the message is "FRIEND_ONLINE" to print twice (as below) but it just seems like a workaround to me. Is there a better way to fix it or is that what it's going to come down to?
lua Code:
if message == "FRIEND_ONLINE" then
ChatFrame_MessageEventHandler(_G["ChatFrame"..i], "CHAT_MSG_BN_INLINE_TOAST_ALERT", message, name, nil, 0, nil, nil, nil, nil, nil, nil, 0, nil, toonid)
end
ChatFrame_MessageEventHandler(_G["ChatFrame"..i], "CHAT_MSG_BN_INLINE_TOAST_ALERT", message, name, nil, 0, nil, nil, nil, nil, nil, nil, 0, nil, toonid)