|
Revision 763, 1.1 kB
(checked in by vkgtaro, 5 months ago)
|
- コンフィグにシンボルを使用するように変更
- lazy_san にあわせるように変更
- notify_observers で失敗しても言いようにエラーハンドリング追加
|
| Line | |
|---|
| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
# encoding: utf-8 |
|---|
| 3 |
|
|---|
| 4 |
$LOAD_PATH << "lib" |
|---|
| 5 |
|
|---|
| 6 |
require 'rubygems' |
|---|
| 7 |
require 'net/irc' |
|---|
| 8 |
require 'wedata' |
|---|
| 9 |
require 'observer' |
|---|
| 10 |
require 'yaml' |
|---|
| 11 |
require 'lazysan' |
|---|
| 12 |
|
|---|
| 13 |
require 'pp' |
|---|
| 14 |
|
|---|
| 15 |
class ThanksBot < Net::IRC::Client |
|---|
| 16 |
include Observable |
|---|
| 17 |
|
|---|
| 18 |
def initialize(config) |
|---|
| 19 |
@channel = config[:irc][:opts][:channel] |
|---|
| 20 |
|
|---|
| 21 |
super( |
|---|
| 22 |
config[:irc][:host], |
|---|
| 23 |
config[:irc][:port], |
|---|
| 24 |
config[:irc][:opts] |
|---|
| 25 |
) |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def on_rpl_welcome(m) |
|---|
| 29 |
post JOIN, @channel |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def on_privmsg(m) |
|---|
| 33 |
if match = m[1].match(/^thanksbot:\s*(.*)/) |
|---|
| 34 |
changed |
|---|
| 35 |
begin |
|---|
| 36 |
notify_observers(match[1]) |
|---|
| 37 |
post NOTICE, m[0], "#{m.prefix.nick}: #{match[1]}" |
|---|
| 38 |
rescue |
|---|
| 39 |
post NOTICE, m[0], "ごめん、失敗したかも" |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|
| 42 |
end |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
class ThanksDB < WedataDatabase |
|---|
| 46 |
def update(message) |
|---|
| 47 |
create_item('thanksbot', { 'message' => message }) |
|---|
| 48 |
end |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
config = YAML.load_file('thanksbot.yaml') |
|---|
| 52 |
|
|---|
| 53 |
thanksbot = ThanksBot.new(config) |
|---|
| 54 |
|
|---|
| 55 |
thanksdb = ThanksDB.new('thanks', config[:wedata_api]) |
|---|
| 56 |
lazysan = LazySan::Twitter.new(config[:twitter][:user], config[:twitter][:pass]) |
|---|
| 57 |
|
|---|
| 58 |
thanksbot.add_observer(thanksdb) |
|---|
| 59 |
thanksbot.add_observer(lazysan) |
|---|
| 60 |
thanksbot.start |
|---|
| 61 |
|
|---|