View Single Post
05-26-09, 04:57 PM   #15
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Although, here's an update that doesn't increment the FOTD every whisper but still whispers random facts:

Code:
if not FactDB then FactDB={" "} end 
local key = "fact"  -- whispered command to get current fact



function Facts_OnLoad()        
    SLASH_FACTS1 = "/fact"      
    SlashCmdList["FACTS"] = Facts_Command
    this:RegisterEvent("CHAT_MSG_WHISPER")
end

function Facts_Command(msg)    
    if IsInGuild() then
        local cmd,newFact = Facts_GetCommand(msg)
        if cmd == "reset" then    
            FactDB.Count = 1 
        end
        if cmd == "next" then 
            FactDB.Count = FactDB.Count + 1
            Facts_ShowFact()
        end
        if cmd == "new" then
            if newFact ~= "" then
                pos = table.getn(FactDB) + 1
                FactDB[pos] = newFact
            else
                message("Please add a new fact after new")
            end
        end
        if cmd == "" then
            Facts_ShowFact()
        end
        
    end
end

local function SendSplitMessages(message, player) --player is optional, name of the player to whisper -- assumes Guild chat if you don't send it anything
   message = strtrim(message)
   local msg, chan = strsplit(" ",message), player and "WHISPER" or "GUILD"
   for i=2, select("#", strsplit(" ",message)) do
       local new = select(i, strsplit(" ",message))
       if not new then
          SendChatMessage(msg, chan, nil, player)
          return
       end
       if strlen(msg) + strlen(new) > 254 then
          SendChatMessage(msg, chan, nil, player)
          msg = ""
       end
       msg = msg .. " " .. new
   end
   if msg and msg ~= "" and msg ~= " " then
       SendChatMessage(msg, chan, nil, player)
   end
end

function Facts_ShowFact(sender)
    if FactDB.Count > table.getn(FactDB) then 
        FactDB.Count = 1
    end
    local FactOfTheDay = FactDB[FactDB.Count]
    if sender then
        SendSplitMessages("RANDOM FACT !: "..FactDB[math.random(1, #FactDB)], sender)
    else
        SendSplitMessages("RANDOM FACT OF THE DAY !: "..FactOfTheDay)  --  is the message printed before your fact
    end
end

function Facts_GetCommand(msg)  -- seperate the command and the following string
    if msg then
 		local a,b,c=strfind(msg, "(%S+)"); --contiguous string of non-space characters
 		if a then
 			return c, strsub(msg, b+2);
 		else	
 			return "";
 		end
 	end
end


function Facts_OnEvent(msg, sender)  -- has someone whispered me the key command?
    if msg == key  then
        Facts_ShowFact(sender)
    end
end
  Reply With Quote