Ruby“包括?”方法不起作用

时间:2018-03-12 20:49:52

标签: ruby chatbot twitch

我有一些来自Twitch聊天的命令,它有效并且很棒!人们可以键入!about 等命令,然后发送回复。只有一个问题,如果你转到thread并查看# // COMMANDS // COMMANDS // COMMANDS下面。有一个问题,有一些管理命令(在# ADMIN COMMANDS // ADMIN COMMANDS下查看)。我有这个:if msg.include?("!project")然后是msg.slice!("!project "),但它不像我想要的那样工作。我希望能够输入!project然后输入一些子串。但是当附加子字符串时,即使我可以打印msg并且它明显包含!project ,它也不起作用。 !disconnect !project 命令都可以在它们自己只有这些命令时工作。意味着像!disconnect Hello World 这样的东西不会起作用,即使它明显包含!disconnect

# Message formatting in console
class String
    def red;            "\e[31m#{self}\e[0m" end
    def yellow;        "\e[33m#{self}\e[0m" end
    def green;          "\e[32m#{self}\e[0m" end
    def cyan;           "\e[36m#{self}\e[0m" end
    def bold;           "\e[1m#{self}\e[22m" end
end

# Requied packages / modules
require 'socket'
require 'logger'
require 'open-uri'

# Create logger
File.delete("log.txt") # Clear previous log
log = Logger.new("log.txt", formatter: proc {|severity, datetime, progname, msg|
    "#{datetime}: #{msg}\n"})

# Required Info
load "credentials.txt"
log.info("Loading \"credentials.txt\"")

# -------- IGNORE -------- #
OAUTH.downcase!
BOTNAME.downcase!
CHANNEL.downcase!.gsub!("#", "")

# //- AGE -// #
time = Time.new
age = time.year - 2000
if time.month == 10
    if time.day < 28
        age -= 1
    end
elsif time.month < 10
    age -= 1
end
# -------- IGNORE -------- #

# Save "Preparing to connect" to "log.txt"
log.info("Preparing to connect")

# Variables
socket = TCPSocket.new('irc.chat.twitch.tv', 6667)
send = "PRIVMSG ##{CHANNEL} :"                      # shortcut for sending messages
running = true
content = nil
message_count = 0
message_limit = Time.now.to_i

# Commands
commands = ["!about","!uptime","!commands","!cortexio","!followed"]
api_commands = ["!followed","!uptime"]
admin_commands = ["!disconnect","!project"]

# Authorization Login
socket.puts("PASS #{OAUTH}")                    # Send the password(oauth) to Twitch
socket.puts("NICK #{BOTNAME}")                  # Send the botname to Twitch
socket.puts("JOIN ##{CHANNEL}")                 # Send the channel to Twitch

# Save "Connected!" to "log.txt
log.info("Joining #{CHANNEL.capitalize} as #{BOTNAME.capitalize} using OAUTH Token: #{OAUTH[6,OAUTH.length-12]}" + "*"*12)

# Thread.abort_on_exception = true

