WoW:USERAPI RunSlashCmd: Difference between revisions
Jump to navigation
Jump to search
(Apparently the frame does need to be named.) |
m (Move page script moved page USERAPI RunSlashCmd to USERAPI RunSlashCmd without leaving a redirect) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{userfunc}} | |||
The following snippet provides a global function RunSlashCmd that can be used to execute an arbitrary slash command (i.e. "/myaddon hi"). | |||
local _G = _G | local _G = _G | ||
function RunSlashCmd(cmd) | |||
local slash, rest = cmd:match("^(%S+)%s*(.-)$") | local slash, rest = cmd:match("^(%S+)%s*(.-)$") | ||
for name in pairs(SlashCmdList) do | for name, func in pairs(SlashCmdList) do | ||
local i = 1 | local i, slashCmd = 1 | ||
repeat | repeat | ||
slashCmd = _G["SLASH_"..name..i] | slashCmd, i = _G["SLASH_"..name..i], i + 1 | ||
if slashCmd == slash then | if slashCmd == slash then | ||
return true, func(rest) | |||
end | end | ||
until not slashCmd | until not slashCmd | ||
end | end | ||
end | -- Okay, so it's not a slash command. It may also be an emote. | ||
local i = 1 | |||
while _G["EMOTE" .. i .. "_TOKEN"] do | |||
local j, cn = 2, _G["EMOTE" .. i .. "_CMD1"] | |||
while cn do | |||
if cn == slash then | |||
return true, DoEmote(_G["EMOTE" .. i .. "_TOKEN"], rest); | |||
end | |||
j, cn = j+1, _G["EMOTE" .. i .. "_CMD" .. j] | |||
end | |||
i = i + 1 | |||
end | |||
end | |||
=== Usage === | === Usage === | ||
Line 24: | Line 32: | ||
RunSlashCmd("/addon do this") | RunSlashCmd("/addon do this") | ||
== Alternative == | == Alternative == | ||
Another method to accomplish the same effect is to insert the command into an editbox and send the command. | Another method to accomplish the same effect is to insert the command into an editbox and send the command. Note that it's usually better to do this with your own EditBox to avoid taint or conflicts with user input into the default editbox. | ||
-- Create our editbox (may not need a name, untested) | -- Create our editbox (may not need a name, untested) | ||
Line 36: | Line 42: | ||
ChatEdit_SendText(editbox) | ChatEdit_SendText(editbox) | ||
However as of 3.3.5 you must set the chatFrame variable on your editbox before the OnLoad function runs. | However as of 3.3.5 you must set the chatFrame variable on your editbox before the OnLoad function runs. So if you want to use the above code you need to create the frame in XML or create a template and then use that template to create the frame. | ||
<EditBox name="MyAddOnEditBoxTemplate" inherits="ChatFrameEditBoxTemplate" virtual="true"> | <EditBox name="MyAddOnEditBoxTemplate" inherits="ChatFrameEditBoxTemplate" virtual="true"> | ||
Line 53: | Line 59: | ||
editbox:SetText("/command here") | editbox:SetText("/command here") | ||
ChatEdit_SendText(editbox) | ChatEdit_SendText(editbox) | ||
[[Category:User defined functions]] | [[Category:User defined functions]] |
Latest revision as of 04:49, 15 August 2023
This page documents a <i>user-defined function</i> that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.
The following snippet provides a global function RunSlashCmd that can be used to execute an arbitrary slash command (i.e. "/myaddon hi").
local _G = _G function RunSlashCmd(cmd) local slash, rest = cmd:match("^(%S+)%s*(.-)$") for name, func in pairs(SlashCmdList) do local i, slashCmd = 1 repeat slashCmd, i = _G["SLASH_"..name..i], i + 1 if slashCmd == slash then return true, func(rest) end until not slashCmd end -- Okay, so it's not a slash command. It may also be an emote. local i = 1 while _G["EMOTE" .. i .. "_TOKEN"] do local j, cn = 2, _G["EMOTE" .. i .. "_CMD1"] while cn do if cn == slash then return true, DoEmote(_G["EMOTE" .. i .. "_TOKEN"], rest); end j, cn = j+1, _G["EMOTE" .. i .. "_CMD" .. j] end i = i + 1 end end
Usage[edit]
RunSlashCmd("/help") RunSlashCmd("/addon do this")
Alternative[edit]
Another method to accomplish the same effect is to insert the command into an editbox and send the command. Note that it's usually better to do this with your own EditBox to avoid taint or conflicts with user input into the default editbox.
-- Create our editbox (may not need a name, untested) local editbox = CreateFrame("EditBox", "myEditBox", UIParent, "ChatFrameEditBoxTemplate") editbox:SetText("/command here") ChatEdit_SendText(editbox)
However as of 3.3.5 you must set the chatFrame variable on your editbox before the OnLoad function runs. So if you want to use the above code you need to create the frame in XML or create a template and then use that template to create the frame.
<EditBox name="MyAddOnEditBoxTemplate" inherits="ChatFrameEditBoxTemplate" virtual="true"> <Scripts> <OnLoad> self.chatFrame = self:GetParent(); ChatEdit_OnLoad(self); </OnLoad> </Scripts> </EditBox>
And then use:
-- Create our editbox local editbox = CreateFrame("EditBox", "MyAddOnEditBox", UIParent, "MyAddOnEditBoxTemplate") editbox:SetText("/command here") ChatEdit_SendText(editbox)