Author Topic: Chat Commands  (Read 198 times)

0 Members and 1 Guest are viewing this topic.

Offline BlueNova

  • Jr. Member
  • **
  • Posts: 92
  • Karma: 10
  • The most powerful force in the universe.
    • My Site
Chat Commands
« on: March 12, 2018, 08:11:59 PM »
Hey, not sure where I should post this so I thought this board was the best idea. Basically I'm stuck on something and I could use some help from people who know more on this stuff than I do. Sorry if this post shouldn't be here though.

Anyways, I'm working on making an addon that uses custom made chat commands. I know that PlayerSay hooks exist yet I can't seem to get them working with parameters/arguments. Seems to elude me. I've found some code snippets that would define chat commands but they would screw with the base function which isn't what I want. Here I'll provide an example.

Code: Lua
  1. concommand.Add("rainbow", function(ply, _, args)
  2.  
  3.         if not ply:IsSuperAdmin() then
  4.                 ply:ChatPrint("No Access!")
  5.                 return
  6.         end
  7.        
  8.         local target = ply
  9.         target = GetPlayer(args[1])
  10.        
  11.         if not target:IsValid() then
  12.                 ply:ChatPrint("Invalid Target!")
  13.                 return
  14.         end
  15.        
  16.         timer.Create("RainbowGuy", 0.2, 0, function() rainbow(target) end)
  17.         target:ChatPrint("Rainbow Effect Enabled!")
  18. end)

Here is a simple script that just, in theory, makes a user's model color and physgun color rapidly change in a rainbow effect. What's provided above may not work on the first try because I changed it ever so slightly to allow it to make more sense here. The changes don't affect my dilemma though.

So, anyways, as you can see I have created a console command named "rainbow" and it has room for targeting players and all of that fun stuff. My question is, is there a way to do this in a chat command as well? Have both a chat and console command that take from the same function? Everything I've tried won't work because concommand.Add() needs to have that cmd parameter and that screws with the methods I've tried. I'd really rather have to not essentially duplicate the function to get the same effect so I'm hoping that someone here could help.

Any help is greatly appreciated.

If you need to see the function behind rainbow() then I can provide that, I just didn't see it necessary for my issue.
Penguin Servers Super Administrator
ByB Super Administrator / ByB Contributor

If you need any help then feel free to PM me or add me on Steam or join my discord. Links available below:

Discord
Steam

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 757
  • Karma: 47
Re: Chat Commands
« Reply #1 on: March 12, 2018, 08:28:50 PM »
What I would do is split the PlayerSay text into args:
Code: Lua
  1. hook.Add( "PlayerSay", "something", function( ply, text, teamChat )
  2.         local args = string.Split( text, " " ) -- Basically this will take everything they said and put it into a table, then you can get each arg index in each function whenever it's required.
  3.         local fn = args[ 1 ] -- This is the command (such as !rainbow)
  4.         local target
  5.         -- If you're using ULib, use:
  6.         target = ULib.getUser( args[ 1 ] )
  7.         -- If not, find the player yourself by comparing partial/full names
  8.         for k, v in pairs( player.GetAll() ) do
  9.                 -- Check v:Nick() against args[ 2 ] to try to find a match
  10.         end
  11.         -- Command logic
  12.         if ( fn == "!rainbow" ) then
  13.                 rainbow( target ) -- Or however you call it
  14.         -- elseif ( fn == "Something else" ) then
  15.                 -- do something else
  16.         end
  17. end )
  18.  

Something similar to that.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline BlueNova

  • Jr. Member
  • **
  • Posts: 92
  • Karma: 10
  • The most powerful force in the universe.
    • My Site
Re: Chat Commands
« Reply #2 on: March 12, 2018, 08:35:36 PM »
   local target
   -- If you're using ULib, use:
   target = ULib.getUser( args[ 1 ] )
   -- If not, find the player yourself by comparing partial/full names

Will try this out

And btw here is how I'm getting my targets incase you're curious

Code: Lua
  1. function GetPlayer(info)
  2.         for k, v in pairs(player.GetAll()) do
  3.                 if string.find(string.lower(v:Nick()), string.lower(tostring(info))) then
  4.                         return v
  5.                 end
  6.         end
  7. end



Having some trouble with this. To help explain I whipped up a quick little god mode command.

Here's the PlayerSay hook you gave me iViscosity

Code: Lua
  1. hook.Add("PlayerSay", "chatcommands", function(ply, text)
  2.         local args = string.Split(text, " ")
  3.  
  4.         local cmd = args[1]
  5.  
  6.         if cmd == "!immortal" then
  7.                 god(ply, args)
  8.                 return ""
  9.         end
  10. end)

