Thread Tools Display Modes
08-05-17, 07:20 AM   #1
Eragon_Edward
A Murloc Raider
Join Date: Aug 2017
Posts: 5
Create Slash Commands

Hi!, im new in Addon's development, and I'm having problems with the next Slash commands that I want to create, my idea is simple I just want to shortcuts, some personalize phrases , this is the code:
And btw, i don't know exactly what means the "msg" or "(txt)" does with "function " , and how does it affect the script

SLASH_PHRASE = "/ph"
SLASH_SPHRASE = "/sph"

SlashCmdList ["PHRASE"] = function (msg)
SendChatMessage ("Go to RBG!", GUILD")

SlashCmdList["SPHRASE"] = function (msg)
SendChatMessage("Do you want come with us to RBG?", "SAY")
end

Ok, the First /ph, works for the guild, but "SAY" is not working, and I have no clue.

Q1: if I write "function ()" what it does?
Q2: Could I use {} to just write 1 code line "SlashCmdList?

-- sry for my English --
  Reply With Quote
08-05-17, 08:20 AM   #2
Mortimerlol
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Jul 2017
Posts: 71
Lua Code:
  1. --Try it.
  2. SLASH_PHRASE = "/ph";
  3. SLASH_SPHRASE = "/sph";
  4.  
  5. local function PHRASEfunc(msg)
  6.     SendChatMessage("Go to RBG!", "GUILD")
  7. end
  8.  
  9. local function SPHRASEfunc(msg)
  10.     SendChatMessage("Do you want come with us to RBG?", "SAY")
  11. end
  12.  
  13. SlashCmdList["PHRASE"] = PHRASEfunc;
  14. SlashCmdList["SPHRASE"] = SPHRASEfunc;
  Reply With Quote
08-05-17, 08:43 AM   #3
Eragon_Edward
A Murloc Raider
Join Date: Aug 2017
Posts: 5
(Thx for answering )

Now both stoped working :S "/ph" and "/sph", but I realized something (while waiting for an answer)
I need to write first "/ph" so "/sph" could work

if I use "/sph" before "/ph" , it doesn't work :S, any idea?
  Reply With Quote
08-05-17, 10:04 AM   #4
Eragon_Edward
A Murloc Raider
Join Date: Aug 2017
Posts: 5
Edit

I could make it work , adding the 1 , at the end of each Slash_

SLASH_PHRASE1
So It works! now
now question 1,
what is the difference between

SlashCmdList = function(msg)
and
local function = <name>(msg)
?
and what exactly does (msg) or (txt) in that line?

question 2, is there function or array that I could use to get in one place all "local function " and "SendChatMessage", like a table or array"?
  Reply With Quote
08-05-17, 11:10 AM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Code:
SLASH_PHRASE1 = "/ph";
SLASH_PHRASE2 = "/sph";
SlashCmdList["PHRASE"] = function(msg)
...
end
if you type "/ph something" or "/sph something", "something" will be passed as a string to the msg/txt parameter of your slash comand function.

local anything is local to that module/code chunk and can't be "seen" outside of it. I'm not sure what you mean by SendChatMessage but if you mrean calls to the SendChatMessage function in code then no.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-05-17 at 11:15 AM.
  Reply With Quote
08-05-17, 11:16 AM   #6
Eragon_Edward
A Murloc Raider
Join Date: Aug 2017
Posts: 5
Oh thx!,
now I would like to make an index, like Blizzard did with "/help" command but I having issues how to make

print_array() into variable. the code is kind of

tmp = {"alpha", "beta", "gamma"}
function print_array(tbl)
for i = 1, #tbl do
print(i, tbl[i])
end end
print_array(tmp)

and that displays
1, alpha
2, beta
3, gamma


I would like to show my /commands
After I write
/hcm -- Help command
could display the next kind of table.
1, /ph , "you say , hello guild mates!"
2, /sph " an other phrase "


but until now I have no clue how to make it just the so simple ones, with foo= 14, print (foo) :S
  Reply With Quote
08-05-17, 11:59 AM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
I'm not sure if that was an actual question but if so, something like:
Lua Code:
  1. local msgList = {
  2.     { "GUILD", "Message 1" },
  3.     { "YELL", "Message 2" },
  4.     { "PARTY", "Message 3" },
  5. }
  6. local function PrintList()
  7.     print("Help commands = /ph or /sph\nOption = Send - Message:")
  8.     for i=1, #msgList do
  9.         print(i, "=", msgList[i][1], "-", msgList[i][2])
  10.     end
  11. end
  12.  
  13. SLASH_PHRASE1 = "/ph";
  14. SLASH_PHRASE2 = "/sph";
  15. SlashCmdList["PHRASE"] = function(msg)
  16.     if msg == "" then
  17.         PrintList()
  18.     else
  19.         local cmd = tonumber(msg)
  20.         if msgList[cmd] then
  21.             SendChatMessage(msgList[cmd][2], msgList[cmd][1])
  22.         else
  23.             print("Not a valid option")
  24.         end
  25.     end
  26. end
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
08-05-17, 12:35 PM   #8
Eragon_Edward
A Murloc Raider
Join Date: Aug 2017
Posts: 5
Originally Posted by Fizzlemizz View Post
I'm not sure if that was an actual question but if so, something like:
Lua Code:
  1. local msgList = {
  2.     { "GUILD", "Message 1" },
  3.     { "YELL", "Message 2" },
  4.     { "PARTY", "Message 3" },
  5. }
  6. local function PrintList()
  7.     print("Help commands = /ph or /sph\nOption = Send - Message:")
  8.     for i=1, #msgList do
  9.         print(i, "=", msgList[i][1], "-", msgList[i][2])
  10.     end
  11. end
  12.  
  13. SLASH_PHRASE1 = "/ph";
  14. SLASH_PHRASE2 = "/sph";
  15. SlashCmdList["PHRASE"] = function(msg)
  16.     if msg == "" then
  17.         PrintList()
  18. ----------------------------------------------------------------------------------------------------
  19.     else
  20.         local cmd = tonumber(msg)
  21.         if msgList[cmd] then
  22.             SendChatMessage(msgList[cmd][2], msgList[cmd][1])
  23.         else
  24.             print("Not a valid option")
  25.         end
  26.     end
  27. end
  28. ----------------------------------------------------------------------------------------------------
Ok now I got it! (the first part) , thank you , now i could not understand all the rest of the code (the one between the lines) exactly how it works down there? ,if you are so kind to explain it for a really dummy :P
  Reply With Quote
08-05-17, 12:43 PM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
The part between the lines, if you type /ph (or /sph) without anything after it, it will print the list of options.
/ph
Help commands = /ph or /sph
Option = Send - Message:
1 = GUILD - Message 1
2 = YELL - Message 2
3 = PARTY - Message 3
if you type say /ph 1, it will use the options in the first msgList entry to create a SendChatMessage():
/ph 1
would be like doing
/g Message 1
/ph 2
would be like doing
/y Message 2
The entries in the msgList are in two parts
{ "chattype", "message" }
as per SendChatMessage

If you're only sending one chattype then you don't need that part.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 08-05-17 at 12:50 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Create Slash Commands

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