mirror of
https://github.com/nishiki/botish.git
synced 2024-10-26 23:23:17 +00:00
feat: add modules
This commit is contained in:
parent
7d31b828fb
commit
c148828690
4 changed files with 88 additions and 37 deletions
|
@ -4,4 +4,8 @@ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
||||||
|
|
||||||
require 'botish'
|
require 'botish'
|
||||||
|
|
||||||
Botish.new.listen
|
Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f|
|
||||||
|
require_relative f
|
||||||
|
end
|
||||||
|
|
||||||
|
Botish::Botish.new.listen
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
require 'socket'
|
require 'socket'
|
||||||
|
|
||||||
class Botish
|
module Botish
|
||||||
|
class Botish
|
||||||
HOST = 'irc.freenode.net'
|
HOST = 'irc.freenode.net'
|
||||||
PORT = 6667
|
PORT = 6667
|
||||||
USERNAME = 'botish'
|
USERNAME = 'botish'
|
||||||
|
@ -11,15 +12,15 @@ class Botish
|
||||||
def initialize
|
def initialize
|
||||||
@connection = TCPSocket.open(HOST, PORT)
|
@connection = TCPSocket.open(HOST, PORT)
|
||||||
|
|
||||||
send("NICK #{USERNAME}")
|
forward("NICK #{USERNAME}")
|
||||||
send("USER #{USERNAME} localhost * :#{USERNAME}")
|
forward("USER #{USERNAME} localhost * :#{USERNAME}")
|
||||||
Kernel.loop do
|
Kernel.loop do
|
||||||
msg = @connection.gets
|
msg = @connection.gets
|
||||||
log("<- #{msg}")
|
log("<- #{msg}")
|
||||||
break if msg.include?('End of /MOTD command.')
|
break if msg.include?('End of /MOTD command.')
|
||||||
end
|
end
|
||||||
send("JOIN #{CHANNEL}")
|
forward("JOIN #{CHANNEL}")
|
||||||
send("PRIVMSG #{CHANNEL} :Je suis là :')")
|
forward("PRIVMSG #{CHANNEL} :Je suis là :')")
|
||||||
end
|
end
|
||||||
|
|
||||||
def listen
|
def listen
|
||||||
|
@ -35,13 +36,23 @@ class Botish
|
||||||
def parse(msg)
|
def parse(msg)
|
||||||
case msg
|
case msg
|
||||||
when /^PING (?<host>.+)/
|
when /^PING (?<host>.+)/
|
||||||
send("PONG #{Regexp.last_match('host')}")
|
forward("PONG #{Regexp.last_match('host')}")
|
||||||
|
|
||||||
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
|
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
|
||||||
send("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
|
forward("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
|
||||||
|
|
||||||
|
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG (?<channel>#?[[:alpha:]]+) :#{USERNAME}: (?<command>[[:lower:]]+)( (?<args>.+))?/
|
||||||
|
command = Regexp.last_match('command')
|
||||||
|
options =
|
||||||
|
%i[user channel args].each_with_object({}) do |key, opts|
|
||||||
|
opts[key] = Regexp.last_match(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
Kernel.const_get("Botish::#{command.capitalize}").new(@connection, options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def send(msg)
|
def forward(msg)
|
||||||
log("-> #{msg}")
|
log("-> #{msg}")
|
||||||
@connection.puts(msg)
|
@connection.puts(msg)
|
||||||
end
|
end
|
||||||
|
@ -49,4 +60,5 @@ class Botish
|
||||||
def log(msg)
|
def log(msg)
|
||||||
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
9
modules/coucou.rb
Normal file
9
modules/coucou.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
module Botish
|
||||||
|
class Coucou
|
||||||
|
def initialize(connection, args)
|
||||||
|
@connection = connection
|
||||||
|
|
||||||
|
@connection.puts("PRIVMSG #{args[:channel]} :#{args[:user]}: coucou mon petit")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
26
modules/wikipedia.rb
Normal file
26
modules/wikipedia.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
require 'json'
|
||||||
|
require 'net/http'
|
||||||
|
|
||||||
|
module Botish
|
||||||
|
class Wikipedia
|
||||||
|
def initialize(connection, args)
|
||||||
|
@connection = connection
|
||||||
|
|
||||||
|
params = {
|
||||||
|
format: 'json',
|
||||||
|
action: 'query',
|
||||||
|
list: 'search',
|
||||||
|
utf8: true,
|
||||||
|
srsearch: args[:args]
|
||||||
|
}
|
||||||
|
uri = URI('https://fr.wikipedia.org/w/api.php')
|
||||||
|
uri.query = URI.encode_www_form(params)
|
||||||
|
|
||||||
|
data = JSON.parse(Net::HTTP.get(uri))['query']['search'][0]
|
||||||
|
title = data['title']
|
||||||
|
url = URI.encode("https://fr.wikipedia.org/wiki/#{title.tr(' ', '_')}")
|
||||||
|
|
||||||
|
@connection.puts("PRIVMSG #{args[:channel]} :#{args[:user]}: #{data['title']} => #{url}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue