|
# |
|
# By Callum Taylor |
|
# Usage: |
|
# !reset -> resets the counts back to 0/0 |
|
# !hit [num] -> adds [num] to the current success rate, and increases the attempt count by [num] (num is optional, defaults to 1) |
|
# !miss [num] -> adds [num] to the current attempts (num is optional, defaults to 1) |
|
# !results -> displays current results |
|
# !rateset [success] [total] -> sets the current values of success/total rates |
|
# !rateadd [username] -> adds a user to the allowed user list to execute commands |
|
# !rateremove [username] -> removes a user from the allowed user list to execute commands |
|
# |
|
|
|
require "socket" |
|
|
|
server = "irc.twitch.tv" |
|
port = "6667" |
|
nick = "scruffyfox" |
|
channel = "#pyrionflax" |
|
success_command = "!hit" |
|
fail_command = "!miss" |
|
results_command = "!results" |
|
set_command = "!rateset" |
|
add_nick_command = "!rateadd" |
|
remove_nick_command = "!rateremove" |
|
allowed_nicks = [nick] |
|
allowed_commands = [success_command, fail_command, results_command, set_command, add_nick_command, remove_nick_command, "!reset"] |
|
|
|
success = 0 |
|
max = 0 |
|
|
|
s = TCPSocket.open(server, port) |
|
print("addr: ", s.addr.join(":"), "\n") |
|
print("peer: ", s.peeraddr.join(":"), "\n") |
|
s.puts "PASS oauth:xxxx" |
|
s.puts "USER scruffyfox 0 * ScruffyFox" |
|
s.puts "NICK #{nick}" |
|
s.puts "JOIN #{channel}" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{success_command} 1" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{fail_command} 1" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{set_command} 5 10" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{results_command}" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{add_nick_command} scruffyfox" |
|
# test = ":#{nick}!#{nick}@#{nick}.tmi.twitch.tv PRIVMSG #{channel} :#{remove_nick_command} scruffyfox" |
|
|
|
until s.eof? do |
|
msg = s.gets |
|
# msg = test |
|
|
|
if (msg.include? "PING :") |
|
s.puts msg.gsub(/PING/, "PONG") |
|
end |
|
|
|
validated = false |
|
validated = allowed_nicks.any? { |nickname| msg.include? ":#{nickname}!#{nickname}@#{nickname}.tmi.twitch.tv" } |
|
validated &= allowed_commands.any? { |command| msg.include?(command) } |
|
|
|
puts msg |
|
|
|
if (validated) |
|
print = true |
|
start = msg.index("PRIVMSG #{channel} :") + "PRIVMSG #{channel} :".length |
|
len = msg.length - start |
|
command = msg[start, len] |
|
|
|
if (command.include? "!reset") |
|
success = 0 |
|
max = 0 |
|
elsif (command.include? "#{success_command}") |
|
end_command = command[success_command.length, command.length].strip! |
|
count = (end_command || " ").split(" ")[0].to_i |
|
success = success + (count == 0 ? 1 : count) |
|
max = max + (count == 0 ? 1 : count) |
|
elsif (command.include? "#{fail_command}") |
|
end_command = command[fail_command.length, command.length].strip! |
|
count = end_command.split(" ")[0].to_i |
|
max = max + (count == 0 ? 1 : count) |
|
elsif (command.include? "#{set_command}") |
|
end_command = command[set_command.length, command.length].strip! |
|
count_success = end_command.split(" ")[0].to_i |
|
count_total = end_command.split(" ")[1].to_i |
|
success = count_success |
|
max = count_total |
|
elsif (command.include? "#{add_nick_command}") |
|
end_command = command[add_nick_command.length, command.length].strip! |
|
puts end_command |
|
user = end_command.split(" ")[0] |
|
allowed_nicks.push(user) |
|
puts allowed_nicks.to_s |
|
print = false |
|
elsif (command.include? "#{remove_nick_command}" and !command.include? "#{nick}") |
|
end_command = command[remove_nick_command.length, command.length].strip! |
|
user = end_command.split(" ")[0] |
|
allowed_nicks.delete(user) |
|
puts allowed_nicks.to_s |
|
print = false |
|
end |
|
|
|
if (print) |
|
s.puts ("PRIVMSG #{channel} :/me ~~ success rate: " + (success.to_s + "/" + max.to_s + " ---> " + (success == 0 ? "0%" : (success.to_f / (max == 0 ? 1 : max.to_f)) * 100).to_i.to_s + "% ~~")).encode('utf-8') |
|
end |
|
end |
|
end |