Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $filename = 'users.txt'
- # Create a new hash
- $users = Hash.new
- # Open and read the contents into the hash
- def load
- # Create a new file if it does not exist
- unless File.exist? $filename
- # Open and immedietly close it (the reason for the block)
- File.open($filename, 'w') {}
- end
- # Open the file and iterate over each line
- File.open($filename, 'r') do |file|
- # Add all of the pairs to the hash
- while (line = file.gets)
- user, rank, userpassword = line.split
- $users[user] = { :rank => rank, :password => userpassword }
- end
- end
- end
- # Write the contents of the hash to a file
- def save
- File.open($filename, 'w') do |file|
- $users.each do |user, rank, userpassword|
- file.puts("#{user} #{rank} #{userpassword}")
- end
- end
- end
- # Remove a user
- def remove(nick)
- $users.delete nick
- end
- # Add a new user to the hash if not already registered
- def register(nick, userpassword)
- puts "Received !register from #{nick}"
- if $users.key? nick
- send "PRIVMSG #{nick} :You are already registered!"
- else
- if !userpassword
- send "PRIVMSG #{nick} :Correct syntax is: !register <password>"
- else
- $users[nick] ||= {}
- $users[nick][:rank] = 1
- $users[nick][:password] = userpassword
- send "PRIVMSG #{nick} :You have successfully registered!"
- end
- end
- end
- # Get a users rank
- def rank(nick)
- puts "Received !rank from #{nick}"
- if $users.key? nick
- send "PRIVMSG #{nick} :#{$users[:rank]}"
- else
- send "PRIVMSG #{nick} :You are not registered!"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement