Ruby “红宝石”;包括?“;方法不起作用
我有一些来自Twitch聊天的命令,它很有效,非常棒!人们可以输入像这样的命令!关于,它将发送响应。只有一个问题,如果您转到Ruby “红宝石”;包括?“;方法不起作用,ruby,chatbot,twitch,Ruby,Chatbot,Twitch,我有一些来自Twitch聊天的命令,它很有效,非常棒!人们可以输入像这样的命令!关于,它将发送响应。只有一个问题,如果您转到线程并查看#//命令//COMMANDS//COMMANDS。有一个问题,有几个管理命令(请查看#admin commands//admin commands)。我有这样一个:if msg.include?(“!project”)thenmsg.slice!(“!project”),但它没有按我希望的那样工作。我想能够键入!然后在此处投影一些子字符串。但是当附加子字符串时,
线程
并查看#//命令//COMMANDS//COMMANDS
。有一个问题,有几个管理命令(请查看#admin commands//admin commands
)。我有这样一个:if msg.include?(“!project”)
thenmsg.slice!(“!project”)
,但它没有按我希望的那样工作。我想能够键入!然后在此处投影一些子字符串。但是当附加子字符串时,它不起作用,即使我可以打印msg
,并且它清楚地包含!项目。两者都是!断开与的连接!项目只有这些命令本身才起作用。意思是像这样的东西!断开Hello World的连接将不起作用,即使它显然包括!断开连接
# 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
控制台中的消息格式设置
类字符串
def红色;“\e[31m{self}\e[0m”结束
def黄色;“\e[33m{self}\e[0m”结束
def绿色;“\e[32m{self}\e[0m”结束
def青色;“\e[36m{self}\e[0m”结束
def bold;“\e[1m{self}\e[22m”结束
结束
#所需的软件包/模块
需要“插座”
需要“记录器”
需要“打开uri”
#创建记录器
File.delete(“log.txt”)#清除以前的日志
log=Logger.new(“log.txt”,格式化程序:proc{| severity,datetime,progname,msg|
“#{datetime}:#{msg}\n”})
#所需信息
加载“credentials.txt”
log.info(“正在加载\“credentials.txt\”)
#-----忽略--------#
唐卡斯!
僵尸名字,唐卡斯!
CHANNEL.downcase!.gsub!(“#”,”)
#//-年龄-//#
时间=时间
年龄=time.year-2000
如果time.month==10
如果time.day<28
年龄-=1
结束
elsif时间月<10
年龄-=1
结束
#-----忽略--------#
#将“准备连接”保存到“log.txt”
log.info(“准备连接”)
#变数
socket=TCPSocket.new('irc.chat.twitch.tv',6667)
send=“PRIVMSG##{CHANNEL}:#发送消息的快捷方式
运行=真
内容=零
消息计数=0
message_limit=Time.now.to_i
#命令
commands=[“!about”、“!uptime”、“!commands”、“!cortexio”、“!followered”]
api_命令=[“!跟随”、“!正常运行时间”]
admin_命令=[“!disconnect”,“!project”]
#授权登录
socket.put(“PASS#{OAUTH}”)#将密码(OAUTH)发送给Twitch
socket.put(“NICK#{BOTNAME}”)#将BOTNAME发送给Twitch
socket.put(“JOIN##{CHANNEL}”)#将频道发送给Twitch
#将“已连接!”保存到“log.txt”
log.info(“使用OAUTH标记将#{CHANNEL.capitalize}连接为#{BOTNAME.capitalize}:#{OAUTH[6,OAUTH.length-12]}”+“*”*12)
#Thread.abort_on_异常=true
#用于接收Twitch聊天数据的循环(后台线程)
Thread.start do
socket.put(发送+“已连接!”)#发送“已连接!”到Twitch频道
将“#{BOTNAME}连接##{CHANNEL}”置于连接状态
显示“您现在应该完全连接”#连接状态
放置“”
将“类型\“清除\”置于清除终端”
放置“”
(跑步时)做什么
就绪=IO。选择([套接字])
准备就绪[0]。每个do|
line=s.get
#响应Twitch IRC“PING”消息
如果行索引(“PING”)==0
行,快走!
socket.put(“PONG:tmi.twitch.tv\r\n”)
log.info(“[IRC消息]:”+行)
log.info(“[IRC响应]:PONG:tmi.twitch.tv”)
放置(“-”。粗体。红色*线条。长度)
放置“[Twitch]”。bold.青色+“IRC:“.bold.黄色+线条。bold.绿色”
将“[响应]”.bold.青色+”置于IRC:“.bold.黄色+”PONG:tmi.twitch.tv“。bold.green
放置(“-”。粗体。红色*线条。长度)
结束
match=line.match(/^:(.+)!(.+)PRIVMSG##{CHANNEL}:(.+)$/)
消息=匹配和匹配[3]
如果消息=~/^/
留言,脱衣舞!
user=匹配[1]#获取用户名
#Twitch消息限制-(30秒内最多100条消息-适用于mods及以上)
#避免全球禁令
如果Time.now.to_i-消息限制>30#如果超过30秒
消息计数=0#重置“消息计数”
结束
如果消息计数==0#如果“消息计数”为0
message_limit=Time.now.to_i#再次开始计数到30
结束
消息计数=消息计数+1
结束
#//命令//命令//命令
如果消息!=nil
msg=message.downcase
#管理命令//管理命令
if admin_命令.include?(msg)和user==通道
如果消息包括(“!断开”)
插座.puts(发送+“断开”)#断开与通道的连接
socket.put(“PART##{CHANNEL}”)#断开与通道的连接
断开连接()
log.info(“[Command]{user}:{message}”)
elsif消息。包括(“!项目”)
msg.slice!(“!project”)
File.write(“Responses/project.txt”,msg)
结束
user=user.capitalize#大写第一个字母(因为我就是那种人)
elsif命令。包括?(msg)和消息计数<80
将“[Command]”.bold.cyan+“#{user}:“.bold+”#{message}.bold.cyan
#API命令//API命令
如果api_命令包含?(msg)
如果消息包括(“!正常运行时间”)
文件=打开(“https://decapi.me/twitch/uptime
if msg.include?("!project")
admin_commands.include?(msg)
if admin_commands.any? { |command| msg.include?(command) }
if msg.include?("!project")
# ...