View Single Post
02-06-14, 04:03 AM   #4
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
I thought this would be a lot simpler to implement, but then I was reminded about cross-realm and all the issues that brings. I wrote something up that might work:

lua Code:
  1. -- Add SHARPARAM_GROUP_WHISPER to SavedVariables to persist the settings
  2. SHARPARAM_GROUP_WHISPER = SHARPARAM_GROUP_WHISPER or {}
  3.  
  4. local opt = SHARPARAM_GROUP_WHISPER
  5.  
  6. opt.enabled = true
  7. op*****ssage = op*****ssage or "Welcome to the group!"
  8.  
  9. local members = {}
  10.  
  11. local PARTY_UNITS = {
  12.     "party1",
  13.     "party2",
  14.     "party3",
  15.     "party4",
  16.     "party5"
  17. }
  18.  
  19. -- Helper function to get a list of group members
  20. local function getGroupMembers()
  21.     local result = {}
  22.     if IsInRaid() then
  23.         for i = 1, GetNumGroupMembers() do
  24.             local name = GetRaidRosterInfo(i)
  25.             if not name:match("%w+-%w+") then
  26.                 -- Try to get realm from UnitName func
  27.                 local _, realm = UnitName(name)
  28.                 -- Fall back to using player realm
  29.                 if not realm then realm = GetRealmName() end
  30.                 name = ("%s-%s"):format(name, realm)
  31.             end
  32.             result[#result + 1] = name
  33.         end
  34.     else
  35.         for _, unit in pairs(PARTY_UNITS) do
  36.             local name, realm = UnitName(unit)
  37.             if not realm then realm = GetRealmName() end
  38.             result[#result + 1] = ("%s-%s"):format(name, realm)
  39.         end
  40.     end
  41.     return result
  42. end
  43.  
  44. -- Loop through members and message anyone new to the group
  45. local function updateMembers()
  46.     if not opt.enabled or not IsInGroup() or not UnitIsGroupLeader("player") then
  47.         wipe(members)
  48.         return
  49.     end
  50.    
  51.     for _, v in pairs(getGroupMembers()) do
  52.         -- New member? Great! Let's message them
  53.         if not members[name] then
  54.             sendMessage(name)
  55.             -- Add them to list so they don't get messaged again
  56.             members[name] = true
  57.         end
  58.     end
  59. end
  60.  
  61. -- Gets the full player name string (name-realm) of the current player
  62. local function getFullPlayerName()
  63.     local name, realm = UnitName("player")
  64.     if not realm then realm = GetRealmName() end
  65.     return ("%s-%s"):format(name, realm)
  66. end
  67.  
  68. local function sendMessage(name, realm) -- realm param currently unused
  69.     realm = realm or GetRealmName()
  70.     if not name:match("%w+-%w+") then
  71.         name = ("%s-%s"):format(name:match("%w+"), realm)
  72.     end
  73.     if name == getFullPlayerName() then return end
  74.     SendChatMessage(op*****ssage, "WHISPER", nil, name)
  75. end
  76.  
  77. local frame = CreateFrame("Frame")
  78.  
  79. frame:SetScript("OnEvent", function(self, event, ...)
  80.     updateMembers()
  81. end)
  82.  
  83. frame:RegisterEvent("GROUP_JOINED")
  84. frame:RegisterEvent("GROUP_ROSTER_UPDATE")
  85. frame:RegisterEvent("INSTANCE_GROUP_SIZE_CHANGED")
  86. frame:RegisterEvent("PARTY_MEMBERS_CHANGED")
  87. frame:RegisterEvent("RAID_ROSTER_UPDATE")
  88.  
  89. SLASH_SHARPARAMGROUPWHISPER1 = "/sharpgw"
  90.  
  91. SlashCmdList["SHARPARAMGROUPWHISPER"] = function(e, msg)
  92.     local cmd, arg = msg:match("(%w)%w*(.*)")
  93.     if cmd == "t" then -- toggle
  94.         opt.enabled = not opt.enabled
  95.         print("Group whisper is now " .. (opt.enabled and "enabled" or "disabled"))
  96.     elseif cmd == "m" then -- set message
  97.         -- dirty workaround to skip the first space
  98.         op*****ssage = arg:gsub("^ ", "")
  99.         print("Update group whisper message!")
  100.     end
  101. end

PLEASE NOTE: I am not sure if this code will work with player names using unicode characters.

Last edited by Sharparam : 02-06-14 at 04:56 PM. Reason: Updated code with savedvars and slash command
  Reply With Quote