Several problems with your code:
Code:
local removestring = "-"..GetRealmName()
This won't work because "-" is a "magic character" in Lua. If you want to match it literally, you need to escape it, as "%-".
Code:
return string.gsub(msg, removestring, "")
This fails because when you change the message in a chat filter (or change any of the arguments) you must return
all of the arguments, in the original order, even the ones you didn't change.
Also, the player's name is not in the [i]message[i] argument. It's in the
sender argument -- but if you modify it in in a chat filter, then the resulting link the chat system produces will be broken. This is one of the cases where you will still need to use the old-fashioned "hook the AddMessage method" tactic, instead of the chat filtering system.
Code:
-- Define this one time, not over and over every time a message comes in:
local SAMEREALM = "%-" .. GetRealmName()
-- Use a table to store the chat frames' original AddMessage functions:
local originals = {}
-- Define your replacement AddMessage function:
local function AddMessage(frame, message, ...)
-- Make sure the message is a string before trying to do anything with it.
-- Not really sure why it's not ALWAYS a string, but it's not...
if type(message) == "string" then
-- Find the formatted player link:
local link, data, name = strmatch(message, "(|Hplayer:(.-)|h%[(.-)%]|h)")
if link then
-- Found it!
-- Strip the server name from the display name only:
name = gsub(name, SAMREALM, "")
-- Make a new link:
local newlink = "|Hplayer:" .. data .. "|h[" .. name .. "]|h"
-- Replace the original link in the message with the new one:
message = gsub(message, link, newlink, 1)
end
end
-- Pass everything back to the frame's original AddMessage method:
return frame[AddMessage](frame, message, ...)
end
-- Replace all the chat frames' AddMessage functions:
for i = 1, NUM_CHAT_FRAMES do
local frame = _G["ChatFrame"..i]
-- Store the frame's original function:
originals[frame] = frame.AddMessage
-- And replace it with yours:
frame.AddMessage = AddMessage
end
-- Set up a hook to catch temporary chat windows too, such as
-- those created when you send a whisper conversation to a new window
-- or have whispers set to appear in new windows.
local orig = FCF_OpenTemporaryWindow
FCF_OpenTemporaryWindow = function(...)
local frame = orig(...)
originals[frame] = frame.AddMessage
frame.AddMessage = AddMessage
return frame
end
If you wanted to remove
all realm names, just change:
Code:
name = gsub(name, SAMREALM, "")
to:
Code:
name = gsub(name, "%-[^|]+", "")