And here's the little god mode command

Code: Lua
  1. function god(ply, args)
  2.  
  3.         if not ply:IsSuperAdmin() then
  4.                 ply:ChatPrint("No Access!")
  5.                 return
  6.         end
  7.        
  8.         if not args[1] or args[1] == "" then
  9.                 if ply:HasGodMode() then
  10.                         ply:GodDisable()
  11.                         echo(ply:Colors(), ply:Nick(), textcolor, " has revoked godmode from ", ply:Colors(), ply:Nick())
  12.                 else
  13.                         ply:GodEnable()
  14.                         echo(ply:Colors(), ply:Nick(), textcolor, " has granted godmode to ", ply:Colors(), ply:Nick())
  15.                 end
  16.                 return
  17.         end
  18.        
  19.         local target = ply
  20.         target = GetPlayer(args[1])
  21.        
  22.         if not target:IsValid() then
  23.                 ply:ChatPrint("Invalid Target!")
  24.                 return
  25.         end
  26.        
  27.         if target:HasGodMode() then
  28.                 target:GodDisable()
  29.                 echo(ply:Colors(), ply:Nick(), textcolor, " has revoked godmode from ", target:Colors(), target:Nick())
  30.         else
  31.                 target:GodEnable()
  32.                 echo(ply:Colors(), ply:Nick(), textcolor, " has granted godmode to ", target:Colors(), target:Nick())
  33.         end
  34.  
  35. end
  36.  
  37. concommand.Add("nas_god", function(ply, cmd, args)
  38.         god(ply, args)
  39. end)

I was still getting talk back because of the cmd parameter all together. So I removed it and just redefined it in concommand.Add(). The console command works fine yet I still cannot get the chat variations to work. I've tried on a multitude of commands, from private chats to commands with more than one parameter and it still doesn't work.
« Last Edit: March 12, 2018, 09:37:27 PM by BlueNova »
Penguin Servers Super Administrator
ByB Super Administrator / ByB Contributor

If you need any help then feel free to PM me or add me on Steam or join my discord. Links available below:

Discord
Steam

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 757
  • Karma: 47
Re: Chat Commands
« Reply #3 on: March 13, 2018, 05:00:51 AM »
Hm. Do you mean it just doesn't work or you're getting errors or something? As I'm looking at it, it looks like it's passing through the "!god" command in the player say, and I think that's why. I think you need to remove the function call:

Code: Lua
  1. hook.Add("PlayerSay", "chatcommands", function(ply, text)
  2.         local args = string.Split(text, " ")
  3.  
  4.         local cmd = args[1]
  5.  
  6.         if cmd == "!immortal" then
  7.                 table.remove( args, 1 ) -- remove the function call
  8.                 god(ply, args)
  9.                 return ""
  10.         end
  11. end)
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.

Offline BlueNova

  • Jr. Member
  • **
  • Posts: 92
  • Karma: 10
  • The most powerful force in the universe.
    • My Site
Re: Chat Commands
« Reply #4 on: March 13, 2018, 08:00:01 AM »
Hm. Do you mean it just doesn't work or you're getting errors or something? As I'm looking at it, it looks like it's passing through the "!god" command in the player say, and I think that's why. I think you need to remove the function call:

Huh, hadn't considered that. Will give it a shot when I get access to my computer later today.
Penguin Servers Super Administrator
ByB Super Administrator / ByB Contributor

If you need any help then feel free to PM me or add me on Steam or join my discord. Links available below:

Discord
Steam

Offline BlueNova

  • Jr. Member
  • **
  • Posts: 92
  • Karma: 10
  • The most powerful force in the universe.
    • My Site
Re: Chat Commands
« Reply #5 on: March 13, 2018, 12:33:28 PM »
Shameless bump, sorry for the double post btw

It worked iViscosity. Using table.remove fixed my issues and new I've got chat commands that mimic my console ones. Thanks so much. :)
Penguin Servers Super Administrator
ByB Super Administrator / ByB Contributor

If you need any help then feel free to PM me or add me on Steam or join my discord. Links available below:

Discord
Steam

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 757
  • Karma: 47
Re: Chat Commands
« Reply #6 on: March 13, 2018, 02:44:28 PM »
Shameless bump, sorry for the double post btw

It worked iViscosity. Using table.remove fixed my issues and new I've got chat commands that mimic my console ones. Thanks so much. :)
Also just an FYI you could always just pass through the whole args without removing and then starting at args[ 2 ] instead. Either way you prefer though.

Sent using Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.