Thread Tools Display Modes
08-31-15, 01:16 AM   #1
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Clear chat slash command.

I'm trying to create a command function that simply clears the chat that you currently have selected, whether it be the combat log or the main chat or any other chat frame.

This is what I have, but the code is off.

Code:
SlashCmdList["CLEAR"] = function() SELECTED_CHAT_FRAME() end
	SLASH_CLEAR1     = "/clear"
  Reply With Quote
08-31-15, 02:10 AM   #2
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 247
Originally Posted by Lesteryoung View Post
I'm trying to create a command function that simply clears the chat that you currently have selected, whether it be the combat log or the main chat or any other chat frame.

This is what I have, but the code is off.

Code:
SlashCmdList["CLEAR"] = function() SELECTED_CHAT_FRAME() end
	SLASH_CLEAR1     = "/clear"
Try this.
Code:
SlashCmdList.CLEAR_CHAT = function()
	for i = 1, NUM_CHAT_WINDOWS do
		_G[format("ChatFrame%d", i)]:Clear()
	end
end
SLASH_CLEAR_CHAT1 = "/clear"
If you want to see some other ones look here.
https://github.com/Kkthnx/KkthnxUI/b...e/Commands.lua
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.

Last edited by Kkthnx : 08-31-15 at 02:12 AM.
  Reply With Quote
09-01-15, 02:23 AM   #3
Petrah
A Pyroguard Emberseer
 
Petrah's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 2,988
This is what I'm currently using. Someone shared it on a forum, I think it was the official forums, but I cannot recall who. I've been using it for quite a long time added in one of the files for ElvUI.

Code:
SlashCmdList["CLEAR"] = function()
    local s, i
    s = ""
    for i=1, 255, 1 do
        s = s..'\r'
    end
    SELECTED_CHAT_FRAME:AddMessage(s)
end
__________________
♪~ ( ) I My Sonos!
AddOn Authors: If your addon spams the chat box with "Addon v8.3.4.5.3 now loaded!", please add an option to disable it!
  Reply With Quote
09-01-15, 04:48 AM   #4
ObbleYeah
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Sep 2008
Posts: 210
seems kinda gross to add 255 blank messages rather than simply clearing them
  Reply With Quote
09-01-15, 05:54 AM   #5
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Originally Posted by Petrah View Post
This is what I'm currently using. Someone shared it on a forum, I think it was the official forums, but I cannot recall who. I've been using it for quite a long time added in one of the files for ElvUI.

Code:
SlashCmdList["CLEAR"] = function()
    local s, i
    s = ""
    for i=1, 255, 1 do
        s = s..'\r'
    end
    SELECTED_CHAT_FRAME:AddMessage(s)
end
How about just doing this instead?
Lua Code:
  1. SlashCmdList["CLEAR"] = function()
  2.     SELECTED_CHAT_FRAME:Clear()
  3. end
__________________

Last edited by MunkDev : 09-01-15 at 05:59 AM.
  Reply With Quote
09-01-15, 07:00 AM   #6
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
http://wow.curseforge.com/addons/cle...earing-method/ (couple years back since I submitted this but I expect it still works)
  Reply With Quote
09-01-15, 02:47 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Dridzt View Post
Code:
SlashCmdList["CLEAR"] = SELECTED_CHAT_FRAME.Clear
SLASH_CLEAR1 = "/clear"
SLASH_CLEAR2 = "/chatclear"
http://wow.curseforge.com/addons/cle...earing-method/
(couple years back since I submitted this but I expect it still works)
That ends up passing the command arguments string as self, which should raise an error.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
09-01-15, 03:35 PM   #8
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
I'm no longer playing, but have you tested it?
I'd been using it for quite a while with no issues, I don't think chatcode has changed significantly.

Last edited by Dridzt : 09-01-15 at 03:38 PM.
  Reply With Quote
09-01-15, 04:10 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
As expected:
Message: Interface\FrameXML\ChatFrame.lua:4454: Attempt to find 'this' in non-table object (used '.' instead of ':' ?)
Time: 09/01/15 15:13:23
Count: 1
Stack: [C]: ?
[C]: in function `?'
Interface\FrameXML\ChatFrame.lua:4454: in function `ChatEdit_ParseText'
Interface\FrameXML\ChatFrame.lua:4108: in function `ChatEdit_SendText'
Interface\FrameXML\ChatFrame.lua:4147: in function `ChatEdit_OnEnterPressed'
[string "*:OnEnterPressed"]:1: in function <[string "*:OnEnterPressed"]:1>

Locals:
Honestly, it's a wonder how this code ever worked. This isn't something they change with an API update.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 09-01-15 at 04:18 PM.
  Reply With Quote
09-02-15, 03:34 AM   #10
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Originally Posted by Dridzt View Post
I'm no longer playing, but have you tested it?
I'd been using it for quite a while with no issues, I don't think chatcode has changed significantly.
It's not really a matter of chatcode. It's a matter of how the function works. Calling a function as owner:function() will pass owner as the hidden argument self, meaning any code within that function that refers to self will change the calling widget.

As an example, when you create a frame or button, it will inherit a lot of global functions which can be used to modify the widget, but these functions expect the widget to pass itself as the first argument. This is what the colon operator does in lua.

What you attempt to do in that snippet is call the function Clear() that is attached to SELECTED_CHAT_FRAME, without giving the function the actual frame that you intend to clear.

