wm: irc-h2o-bot

Download patch

ref: fc2812c9314da5925a65b970378dc18a92c1ce56
parent: 04f5e130140d3a42306923498cb69ea80982357c
author: 0x4A4D00 <scorpion_rn@yahoo.com>
date: Mon Jul 31 18:05:29 EDT 2023

Attempt to Make bot Multi Server

--- a/.idea/irc-h2o-bot.iml
+++ b/.idea/irc-h2o-bot.iml
@@ -12,5 +12,11 @@
     <orderEntry type="jdk" jdkName="ruby-3.2.1-p31" jdkType="RUBY_SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" scope="PROVIDED" name="Ruby-IRC (v1.0.13, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="async (v2.6.2, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="console (v1.17.4, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="fiber-annotation (v0.2.0, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="fiber-local (v1.0.0, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="io-event (v1.2.2, ruby-3.2.1-p31) [gem]" level="application" />
+    <orderEntry type="library" scope="PROVIDED" name="timers (v4.3.5, ruby-3.2.1-p31) [gem]" level="application" />
   </component>
 </module>
\ No newline at end of file
--- a/CONFIG
+++ b/CONFIG
@@ -1,6 +1,14 @@
 {
+"1":{
+  "IRCServer": "irc.freenode.net",
+  "BotNickName": "wat",
+  "SessionUrl": "https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909",
+  "SessionCookie": "h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4"
+},
+"2":{
   "IRCServer": "irc.ircnow.org",
   "BotNickName": "h2o",
   "SessionUrl": "https://gpt-gm.h2o.ai/conversation/64ac1344245bd909c99ed909",
   "SessionCookie": "h2ogpt-chat=a4fc9f08-6c67-466c-8d36-dd425702d6b4"
-}
\ No newline at end of file
+}
+}
--- a/bot.rb
+++ b/bot.rb
@@ -2,33 +2,108 @@
 require 'uri'
 require 'json'
 require 'IRC'
+require 'async'
 
-config = File.read('CONFIG')
+# 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)
-Server = parsed_config['IRCServer']
-BotNick = parsed_config['BotNickName']
-$URL = parsed_config['SessionUrl']
-$Cookie = parsed_config['SessionCookie']
+# parsed_config = JSON.parse(config)
 
-$bot = IRC.new(
-  BotNick,
-  Server,
-  "6667",
-  "H2O")
 
+# $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...")
+      @bot.send_message(peer, "#{from}, Im thinking...")
     else
-      $bot.send_message(peer, "Im thinking...")
+      @bot.send_message(peer, "Im thinking...")
     end
   end
 
   def SendReq(peer, input, c)
-    uri = URI($URL)
+    uri = URI(@URL)
     https = Net::HTTP.new(uri.host, uri.port)
     https.use_ssl = true
 
@@ -36,7 +111,7 @@
 
     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['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":""}}'
 
@@ -63,12 +138,12 @@
           break
         elsif counts >= 50
         begin
-          $bot.send_message(peer, 'limit reached!')
+          @bot.send_message(peer, 'limit reached!')
           break
         end
         elsif result.include?("\n")
         begin
-          $bot.send_message(peer, b)
+          @bot.send_message(peer, b)
           sleep(0.3)
           counts += 1
           b = ''
@@ -78,45 +153,82 @@
         end
       end
 
-      $bot.send_message(peer, b)
+      @bot.send_message(peer, b)
 
     rescue StandardError => e
       if (e.to_s.start_with?("SSL_read"))
         retry
       end
-      $bot.send_message(peer, e)
+      @bot.send_message(peer, e)
     end
   end
 end
 
-sender = Sender.new
+# 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
+#
+# }
 
-IRCConnection.add_IO_socket(STDIN)
-IRCEvent.add_callback('endofmotd') { |event| $bot.add_channel('#fpc') }
+# 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
 
-IRCEvent.add_callback('privmsg') {|event|
+def Init()
+  config = File.read('CONFIG')
 
-  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
+  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
-  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
+  threads.each(&:join)
+end
 
-}
 
-$bot.connect
+Init()
\ No newline at end of file