ref: fc2812c9314da5925a65b970378dc18a92c1ce56
dir: /bot.rb/
require 'net/http' require 'uri' require 'json' require 'IRC' require 'async' # config = File.read('CONFIG') # # parsed_config = JSON.parse(config) # parsed_json_objects = [] # # parsed_config.each do |i, json| # @Server = json['IRCServer'] # @BotNick = json['BotNickName'] # @URL = json['SessionUrl'] # @Cookie = json['SessionCookie'] # # sender = Sender.new(server: Server, nick: BotNick, url: URL, cookie: Cookie) # # end # # parsed_config.length.times do |i| # Server = parsed_config[i.to_s]['IRCServer'] # BotNick = parsed_config[i.to_s]['BotNickName'] # $URL = parsed_config[i.to_s]['SessionUrl'] # $Cookie = parsed_config[i.to_s]['SessionCookie'] # end # # parsed_config.each do |json| # #parsed_config = JSON.parse(json) # # Server = json['IRCServer'] # BotNick = json['BotNickName'] # $URL = json['SessionUrl'] # $Cookie = json['SessionCookie'] # # end # parsed_config = JSON.parse(config) # $bot = IRC.new( # BotNick, # Server, # "6667", # "H2O") class Sender def ts() IRCConnection.add_IO_socket(STDIN) IRCEvent.add_callback('endofmotd') { |event| @bot.add_channel('#th2o');puts '#th2o' } IRCEvent.add_callback('privmsg') {|event| puts 'moz' if event.channel.eql?(@BotNick) # pv message begin if (event.message.start_with?(".invite")) # invite message begin event.message.slice! ".invite " @bot.add_channel(event.message) end else Wait(event.from) #@bot.send_message(event.from, "I'm thinking...") SendReq(event.from, event.message, '') end end else # channel message if (event.message.start_with?(@BotNick)) Wait(event.channel, event.from) #@bot.send_message(event.channel, "#{event.from}, I'm thinking...") SendReq(event.channel, event.message.gsub(/#{@BotNick+" "}\b/, ""), '') end end } puts 'ss' Async do |task| task.async do @bot.connect end end end def initialize(server:, nick:, url:, cookie:) @BotNick = nick @URL = url @Cookie = cookie @bot = IRC.new( nick, server, "6667", "H2O") end def Wait(peer, from = nil) if from != nil @bot.send_message(peer, "#{from}, Im thinking...") else @bot.send_message(peer, "Im thinking...") end end def SendReq(peer, input, c) uri = URI(@URL) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Post.new(uri.path) request['Content-Type'] = 'application/json; charset=UTF-8' request['User-Agent'] = 'Pedaret Mozilla/5.0 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.127 Safari/537.36' request['Cookie'] = @Cookie request.body = '{"inputs":"'+ input.to_s() +'","parameters":{"temperature":0.1,"truncate":2048,"max_new_tokens":1024,"do_sample":true,"repetition_penalty":1.2,"return_full_text":false},"stream":true,"options":{"id":"6543eb99-e311-4f4e-b791-7af4fcbe556b","response_id":"b194833d-e13c-4390-9d7f-3cf4e4d95430","is_retry":false,"use_cache":false,"web_search_id":""}}' begin response = https.request(request) json_strings = response.body.split('data:') parsed_json_objects = [] json_strings.each do |json_str| if json_str != '' parsed_json_objects << JSON.parse(json_str) end end counts = 0 b = '' parsed_json_objects.each do |json_obj| result = json_obj['token']['text'] if result == '<|endoftext|>' then break elsif counts >= 50 begin @bot.send_message(peer, 'limit reached!') break end elsif result.include?("\n") begin @bot.send_message(peer, b) sleep(0.3) counts += 1 b = '' end else b += result end end @bot.send_message(peer, b) rescue StandardError => e if (e.to_s.start_with?("SSL_read")) retry end @bot.send_message(peer, e) end end end # sender = Sender.new # # IRCConnection.add_IO_socket(STDIN) # IRCEvent.add_callback('endofmotd') { |event| $bot.add_channel('#fpc') } # # IRCEvent.add_callback('privmsg') {|event| # # if event.channel.eql?(BotNick) # pv message # begin # if (event.message.start_with?(".invite")) # invite message # begin # event.message.slice! ".invite " # $bot.add_channel(event.message) # end # else # sender.Wait(event.from) # #$bot.send_message(event.from, "I'm thinking...") # sender.SendReq(event.from, event.message, '') # end # end # else # channel message # if (event.message.start_with?("h2o")) # sender.Wait(event.channel, event.from) # #$bot.send_message(event.channel, "#{event.from}, I'm thinking...") # sender.SendReq(event.channel, event.message.gsub(/#{"h2o "}\b/, ""), '') # end # end # # } # config = File.read('CONFIG') # # parsed_config = JSON.parse(config) # parsed_json_objects = [] # # parsed_config.each do |i, json| # Server = json['IRCServer'] # BotNick = json['BotNickName'] # URL = json['SessionUrl'] # Cookie = json['SessionCookie'] # # sender = Sender.new(server: Server, nick: BotNick, url: URL, cookie: Cookie) # # end def Init() config = File.read('CONFIG') parsed_config = JSON.parse(config) parsed_json_objects = [] threads = [] parsed_config.each do |i, json| @Server = json['IRCServer'] @BotNick = json['BotNickName'] @URL = json['SessionUrl'] @Cookie = json['SessionCookie'] sender = Sender.new(server: @Server, nick: @BotNick, url: @URL, cookie: @Cookie) threads << Thread.new { sender.ts() } end threads.each(&:join) end Init()