Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MrClever version 1.3
- -- store the version here for simplicity of updating
- local ver = '1.3'
- -- Loads the configuation file into the table "config"
- local config = cfg.load( "MrClever" ) or {}
- -- required config tables
- config.names = config.names or {}
- config.ops = config.ops or {}
- config.blacklist = config.blacklist or {}
- config.opPrograms = config.opPrograms or {}
- -- programs that need speical permission to run (console or op)
- local blacklist = {
- startup = true,
- shell = true,
- edit = true,
- lua = true,
- shutdown = true,
- exit = true
- }
- local opPrograms = {
- delete = true,
- op = true,
- deop = true,
- say = true,
- tell = true
- }
- -- Program help
- local help = {
- program = "<name> ...: runs a program",
- help = "help [command]: displays usage for commands",
- assign = "assign [name]: assigns a name to MrClever",
- unassign = "unassign [name]: unassigns a name from MrClever",
- say = "say <msg>: Says something as MrClever",
- tell = "tell <name> <msg>: Sends a message as MrClever",
- op = "op <name>: Gives operator status",
- deop = "deop <name>: removes operator status"
- }
- -- Load functions
- local bot = cleverbot.Cleverbot.new()
- local chatbox = peripheral.find( config.moar and "chatbox" or "chatBox" )
- local say, tell
- if config.moar then
- chatbox.setLabel( "MrClever" )
- function say( text )
- return chatbox.say( text )
- end
- function tell( name, text )
- return chatbox.tell( name, text )
- end
- else
- function say( text )
- return chatbox.say( text, 60000000, true, 'MrClever' )
- end
- function tell( name, text )
- return chatbox.tell( name, text, 60000000, true, 'MrClever' )
- end
- end
- -- functon to turn a space separate command into arguments
- local find = string.find
- function processArgs( input )
- local quote = {}
- local args = {}
- for match in string.gmatch( input, '%S+' ) do
- if quote[1] then
- table.insert( quote, match )
- if find( match, '"$' ) then
- local rep = table.concat( quote, " " ):gsub( '^"(.+)"$', "%1" )
- table.insert( args, rep )
- quote = {}
- end
- elseif find( match, '^"' ) then
- if find( match, '"$' ) then
- local rep = match:gsub( '^"(.+)"$', "%1" )
- table.insert( args, rep )
- else
- table.insert( quote, match )
- end
- else
- table.insert( args, match )
- end
- end
- return args
- end
- -- Sends messages to the user
- local consolePrint = print
- local name = ''
- local console
- print = function( text, ... )
- if not console then
- tell( name, text )
- sleep( 0.6 )
- end
- return consolePrint( text, ... )
- end
- -- Runs commands
- function runProgram( input, name )
- local args = processArgs( input )
- if name == "console" then
- console = true
- end
- if args[1] == "exit" and console then
- return true
- elseif not ( console or config.ops[name] ) and ( opPrograms[args[1]] or config.opPrograms[args[1]] ) then
- print( "This program is requires an operator to run" )
- elseif not console and ( blacklist[args[1]] or config.blacklist[args[1]] ) then
- print( "This program is blacklisted" )
- elseif args[1] == "tell" then
- if args[2] and args[3] then
- print( "Told " .. args[2] .. ' "' .. args[3] .. '"' )
- tell( args[2], args[3] )
- else
- print "Usage: tell <user> <message>"
- end
- elseif args[1] == "say" then
- if args[2] then
- print( 'Said "' .. args[2] .. '"' )
- say( args[2] )
- else
- print "Usage: say <message>"
- end
- elseif args[1] == "help" then
- print( "Displaying help" )
- if args[2] then
- print( help[args[2]] )
- else
- for _, v in pairs( help ) do
- print( v )
- end
- end
- elseif args[1] == "op" then
- if args[2] then
- config.names[args[2]] = true
- print( "Oped " .. n )
- cfg.save( 'MrClever', config )
- else
- print "Usage: op <name>"
- end
- elseif args[1] == "deop" then
- if args[2] then
- if args[2] == name then
- print( "Cannot deop yourself" )
- else
- config.names[args[2]] = nil
- print( "Deoped " .. n )
- cfg.save( 'MrClever', config )
- end
- else
- print( "Usage: deop <name>" )
- end
- elseif args[1] == "assign" then
- local n = args[2] or name
- config.names[n] = true
- print( "Assigned " .. n .. " to MrClever" )
- cfg.save( 'MrClever', config )
- elseif args[1] == "unassign" then
- local n = args[2] or name
- config.names[n] = nil
- print( "Unassigned " .. n .. " from MrClever" )
- cfg.save( 'MrClever', config )
- elseif not console and extensions and extensions.program( args[1] ) then
- print( "Cannot run files" )
- else
- consolePrint( 'Running "' .. args[1] .. '"' )
- local success = shell.run( unpack( args ) )
- if not success and not console then
- tell( name, 'No such program' )
- end
- end
- console = nil
- end
- consolePrint( "Welcome to MrClever " .. ver .. ", an AI based off of CleverBot and Lua" )
- -- Main loop
- local history = {}
- while true do
- -- Get user imput
- local data = { os.pullEvent() }
- -- dump the side data when using a MoarPeripherals chatbox, we don't need that
- if config.moar then
- table.remove( data, 2 )
- end
- if data[1] == "key" then
- write( "MrClever] " )
- local input = read( nil, history )
- if input ~= '' then
- table.insert( history, input )
- local done = runProgram( input, "console" )
- if done then
- break
- end
- end
- elseif data[1] == ( config.moar and "chatbox_command" or "command" ) then
- local args = data[3]
- if not config.moar then
- args = table.concat( args, ' ' ):gsub( '^\\ *', '' )
- end
- runProgram( args, data[2] )
- elseif data[1] == ( config.moar and "chat_message" or "chat" ) then
- name = data[2]
- local msg = data[3]
- if config.names[name] then
- consolePrint "MrClever is thinking..."
- msg = bot:send( msg )
- say( msg )
- end
- sleep( 0.1 )
- end
- end
- print = consolePrint
Add Comment
Please, Sign In to add comment