Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- [Program] MoarPeripherals - Teams
- @version 1.0, 2015-08-03
- @author TheOddByte
- # Commands
- ##team help <-command>
- ##team list
- ##team create <name> <-password>
- ##team join <name> <-password>
- ##team say <text>
- ##team info
- ##team members
- ##team removepassword
- ##team setpassword <password>
- ##team kick <username> <-reason>
- ##team ban <username> <-reason>
- ##team unban <username>
- ##team disband confirm <-password>
- --]]
- local chatbox = peripheral.find( "chatbox_admin" ) or peripheral.find( "chatbox" )
- if not chatbox then
- error( "No chatbox attached" )
- else
- chatbox.setLabel( "Teams" )
- end
- --# Check if it's a regular or an admin chatbox
- if chatbox.setSayRange then
- chatbox.setSayRange( -1 )
- chatbox.setTellRange( -1 )
- chatbox.setReadRange( -1 )
- end
- local autorestart = true --# Should the program restart automatically?
- local time = 5*60 --# The time of between backup creations( in seconds )
- local timer = os.startTimer( time )
- local users, teams = {}, {}
- local list = {
- "help", "list", "create", "join", "info", "members", "say",
- "kick", "ban", "unban", "disband", "setpassword", "removepassword",
- }
- local function save( data, path )
- local f = assert( fs.open( path, "w" ), "could not open file for writing" )
- f.writeLine( textutils.serialize( data ) )
- f.close()
- end
- local function load( path )
- local f = assert( fs.open( path, "r" ), "could not open file for reading" )
- local data = textutils.unserialize( f.readAll() )
- f.close()
- return data
- end
- local function getLength( t )
- local n = 0
- for k, v in pairs( t ) do
- n = n + 1
- end
- return n
- end
- local function broadcast( username, team, message )
- chatbox.setLabel( username )
- for i = 1, #teams[team].users do
- chatbox.tell( teams[team].users[i], message )
- end
- chatbox.setLabel( "Teams" )
- end
- local commands = {
- ["help"] = {
- description = "Lists all available commands";
- action = function( username, ... )
- local command
- local tArgs = {...}
- if #tArgs > 0 then
- for i = 1, #list do
- if tArgs[1]:lower() == list[i] then
- command = list[i]
- break
- end
- end
- end
- chatbox.tell( username, "Optional usage: ##team help <command>" )
- chatbox.tell( username, "# Commands Available" )
- local text = ""
- for i = 1, #list do
- text = text .. list[i] .. ( i ~= #list and ", " or "" )
- end
- chatbox.tell( username, text )
- end;
- };
- ["create"] = {
- description = "Creates a new team";
- action = function( username, ... )
- local args = {...}
- if #args >= 1 then
- if not users[username] then
- local name, password = unpack( args )
- if not teams[name] then
- teams[name] = {
- leader = username;
- password = password;
- users = {username};
- blacklist = {};
- }
- users[username] = name;
- chatbox.tell( username, "Team successfully created" )
- if password then
- chatbox.tell( username, "Password set to: " .. password )
- end
- else
- chatbox.tell( username, "Team does already exist" )
- end
- else
- chatbox.tell( username, "You can't create a new team because you already belong to a team" )
- end
- else
- chatbox.tell( username, "##team create <name> <-password>" )
- end
- end;
- };
- ["join"] = {
- description = "Join a team";
- action = function( username, ... )
- local args = {...}
- if #args >= 1 then
- local name, password = unpack( args )
- if not users[username] then
- if teams[name] then
- if teams[name].blacklist[username] then
- local reason = type( teams[name].blacklist[username] ) == "string" and teams[name].blacklist[username] or nil
- chatbox.tell( username, "You've been banned from this team " .. (reason and ("because of \"" .. reason .. "\"") or "" ) )
- return
- end
- local pass = teams[name].password
- if pass then
- if password then
- if password == pass then
- table.insert( teams[name].users, username )
- users[username] = name;
- chatbox.tell( username, "Successfully joined team!" )
- else
- chatbox.tell( username, "Invalid password" )
- end
- else
- chatbox.tell( username, "This team has a password" )
- end
- else
- table.insert( teams[name].users, username )
- users[username] = name;
- chatbox.tell( username, "Successfully joined team!" )
- end
- else
- chatbox.tell( username, "There's no such team" )
- end
- else
- chatbox.tell( username, "You're already in a team!" )
- end
- else
- chatbox.tell( username, "##team join <name> <-password>" )
- end
- end;
- };
- ["disband"] = {
- description = "Disbands the team";
- action = function( username, ... )
- local args = {...}
- if users[username] then
- local name = users[username]
- local text, password = unpack( args )
- if teams[name].leader == username then
- if text and text == "confirm" then
- local disband = false
- if teams[name].password then
- if password then
- if password == teams[name].password then
- disband = true
- else
- chatbox.tell( username, "Invalid password" )
- end
- else
- chatbox.tell( username, "You need to input the password in order to disband the team" )
- end
- else
- disband = true
- end
- if disband then
- broadcast( "Alert", name, username .. " has disbanded the team" )
- for _, user in ipairs( teams[name].users ) do
- users[user] = nil
- end
- teams[name] = nil
- end
- else
- chatbox.tell( username, "You need to confirm that you want to disband the team, \"##disband confirm <-password>\"" )
- end
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can disband the team" )
- end
- else
- chatbox.tell( "You're not in a team, what're you expecting to disband? .-." )
- end
- end
- };
- ["say"] = {
- description = "Say something to the team";
- action = function( username, ... )
- local args = {...}
- if users[username] then
- if #args >= 1 then
- local text = ""
- for i = 1, #args do
- text = text .. args[i] .. " "
- end
- broadcast( username, users[username], text )
- else
- chatbox.tell( username, "##team say <message>" )
- end
- else
- chatbox.tell( username, "You're not in a team, no-one will hear you" )
- end
- end;
- };
- ["info"] = {
- description = "Show info about the current team";
- action = function( username, ... )
- local args = {...}
- if users[username] then
- local team = teams[users[username]]
- chatbox.tell( username, "# Team info" )
- chatbox.tell( username, "Team name: " .. users[username] )
- chatbox.tell( username, "Team Leader: " .. team.leader )
- chatbox.tell( username, "Members: " .. #team.users )
- else
- chatbox.tell( username, "You're not in a team, can not show any info" )
- end
- end;
- };
- ["members"] = {
- descriptions = "Shows all members in the current team";
- action = function( username, ... )
- local args = {...}
- if users[username] then
- local team = teams[users[username]]
- local text = ""
- for _, user in ipairs( team.users ) do
- text = text .. user .. ", "
- end
- text = text:sub( 1, #text - 2 )
- chatbox.tell( username, "# Team Members" )
- chatbox.tell( username, text )
- else
- chatbox.tell( username, "You're not in a team, can not show any info" )
- end
- end;
- };
- ["removepassword"] = {
- description = "Removes the password of the team";
- action = function( username, ... )
- if users[username] then
- local name = users[username]
- if teams[name].leader == username then
- broadcast( "Alert", name, username .. " has removed the password" )
- teams[name].password = nil
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can remove the password" )
- end
- else
- chatbox.tell( username, "You don't belong to any team, don't expect to remove a password of something that doesn't exist" )
- end
- end;
- };
- ["setpassword"] = {
- description = "Set/Change password of the team";
- action = function( username, ... )
- local args = {...}
- local password = unpack( args )
- if users[username] then
- local name = users[username]
- if teams[name].leader == username then
- if password then
- teams[name].password = password
- broadcast( "Alert", name, username .. " has changed the password" )
- chatbox.tell( username, "Password set to: " .. password )
- else
- chatbox.tell( username, "You need to specify a password, \"##team setpassword <password>\"" )
- end
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can change the password" )
- end
- else
- chatbox.tell( username, "You don't belong to any team, so this command is pretty useless for you" )
- end
- end;
- };
- ["kick"] = {
- description = "Kicks a user from the team";
- action = function( username, ... )
- local args = {...}
- local user = args[1]
- local reason
- if #args >= 2 then
- reason = ""
- for i = 2, #args do
- reason = reason .. args[i] .. " "
- end
- reason = reason:sub( 1, #reason - 1 )
- end
- if users[username] then
- local name = users[username]
- if teams[name].leader == username then
- if user then
- if users[user] and users[user] == name then
- if user == username then
- chatbox.tell( username, "You can't kick yourself" )
- else
- chatbox.tell( user, "You've been kicked from this team " .. (reason and ("because of \"" .. reason .. "\"") or "" ) )
- for i = 1, #teams[name].users do
- if teams[name].users[i] == user then
- table.remove( teams[name].users, i )
- break
- end
- end
- broadcast( "Alert", name, username .. " has kicked " .. user .. " from the team " .. (reason and ("because of \"" .. reason .. "\"") or "" ) )
- end
- else
- chatbox.tell( username, "This user does not belong to this team" )
- end
- else
- chatbox.tell( username, "You need to specify a username" )
- end
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can kick people" )
- end
- else
- chatbox.tell( username, "You don't belong to any team, so this command is pretty useless for you" )
- end
- end;
- };
- ["ban"] = {
- description = "Bans a user from the team";
- action = function( username, ... )
- local args = {...}
- local user = args[1]
- local reason
- if #args >= 2 then
- reason = ""
- for i = 2, #args do
- reason = reason .. args[i] .. " "
- end
- reason = reason:sub( 1, #reason - 1 )
- end
- if users[username] then
- local name = users[username]
- if teams[name].leader == username then
- if user then
- if users[user] and users[user] == name then
- if user == username then
- chatbox.tell( username, "You can't ban yourself" )
- else
- chatbox.tell( user, "You've been banned from this team " .. (reason and ("because of \"" .. reason .. "\"") or "" ) )
- for i = 1, #teams[name].users do
- if teams[name].users[i] == user then
- table.remove( teams[name].users, i )
- break
- end
- end
- teams[name].blacklist[user] = type( reason ) == "string" and reason or true
- broadcast( "Alert", name, username .. " has banned " .. user .. " from the team " .. (reason and ("because of \"" .. reason .. "\"") or "" ) )
- end
- else
- chatbox.tell( username, "This user does not belong to this team" )
- end
- else
- chatbox.tell( username, "You need to specify a username" )
- end
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can ban people" )
- end
- else
- chatbox.tell( username, "You don't belong to any team, so this command is pretty useless for you" )
- end
- end;
- };
- ["unban"] = {
- description = "Unbans a user from the team";
- action = function( username, ... )
- local args = {...}
- local user = args[1]
- local reason
- if #args >= 2 then
- reason = ""
- for i = 2, #args do
- reason = reason .. args[i] .. " "
- end
- reason = reason:sub( 1, #reason - 1 )
- end
- if users[username] then
- local name = users[username]
- if teams[name].leader == username then
- if user then
- if teams[name].blacklist[user] then
- chatbox.tell( user, "You've been unbanned from the team: \"" .. name .. "\"" )
- teams[name].blacklist[user] = false
- broadcast( "Alert", name, username .. " has unbanned " .. user .. " from the team " )
- else
- chatbox.tell( username, "This user does not belong to this team" )
- end
- else
- chatbox.tell( username, "You need to specify a username" )
- end
- else
- chatbox.tell( username, "You're not the leader of this team, only the leader can ban people" )
- end
- else
- chatbox.tell( username, "You don't belong to any team, so this command is pretty useless for you" )
- end
- end;
- };
- ["list"] = {
- descriptions = "Lists all teams";
- action = function( username, ... )
- if getLength( teams ) > 0 then
- chatbox.tell( username, "# Teams Available" )
- local text = ""
- for k, v in pairs( teams ) do
- text = text .. k .. ", "
- end
- text = text:sub( 1, #text - 2 )
- chatbox.tell( username, text )
- else
- chatbox.tell( username, "There are no teams, why don't you create one? \"##team create <name> <-password>\"" )
- end
- end
- };
- }
- local function backup()
- save( users, "teams-users.data" )
- save( teams, "teams-teams.data" )
- end
- local function main()
- if fs.exists( "teams-users.data" ) then
- users = load( "teams-users.data" )
- end
- if fs.exists( "teams-teams.data" ) then
- teams = load( "teams-teams.data" )
- end
- local key_commands = {
- "join", "create", "kick", "ban", "unban", "disband", "setpassword", "removepassword",
- }
- while true do
- local e = { os.pullEvent() }
- if e[1] == "chatbox_command" then
- local username, command = e[3], e[4]:match( "^%s*team%s+(.+)$" )
- if command ~= nil and command ~= "" then
- local args = {}
- for arg in command:gmatch( "%S+" ) do
- table.insert( args, arg )
- end
- command = args[1]:lower()
- table.remove( args, 1 )
- if commands[command] then
- commands[command].action( username, unpack( args ) )
- else
- chatbox.tell( username, "Invalid command, try \"help\" for a list of commands" )
- end
- for i, v in ipairs( key_commands ) do
- if command == v then
- backup()
- break
- end
- end
- else
- if e[4] == "team" then
- chatbox.tell( username, "Usage" )
- chatbox.tell( username, "##team <command> <...>" )
- chatbox.tell( username, "# Commands Available" )
- local text = ""
- for i = 1, #list do
- text = text .. list[i] .. ( i ~= #list and ", " or "" )
- end
- chatbox.tell( username, text )
- end
- end
- elseif e[1] == "timer" and e[2] == timer then
- backup()
- timer = os.startTimer( time )
- end
- end
- end
- if autorestart then
- while true do
- term.clear()
- term.setCursorPos( 1, 1 )
- print( "MoarPeripherals - Teams" )
- local ok, err = pcall( main )
- end
- else
- term.clear()
- term.setCursorPos( 1, 1 )
- print( "MoarPeripherals - Teams" )
- local ok, err = pcall( main )
- if not ok and err ~= "Terminated" then
- term.clear()
- term.setCursorPos( 1, 1 )
- printerror( "An unexpected error occured: " .. err )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement