Skip to content

Instantly share code, notes, and snippets.

@aburgd
Last active May 21, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aburgd/fe474df186d0fda4d54c5548815d0ebf to your computer and use it in GitHub Desktop.
Save aburgd/fe474df186d0fda4d54c5548815d0ebf to your computer and use it in GitHub Desktop.
require 'cinch'
require 'securerandom'
class Time
include Cinch::Plugin
match (/(\!time [+-]\d{2}:00)/)
def execute(m, zone)
m.reply 'The time is now #{Time.now.localtime(zone).inspect}.'
end
end
class Randy
include Cinch::Plugin
hook :pre, method: :generate_random_number
def generate_random_number(*)
# Hooks are called in the same thread as the handler and thus
# using thread local variables is possible.
Thread.current[:rand] = SecureRandom.random_number(10_000_000)
end
hook :post, method: :cheer
def cheer(m)
m.reply 'Yay, I successfully ran a command!'
end
match 'rand'
def execute(m)
m.reply 'Random number: ' + Random.rand(10_000_000) #Thread.current[:rand].to_s
end
end
bot = Cinch::Bot.new do
configure do |c|
c.server = 'irc.freenode.net'
c.nick = 'bot_randy'
c.channels = ['##cinchtest']
c.plugins.plugins = [Randy, Time]
end
on :message, '!hi' do |m|
m.reply 'Hi, #{m.user.nick}.'
end
end
bot.start
bot.rb:55: warning: assigned but unused variable - picked
bot.rb:79: warning: assigned but unused variable - picked
/home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/logger/formatted_logger.rb:44: warning: character class has duplicated range: /[^[:print:][:space:]]/
/home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/logger/formatted_logger.rb:44: warning: character class has duplicated range: /[^[:print:][:space:]]/
[2016/05/21 17:46:04.673] !! [on handler] Registering handler with pattern `#<Cinch::Pattern:0x00000000d6a880 @prefix=/^/, @pattern=/!hi/, @suffix=/$/>`, reacting on `message
`
[2016/05/21 17:46:04.673] !! [on handler] Registering handler with pattern `#<Cinch::Pattern:0x00000000d69c78 @prefix=nil, @pattern=/^\!time (.+?)/, @suffix=nil>`, reacting o
n `message`
[2016/05/21 17:46:04.674] !! [plugin] randy: Registering executor with pattern `#<Cinch::Pattern:0x00000000d69200 @prefix=/^!/, @pattern="rand", @suffix=nil>`, reacting on `m
essage`
[2016/05/21 17:46:04.674] !! [on handler] Registering handler with pattern `#<Cinch::Pattern:0x00000000d69200 @prefix=/^!/, @pattern="rand", @suffix=nil>`, reacting on `messa
ge`
/home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:11:in `initialize': no implicit conversion of Cinch::Bot into Integer (TypeError)
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:11:in `new'
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:11:in `register_plugin'
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:16:in `block in register_plugins'
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:16:in `each'
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/plugin_list.rb:16:in `register_plugins'
from /home/nitrous/.rvm/gems/ruby-2.2.1/gems/cinch-2.3.2/lib/cinch/bot.rb:239:in `start'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment