mirror of
https://github.com/nishiki/botish.git
synced 2025-02-20 02:10:04 +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'
|
||||
|
||||
Botish.new.listen
|
||||
Dir["#{File.expand_path('../../modules', __FILE__)}/*"].each do |f|
|
||||
require_relative f
|
||||
end
|
||||
|
||||
Botish::Botish.new.listen
|
||||
|
|
|
@ -2,51 +2,63 @@
|
|||
|
||||
require 'socket'
|
||||
|
||||
class Botish
|
||||
HOST = 'irc.freenode.net'
|
||||
PORT = 6667
|
||||
USERNAME = 'botish'
|
||||
CHANNEL = '#testnishiki'
|
||||
module Botish
|
||||
class Botish
|
||||
HOST = 'irc.freenode.net'
|
||||
PORT = 6667
|
||||
USERNAME = 'botish'
|
||||
CHANNEL = '#testnishiki'
|
||||
|
||||
def initialize
|
||||
@connection = TCPSocket.open(HOST, PORT)
|
||||
def initialize
|
||||
@connection = TCPSocket.open(HOST, PORT)
|
||||
|
||||
send("NICK #{USERNAME}")
|
||||
send("USER #{USERNAME} localhost * :#{USERNAME}")
|
||||
Kernel.loop do
|
||||
msg = @connection.gets
|
||||
log("<- #{msg}")
|
||||
break if msg.include?('End of /MOTD command.')
|
||||
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
|
||||
forward("JOIN #{CHANNEL}")
|
||||
forward("PRIVMSG #{CHANNEL} :Je suis là :')")
|
||||
end
|
||||
send("JOIN #{CHANNEL}")
|
||||
send("PRIVMSG #{CHANNEL} :Je suis là :')")
|
||||
end
|
||||
|
||||
def listen
|
||||
Kernel.loop do
|
||||
msg = @connection.gets
|
||||
log("<- #{msg}")
|
||||
parse(msg)
|
||||
def listen
|
||||
Kernel.loop do
|
||||
msg = @connection.gets
|
||||
log("<- #{msg}")
|
||||
parse(msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def parse(msg)
|
||||
case msg
|
||||
when /^PING (?<host>.+)/
|
||||
send("PONG #{Regexp.last_match('host')}")
|
||||
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
|
||||
send("PRIVMSG #{CHANNEL} :#{Regexp.last_match('user')}: pong")
|
||||
def parse(msg)
|
||||
case msg
|
||||
when /^PING (?<host>.+)/
|
||||
forward("PONG #{Regexp.last_match('host')}")
|
||||
|
||||
when /^:(?<user>[[:alpha:]]+)([^ ]+)? PRIVMSG #{CHANNEL} :#{USERNAME}: ping/
|
||||
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
|
||||
|
||||
def send(msg)
|
||||
log("-> #{msg}")
|
||||
@connection.puts(msg)
|
||||
end
|
||||
def forward(msg)
|
||||
log("-> #{msg}")
|
||||
@connection.puts(msg)
|
||||
end
|
||||
|
||||
def log(msg)
|
||||
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
||||
def log(msg)
|
||||
puts "#{Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')} #{msg}"
|
||||
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…
Add table
Reference in a new issue