1
0
Fork 0
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:
Adrien Waksberg 2017-04-17 22:57:29 +02:00
parent 7d31b828fb
commit c148828690
4 changed files with 88 additions and 37 deletions

View file

@ -4,4 +4,8 @@ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
require 'botish'
Botish.new.listen
Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f|
require_relative f
end
Botish::Botish.new.listen

View file

@ -2,7 +2,8 @@
require 'socket'
class Botish
module Botish
class Botish
HOST = 'irc.freenode.net'
PORT = 6667
USERNAME = 'botish'
@ -11,15 +12,15 @@ class Botish
def initialize
@connection = TCPSocket.open(HOST, PORT)
send("NICK #{USERNAME}")
send("USER #{USERNAME} localhost * :#{USERNAME}")
forward("NICK #{USERNAME}")
forward("USER #{USERNAME} localhost * :#{USERNAME}")
Kernel.loop do
msg = @connection.gets
log("<- #{msg}")
break if msg.include?('End of /MOTD command.')
end
send("JOIN #{CHANNEL}")
send("PRIVMSG #{CHANNEL} :Je suis là :')")
forward("JOIN #{CHANNEL}")
forward("PRIVMSG #{CHANNEL} :Je suis là :')")
end
def listen
@ -35,13 +36,23 @@ class Botish
def parse(msg)
case msg
when /^PING (?<host>.+)/
send("PONG #{Regexp.last_match('host')}")
forward("PONG #{Regexp.last_match('host')}")
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
def send(msg)
def forward(msg)
log("-> #{msg}")
@connection.puts(msg)
end
@ -49,4 +60,5 @@ class Botish
def log(msg)
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
end
end
end

9
modules/coucou.rb Normal file
View 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
View 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