You're going to need more than "a function" for that. You'll need a variable outside the function to keep a running total of your damage taken. Also, unless you want to sit there manually pushing that button to run that command over and over and over, you're going to need a frame to run a timer, so it will automatically alert you when the time is up. You could go even further and have it automatically detect when you entered Defensive Stance, so you never had to run the command at all.
Code:
-- You need a frame to run a timer and listen for events.
local frame = CreateFrame("Frame")
-- You need variables to keep track of stuff.
local totalDamageTaken = 0
local playerGUID
-- You can use an animation object to run a timer.
local timer = frame:CreateAnimationGroup()
timer.anim = timer:CreateAnimation()
timer.anim:SetDuration(30)
timer.anim:SetOrder(1)
-- Tell the timer what to do when the time runs out.
timer:SetScript("OnFinished", function(self, requested)
-- "requested" is true if you manually called :Stop() or :Finish() on the timer,
-- or nil if the timer finished on its own.
if not requested then
-- Check to see if you've taken at least 3k damage since the timer started.
if totalDamageTaken < 3000 then
-- Nope. Show the alert.
UIErrorsFrame:AddMessage("Get out of Defensive Stance!", 1, 0.8, 0, UIERRORS_HOLD_TIME)
end
end
end)
-- Tell the frame to listen for combat events.
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
-- Tell the frame to listen for your spell casts.
frame:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player")
-- Tell the frame what to do when a registered event fires.
frame:SetScript("OnEvent", function(self, event, ...)
-- Check which event fired.
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
-- Assign the arguments to named variables.
-- Use the name "_" for variables you don't care about, for tidiness.
local _, subevent, _, _, _, _, _, destGUID, _, _, _, _, _, _, amount = ...
-- Check if this combat event indicates damage being done.
if subevent == "SPELL_DAMAGE" or subevent == "SWING_DAMAGE" then
if not playerGUID then
-- Avoid unnecessary function calls by only defining this variable once per session.
playerGUID = UnitGUID("player")
end
-- Check if this damage was done to the player.
if destGUID == playerGUID then
-- Add the amount to the total.
totalDamageTaken = totalDamageTaken + amount
end
end
else
-- Since you're only listening for 2 events, if it wasn't CLEU it must be a spellcast,
-- so you can just use "else" and not waste time explicitly checking the event name.
local _, _, _, _, spellID = ...
-- Check if the spell was Defensive Stance.
if spellID == 71 then
-- Clear the total.
totalDamageTaken = 0
-- Start the timer.
timer:Play()
end
end)
There are a number of improvements that could be made there, but that should get you started. For example, you should probably only listen for CLEU events while in Defensive Stance. You may also want to listen for combat starting/stopping, and only run the timer in combat, since you won't be taking damage out of combat even when you're supposed to be in Defensive Stance. You should also stop the timer if you switch out of Defensive Stance on your own.