Example of difference between dot and colon operator:
Lua Code:
  1. function SetID(self, value)
  2.     self.ID = value
  3. end
  4.  
  5. owner.SetID = SetID -- attach function to widget
  6.  
  7. owner:SetID(1) -- passing itself as the hidden argument self
  8.  
  9. owner.SetID(self, 1) -- calling the function without hidden argument
  10.  
  11. owner.SetID(1) -- error, function will try to add nil value to an integer
  12.  
  13. owner.SetID(someOtherFrame, 1) -- uses the function SetID on owner to change someOtherFrame
__________________

Last edited by MunkDev : 09-02-15 at 09:01 AM.
  Reply With Quote
09-02-15, 10:47 AM   #11
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
I have no reason to doubt my version is broken now, but it was absolutely working in 50200 (and for a while before that).
  Reply With Quote
09-02-15, 05:55 PM   #12
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.
  Reply With Quote
09-02-15, 07:05 PM   #13
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 247
Originally Posted by Lesteryoung View Post
Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.
Have you even tried the one I posted? It does everything you've requested even the combat log?

Here I'll post it again and quote myself.

Originally Posted by Kkthnx View Post
Try this.
Code:
SlashCmdList.CLEAR_CHAT = function()
	for i = 1, NUM_CHAT_WINDOWS do
		_G[format("ChatFrame%d", i)]:Clear()
	end
end
SLASH_CLEAR_CHAT1 = "/clear"
If you want to see some other ones look here.
https://github.com/Kkthnx/KkthnxUI/b...e/Commands.lua
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.
  Reply With Quote
09-02-15, 07:18 PM   #14
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Lesteryoung View Post
Been using MunkDev's version and it works fine. Is this the preferable way to clear chat? Obviously creating 255 blank lines seem egregious.

Only complaint is that it doesn't clear the combat log when it's selected. There's a code floating around on arenajunkies but it doesn't work either. They must have changed chat API within the last few years.
That's not MunkDev's code, that was Petrah's that he had quoted. And, no, creating 255 blank lines is not the best way. And the reason why it doesn't work in the combat log is because you can't add messages to it (to my knowledge, anyway).

Take another look at MunkDev's actual posted solution.


Originally Posted by Kkthnx View Post
Have you even tried the one I posted? It does everything you've requested even the combat log?

Here I'll post it again and quote myself.
The OP asked for a script to clear the currently selected chat window, not all at once.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
09-02-15, 07:32 PM   #15
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 247
Originally Posted by Seerah View Post
That's not MunkDev's code, that was Petrah's that he had quoted. And, no, creating 255 blank lines is not the best way. And the reason why it doesn't work in the combat log is because you can't add messages to it (to my knowledge, anyway).

Take another look at MunkDev's actual posted solution.



The OP asked for a script to clear the currently selected chat window, not all at once.
Sigh, again he needs to use the one I posted. Who mentioned all at once?
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.
  Reply With Quote
09-02-15, 07:33 PM   #16
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Seerah, I did use MunkDev's code, and it work's fine on chat but doesn't clear the combat log.
The one kkthnx posted clears all chats, not the one you have selected.
And the one Petrah posted creates 255 blank lines which everyone has said is stupid.
  Reply With Quote
09-02-15, 07:35 PM   #17
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 247
Originally Posted by Lesteryoung View Post
Seerah, I did use MunkDev's code, and it work's fine on chat but doesn't clear the combat log.
The one kkthnx posted clears all chats, not the one you have selected.
And the one Petrah posted creates 255 blank lines which everyone has said is stupid.
The one I posted will not clear the combat log unless you have that focused.
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.
  Reply With Quote
09-02-15, 07:37 PM   #18
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
It clears the combat for about a second, and then the data just repopulates instantly.

And if you have the combat log targeted and /clear, it clears all other windows as well.

So far MunkDev's code has worked the best, in that it only clears selected windows, but among them can't be the combat log for some reason.
  Reply With Quote
09-02-15, 07:45 PM   #19
Kkthnx
A Cobalt Mageweaver
 
Kkthnx's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2011
Posts: 247
Originally Posted by Lesteryoung View Post
It clears the combat for about a second, and then the data just repopulates instantly.

And if you have the combat log targeted and /clear, it clears all other windows as well.

So far MunkDev's code has worked the best, in that it only clears selected windows, but among them can't be the combat log for some reason.
Here you go

Code:
SLASH_CLEARCHAT1 = "/clear"
SLASH_CLEARCHAT2 = "/clearchat"

SlashCmdList.CLEARCHAT = function(cmd)
	cmd = cmd and strtrim(strlower(cmd))
	for i = 1, NUM_CHAT_WINDOWS do
		local f = _G["ChatFrame"..i]
		if f:IsVisible() or cmd == "all" then
			f:Clear()
		end
	end
end
I tested this window by window and this is what you are looking for 100%

Credits to Phanx / PhanxChat
https://github.com/Phanx/PhanxChat/b.../Core.lua#L273
__________________
Success isn't what you've done compared to others. Success is what you've done compared to what you were made to do.

Last edited by Kkthnx : 09-02-15 at 07:48 PM.
  Reply With Quote
09-02-15, 07:55 PM   #20
Lesteryoung
A Black Drake
Join Date: Aug 2015
Posts: 81
Thanks kkThnx, the one you posted works the same as Munk's code basically. It clears selected windows without clearing all windows.

For some reason, it clears the combat log fine, but if you tab between the combat log views (What happened to me?) thing it repopulates the data you previously cleared. If this is unavoidable then it's fine, mainly just wanted to clear chat anyway.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Clear chat slash command.

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off