# Loop (Background Thread) for recieving Twitch chat data
Thread.start do
    socket.puts(send + "Connected!")                # Send "Connected!" to the Twitch channel
    puts "#{BOTNAME} Joined ##{CHANNEL}"                # Connection Status
    puts "You should be fully connected now"        # Connection Status
    puts ""
    puts "Type \"clear\" to clear terminal"
    puts ""
    while (running) do
        ready = IO.select([socket])

        ready[0].each do |s|
            line = s.gets
            # Respond to Twitch IRC "PING" Message
            if line.index("PING") == 0
                line.strip!
                socket.puts("PONG :tmi.twitch.tv\r\n")
                log.info("[IRC Message]: " + line)
                log.info("[IRC Response]: PONG :tmi.twitch.tv")
                puts("-".bold.red*line.length)
                puts "[Twitch] ".bold.cyan + "IRC:  ".bold.yellow + line.bold.green
                puts "[Response] ".bold.cyan + "IRC: ".bold.yellow + "PONG :tmi.twitch.tv".bold.green
                puts("-".bold.red*line.length)
            end
            match = line.match(/^:(.+)!(.+)PRIVMSG ##{CHANNEL} :(.+)$/)
            message = match && match[3]
            if message =~ /^/
                message.strip!
                user = match[1]         # Get username

                # Twitch message limit - (Max 100 messages in 30 secs - Applies to mods and above)
                # Avoid global ban
                if Time.now.to_i - message_limit > 30 # If more than 30 seconds has passed
                    message_count = 0 # Reset "message_count"
                end
                if message_count == 0 # If "message_count" is 0
                    message_limit = Time.now.to_i # Start counting to 30 again
                end
                message_count = message_count + 1
            end

            # // COMMANDS // COMMANDS // COMMANDS
            if message != nil
                msg = message.downcase
                # ADMIN COMMANDS // ADMIN COMMANDS
                if admin_commands.include?(msg) and user == CHANNEL
                    if msg.include?("!disconnect")
                        socket.puts(send + "Disconnecting") # Disconnect from the channel
                        socket.puts("PART ##{CHANNEL}")     # Disconnect from the channel
                        Disconnect()
                        log.info("[Command] #{user}: #{message}")
                    elsif msg.include?("!project")
                        msg.slice!("!project ")
                        File.write("Responses/project.txt", msg)
                    end
                user = user.capitalize  # Capitalize first letter (Cuz I'm that kind of person)
                elsif commands.include?(msg) and message_count < 80
                    puts "[Command] ".bold.cyan + "#{user}: ".bold + "#{message}".bold.cyan
                    # API COMMANDS // API COMMANDS
                    if api_commands.include?(msg)
                        if msg.include?("!uptime")
                            file = open("https://decapi.me/twitch/uptime?channel=#{CHANNEL}")
                            content = "#{CHANNEL} has been streaming for: " + file.read
                        elsif msg.include?("!followed")
                            file = open("https://decapi.me/twitch/followage/#{CHANNEL}/#{user}")
                            content = file.read
                            if content == "Follow not found"
                                content = "#{user} is not following #{CHANNEL}"
                            else
                                content = "#{user} has been following #{CHANNEL} for " + content
                            end
                        end
                        puts "[Response] ".bold.red + "Cortexio: ".bold + "API: ".bold.yellow + "\"#{content}\"".bold.red
                    else
                        file = open "Responses/" + msg.gsub!("!","") + ".txt" # open matching file
                        content = file.read
                        file.close
                        puts "[Response] ".bold.red + "Cortexio: ".bold + "File: ".bold.yellow + "\"#{msg}.txt\"".bold.red
                    end
                    file.close
                    log.info("[Command] #{user}: #{message}")
                else
                    puts "[Message] ".bold.green + "#{user}: ".bold + "#{message}".bold.green
                    log.info("[Message] #{user}: #{message}")
                    if message[0] == "!" # Unrecognized command
                        content = "Unrecognized command: \"#{message}\" - Type !commands to see a list of available commands."
                    end
                end
                # Response handling
                if content != nil
                    content.gsub!("USER", "@#{user}")
                    content.gsub!("AGE", "#{age}")
                    content.gsub!("CHANNEL", "#{CHANNEL}")
                    if content.include?("COMMANDS")
                        content.gsub!("COMMANDS", "#{commands}")
                        content.gsub!("\"", "")
                        content.gsub!("[","")
                        content.gsub!("]","")
                    end
                    socket.puts(send + content) # Send response if any
                    content = nil # Too avoid multiple messages with the same response
                end
            end
        end
    end
end

def Disconnect() # End script
    running = false
    exit
end

# Loop to keep bot going
while (running) do
    input = gets.chomp
    if input == "clear"
        system "clear" or system "cls"
    end
end

1 个答案:

答案 0 :(得分:1)

问题不在于

if msg.include?("!project")

它在前面的行:

admin_commands.include?(msg)

admin_commands是一个包含2个字符串["!disconnect","!project"]的数组。但,包含字符串"!project Then some substring here",因此您永远不会检查是否msg.include?("!project")。您应该看到有关无法识别的命令的日志消息,这意味着您没有将其转换为您的第一个if语句。你想要做的是:

if admin_commands.any? { |command| msg.include?(command) }
  if msg.include?("!project")
    # ...