• Making a chat command?
    4 replies, posted
Hey guys. Well to skip to the point, I need to know how to make chat commands. Such as the AddChatCommand feature in DarkRP. I looked at the DarkRP code but couldn't figure out how it's done. I also searched but found nothing. So can someone tell me how to make a feature very similar to AddChatCommand(Where at the end of the function that i want to have a chat command for, I just add AddChatCommand( "What To Type", What the function is called )) I'd really appreciate it. Thanks.
[lua]local _ChatCommands = { }; local _ChatPrefix = "!"; --The character before the command, eg !ban function AddChatCommand( txt, func ) _ChatCommands[ _ChatPrefix .. txt ] = func; end hook.Add( "PlayerSay", "ChatCommands", function( ply, text, toall ) local tab = string.Explode( " ", text ); local func = _ChatCommands[ tab[1] ]; if ( func ) then local c = tab[1]; table.remove( tab, 1 ) func( ply, c, unpack( tab ) ); return ""; end end ); //EXAMPLE USAGE AddChatCommand( "hello", function( ply, command, args ) p:ChatPrint( "hello" ); end );[/lua]
This is an old script I wrote. I've no idea if it works. It may not work due to being old. Don't care if you give credit or not, it's pretty routine coding.. [lua] chatcommand = {}; chatcommand.Commands = {}; chatcommand.Prefix = "!"; local function Phrase(C) if C.Admin then if C.SuperAdmin then return "a super admin"; end return "an admin"; end return ""; end /* Usage: ChatName - Name to use in chat Func - Callback Desc - Description Usage - Usage guide ArgCount - Minimum argument count Admin - Is it admin only SuperAdmin - Is it super admin only */ function chatcommand.Add(ChatName,Func,Desc,Usage,ArgCount,Admin,SuperAdmin) if (type(ChatName) == "string" && #ChatName > 0) && type(Func) == "function" then local C = {}; C.ChatName = ChatName; C.Callback = Func; C.Desc = Desc or ""; C.Usage = Usage or ""; C.ArgCount = ArgCount or 0; C.Admin = Admin or false; C.SuperAdmin = SuperAdmin or false; if C.SuperAdmin then C.Admin = false; end table.insert(chatcommand.Commands,C); end end function chatcommand.Hook(Player,Text) local T = string.Explode(" ",Text); local CName = string.lower(T[1]); table.remove(T,1); if string.Left(CName,1) == chatcommand.Prefix then CName = string.Right(CName,#CName - 1); for _,C in pairs(chatcommand.Commands) do if C.ChatName == CName then if (C.Admin && Player:IsAdmin()) || (C.SuperAdmin && Player:IsSuperAdmin()) || (!C.Admin && !C.SuperAdmin) then if #T >= AC then C.Callback(Player,T); return ""; else Player:ChatPrint("Usage: "..chatcommand.Prefix..C.ChatName.." "..C.Usage); return ""; end else Player:ChatPrint("You need to be "..Phrase(C).." to use this command!"); return ""; end end end end end hook.Add("PlayerSay","chatcommand.Hook",chatcommand.Hook); //some default commands local function DescCommand(Player,Args) local CName = string.lower(Args[1]); for _,C in pairs(chatcommand.Commands) do if C.ChatName == CName then if C.Desc != "" then Player:ChatPrint(C.Desc); if C.Usage != "" then Player:ChatPrint("Usage: "..chatcommand.Prefix..C.ChatName.." "..C.Usage); end else Player:ChatPrint("No description available!"); end return; end end Player:ChatPrint("Command \""..CName.."\" not found!"); end chatcommand.Add("desc",DescCommand,"Prints out a description of a command","<command name>",1); local function ListCommands(Player) local T = "\n"; for _,C in pairs(chatcommand.Commands) do T = T..chatcommand.Prefix..C.ChatName.." "..C.Usage.."\n"; T = T..C.Desc.."\n"; T = T.."Minimum arguments: "..C.ArgCount.."\n"; T = T.."You need to be "..Phrase(C).."to use this command"; T = T.."\n"; end Player:PrintMessage(HUD_PRINTCONSOLE,T); Player:ChatPrint("A list of commands has been printed to your console!"); end chatcommand.Add("list",ListCommands,"Prints out a list of available commands"); [/lua]
Thanks a ton guys.
Great!
Sorry, you need to Log In to post a reply to this thread.