Quantcast
Slash command functions help - WoWInterface
Thread Tools Display Modes
06-18-21, 03:34 PM   #1
henry8362
A Defias Bandit
Join Date: Jun 2021
Posts: 3
Slash command functions help

Hi,

I have written an addon for a simple text based game, which has several functions. I would like these functions to be callable by doing something like this:

/addonname nameoffunction argument

- The / command documentation i've seen doesn't seem to allude to how this can be done, so i'm a bit stuck (trying to google) I know it's possible as DBM does something similar, and i'm trying to search through that to find out how it's done but it's a huge addon, and i'm quite new to Lua.

Is there any guide on how to do this? Or can somebody help me figure this one out?

Thanks
  Reply With Quote
06-18-21, 03:48 PM   #2
henry8362
A Defias Bandit
Join Date: Jun 2021
Posts: 3
just to add, I have been doing some more digging and thing I need to use the ace console library to achieve this?
  Reply With Quote
06-18-21, 04:17 PM   #3
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 578
Lua Code:
  1. SLASH_YOURUNIQUENAMEHERE1="/command" -- SLASH_ and 1 are required
  2. SLASH_YOURUNIQUENAMEHERE2="/comm" -- increase the number for aliases
  3. SlashCmdList["YOURUNIQUENAMEHERE"]=function(arg) -- the name you used without SLASH_ or a number
  4.     -- arg is a string with everything typed after the command, including spaces
  5. end
  Reply With Quote
06-18-21, 07:07 PM   #4
briskman3000
A Warpwood Thunder Caller
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 96
If you want to be able to use a sub-command or a command with an argument, you can use a string grab to separate the stuff.

Here is an example of how I do it, without using any libraries.

This is a straight snippet from one of my working addons, so some of the variables wont make any sense in just this snippet, but you should be able to get an idea of how it works.

Lua Code:
  1. SLASH_HONORTRACK1, SLASH_HONORTRACK2 = '/honortrack', '/ht';
  2. --Function to handle the context of the slash command arguements
  3. function SlashCmdList.HONORTRACK(msg, editBox)
  4.     --Capture the command and then the rest of whatever the user input then do things
  5.     local command, rest = msg:match("^(%S*)%s*(.-)$");
  6.     --Show the frame command check
  7.     if string.lower(command) == 'show' then
  8.         --Show it
  9.         Addon:Show();
  10.         --Set the visible variable to save between sessions
  11.         htvisible = true
  12.         --Let the user know it worked and how to hide it.
  13.         print("Honor Track: Showing tracker. You can hide the tracker with /honortrack hide")
  14.     --Hide the frame command check
  15.     elseif string.lower(command) == 'hide' then
  16.         --Hide it
  17.         Addon:Hide();
  18.         --set the variable
  19.         htvisible = false
  20.         --Let the user know it worked and how to show it.
  21.         print("Honor Track: Hiding tracker. You can show the tracker again with /honortrack show")
  22.     --Set the goal command check. This makes sure the user input only numbers and errors if they didn't
  23.     elseif string.lower(command) == 'goal' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
  24.         --Grab the goal amount entered
  25.         hthonorgoal = string.match(rest, "%d*")
  26.         --Send the goal to the update function
  27.         UpdateGoal(self)
  28.         htgoalset = true
  29.         --Let the user know it worked and what we set the goal to.
  30.         print("Honor Track: Honor Goal set to " .. string.match(rest, "%d*"))
  31.     --Reset the goal
  32.     elseif string.lower(command) == 'goal' and string.lower(rest) == "reset" then
  33.         --Set the goal to 0
  34.         hthonorgoal = 0
  35.         --Send that 0 to the frame update function
  36.         UpdateGoal(self)
  37.         htgoalset = false
  38.         --Let the user know we reset the goal
  39.         print("Honor Track: Honor Goal reset")
  40.     --Reset the honor per hour tracker to zero
  41.     elseif string.lower(command) == 'hprreset' then
  42.         --set the honor per hour variable to 0
  43.         hthonorgained = 0
  44.         print("Honor Track: Your honor per hour statistic has been reset.")
  45.     --Test slash function
  46.     elseif string.lower(command) == 'test' and string.match(rest, "%d*") ~= nil and string.match(rest, "%a") == nil then
  47.         hthonorgained = string.match(rest, "%d*")
  48.         print("HonorTrack: Added honor to the functions for testing.")
  49.     --User entered something that is not a valid command
  50.     else
  51.         --Let the user know what the proper slash command syntax is
  52.         print("Honor Track: Available commands are show, hide, hprreset and goal")
  53.         print("Honor Track: To set goal, use /honortrack goal ####")
  54.         print("Honor Track: To reset goal, use /honortrack goal reset")
  55.         print("Honor Track: To reset your honor per hour statistic, use /honortrack hprreset")
  56.     end
  57. end
__________________
My Addons: Convert Ratings Honor Track

Last edited by briskman3000 : 06-18-21 at 07:09 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Slash command functions help

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