Navigation menu

WoW:USERAPI RunSlashCmd: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Apparently the frame does need to be named.)
No edit summary
Line 1: Line 1:
Thanks to cladhaire and Josh_Borke for all the effort in coming up with this code that allows you to pass a /command to wow from a lua script. The following code is cladhaire's final product
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
  local function RunSlashCmd(cmd)
  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
      local slashCmd
       repeat
       repeat
         slashCmd = _G["SLASH_"..name..i]
         slashCmd, i = _G["SLASH_"..name..i], i + 1
       
         if slashCmd == slash then
         if slashCmd == slash then
             -- Call the handler
             return true, func(rest)
            SlashCmdList[name](rest)
            return true
         end
         end
        i = i + 1
       until not slashCmd
       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
  end  
  end  
Line 24: Line 30:
  RunSlashCmd("/addon do this")
  RunSlashCmd("/addon do this")


Try
local t={}; t.GetText = function(self) return self.text end; local function RunSlash(slash) t.text = slash; ChatEdit_ParseText(t) end


== Alternative ==
== Alternative ==