WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Simple addon help, sending guild message on event (https://www.wowinterface.com/forums/showthread.php?t=55278)

JTyld 03-30-17 08:20 PM

Simple addon help, sending guild message on event
 
I am trying to make an addon that does a simple checking for achievement in guild and then spits out a chat message.

So far this is what I have come up with:

Code:

local frame = CreateFrame("Frame")
frame:RegisterEvent("CHAT_MSG_GUILD_ACHIEVEMENT")
frame:SetScript("OnEvent",
  function(self, event)
      SendChatMessage("oh wow grats","GUILD")
end)

Not sure what I am missing here, but this isn't working for me. Also I am trying to come up with a way to make it only do this every X seconds (so it doesnt spam after 20 people get an achievement for example). Any suggestions? Appreciate any help.

Ketho 03-30-17 08:33 PM

It looks fine to me, did you also try turning it into an addon with http://addon.bool.no/

JTyld 03-31-17 02:33 PM

Quote:

Originally Posted by Ketho (Post 322728)
It looks fine to me, did you also try turning it into an addon with http://addon.bool.no/

That worked! Thank you! I must have jacked up something with the TOC file. Now I am just trying to figure out a timing mechanism.

Tim 03-31-17 03:21 PM

Here's the lovely C_Timer functions..

http://wow.gamepedia.com/API_C_Timer.After
http://wow.gamepedia.com/API_C_Timer.NewTimer
http://wow.gamepedia.com/API_C_Timer.NewTicker


Try to remember to give your frames unique names so it's easier to point out when further developing your addon. While naming the frame "frame" is alright with this small addon you should make it a habit to do the other.

Code:

local AchieveHandler = CreateFrame("Frame")
AchieveHandler:RegisterEvent("CHAT_MSG_GUILD_ACHIEVEMENT")
AchieveHandler:SetScript("OnEvent", function(self, event)

  C_Timer.After(10, function()
      SendChatMessage("oh wow grats", "GUILD")
  end)

end)

Change 10 to whatever delay you want.

Kanegasi 03-31-17 04:47 PM

Those timers won't work that way. If 20 people get an achievement, 20 messages will still go out but 10 seconds later.

Try this:

Code:

local autograts=CreateFrame('frame')
autograts:RegisterEvent('CHAT_MSG_GUILD_ACHIEVEMENT')
autograts:SetScript('OnEvent',function()
    autograts.trigger=true
    C_Timer.After(5,function()
        if autograts.trigger then
            autograts.trigger=false
            SendChatMessage('oh wow grats','GUILD')
        end
    end)
end)

This code will wait 5 seconds after the first event, then send only one message for any events that happened in that 5 seconds. This isn't perfect, there could be times where an event happens immediately after the message, causing any timers that were triggered earlier to push out a second message, but this way, you don't spam guild chat with 20 instant messages.

SDPhantom 04-01-17 05:22 AM

Quote:

Originally Posted by Tim (Post 322746)
Try to remember to give your frames unique names so it's easier to point out when further developing your addon. While naming the frame "frame" is alright with this small addon you should make it a habit to do the other.

The OP is actually creating a dynamic frame with no name. frame is a local variable that the widget is being stored in. As the frame isn't set up to be rendered, it won't be visible to /fstack, so naming it isn't going to provide any benefit anyway.



Quote:

Originally Posted by Kanegasi (Post 322748)
This isn't perfect, there could be times where an event happens immediately after the message, causing any timers that were triggered earlier to push out a second message

This can be fixed using separate timers for the delay and reset. This also fixes self congratulating and addresses some possible performance issues with queuing up extraneous timers.
Lua Code:
  1. local Throttle=30;--    Message throttle in seconds
  2. local Delay=1;--    Message delay in seconds
  3. local Message="Grats!!!";
  4.  
  5. --  Define this here so we aren't recreating it every time we want to send the message
  6. local function SendMessage() SendChatMessage(Message,"GUILD"); end
  7. local LastTrigger,PlayerGUID=0,nil;--   Throttle control and Player ID
  8.  
  9. local Frame=CreateFrame("Frame");
  10. Frame:RegisterEvent("PLAYER_LOGIN");
  11. Frame:RegisterEvent("CHAT_MSG_GUILD_ACHIEVEMENT");
  12. Frame:SetScript("OnEvent",function(self,event,...)
  13.     local now=GetTime();--  Cache current time for later
  14.     if event=="PLAYER_LOGIN" then-- Store our GUID on login
  15.         PlayerGUID=UnitGUID("player");
  16.     elseif event=="CHAT_MSG_GUILD_ACHIEVEMENT" and now-LastTrigger>Throttle and select(12,...)~=PlayerGUID then--   Check throttle and compare sender (we don't want to congradulate ourselves)
  17.         C_Timer.After(Delay,SendMessage);-- Send after delay
  18.         LastTrigger=now;--  Update throttle control
  19.     end
  20. end);


All times are GMT -6. The time now is 01:32 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI