The way you're handling GUILD_ROSTER_UPDATE seems pretty weird in general. Rather than imposing an arbitrary 5-second delay, I'd suggest re-registering it only after all actions have completed. Change:
Code:
-- Start it again if we did something.
if didAction then
self:Play()
end
To:
Code:
if didAction then
-- Start it again if we did something.
self:Play()
else
-- All done, start listening again:
caelCore.management:RegisterEvent("GUILD_ROSTER_UPDATE")
end
Then get rid of all the PLAYER_ENTERING_WORLD event stuff, and change your GUILD_ROSTER_UPDATE code to:
Code:
self:UnregisterEvent("GUILD_ROSTER_UPDATE")
-- No need to do this function call and comparison twice:
local inTheGuild = GetGuildInfo("player") == "We Did It"
if inTheGuild then
SetGuildRosterShowOffline(true)
GuildRosterShowOfflineButton:Disable()
end
if GuildFrame and not GuildFrame:IsVisible() then
SortGuildRoster("online")
CalendarManagement()
if inTheGuild and (CanGuildPromote() or CanGuildRemove()) then
-- The timer will reregister the event when it's done:
return timer:Play()
end
end
-- If we got this far we didn't run the timer, so no more changes will be made:
self:RegisterEvent("GUILD_ROSTER_UPDATE")
Also:
(1) Instead of "if event == A then X end, if event == B then Y end" you should use "if event == A then X
elseif event == B then Y end", so you're not processing a bunch of
if checks that you already know won't pass.
(2) Don't use
LoadAddOn directly on Blizzard addons. The calendar, in particular, can break if you load it too early. Instead, just listen for those addons' ADDON_LOADED events and delay running any code that depends on them until it fires. If you absolutely
need to run that code without waiting for the user to open the calendar, use Blizzard's special methods, such as
Calendar_LoadUI() and
GuildFrame_LoadUI(), to ensure that any dependencies get loaded in the right order.
(3) Rather than using SPELLS_CHANGED as an initialization event, you should really use an event that's actually intended for initialization purposes, such as your addon's own ADDON_LOADED event, or PLAYER_LOGIN.
(4) Why on earth are you using a
while loop with a manually-incremented index, instead of
for i = 1, GetNumGuildMembers() ?