cinchrb / cinch

The IRC Bot Building Framework
http://www.rubydoc.info/gems/cinch
MIT License
1k stars 180 forks source link

Cinch::Helper can't get instances of Channel and User objects #238

Closed noraj closed 7 years ago

noraj commented 7 years ago

https://github.com/cinchrb/cinch/blob/master/docs/common_tasks.md told me that I can use User('user').send("Hello!")

but I get an error

/home/user/.gem/ruby/2.3.0/gems/cinch-2.3.3/lib/cinch/user_list.rb:42:in `find_ensured': undefined method `isupport' for nil:NilClass (NoMethodError)
    from /home/user/.gem/ruby/2.3.0/gems/cinch-2.3.3/lib/cinch/helpers.rb:57:in `User'
    from irc.rb:10:in `block in <main>'
    from /home/user/.gem/ruby/2.3.0/gems/cinch-2.3.3/lib/cinch/bot.rb:355:in `instance_eval'
    from /home/user/.gem/ruby/2.3.0/gems/cinch-2.3.3/lib/cinch/bot.rb:355:in `initialize'
    from irc.rb:3:in `new'
    from irc.rb:3:in `<main>

Used with this code :

require 'cinch'

bot = Cinch::Bot.new do
  configure do |c|
    c.server   = "irc.server.org"
    c.nick     = "bot_name"
    c.channels = ["#channel"]
  end

  User('username').send("!command")

  on :message, /^regex)/ do |m,challenge|
    # some stuff
    m.reply "!command answer}"
  end
end

bot.start

find_ensured does the same error with Channel("#channel").

dominikh commented 7 years ago

User('username').send("!command") executes in the Bot.new block, i.e. when you load the file, not when the bot is connected. As there is no connection yet, this will fail in many ways.

noraj commented 7 years ago

all I put after bot.start is not executed.

For example

require 'cinch'

bot = Cinch::Bot.new do
  configure do |c|
    c.server   = "irc.server.org"
    c.nick     = "bot_name"
    c.channels = ["#channel"]
  end

  on :message, /^regex)/ do |m,challenge|
    # some stuff
    m.reply "!command answer}"
  end
end

bot.start
User('username').send("!command")
# all stuff here, cinch or ruby command, won't be executed
Defman21 commented 7 years ago

Because bot.start is thread-locking (its waiting for incoming messages). I'd suggest to try Cinch plugins or create Threads

noraj commented 7 years ago

That would have been great if Cinch has this feature out of the box. I don't understand that Cinch can reply to message with on :message but can't directly send message to a user.

Plugin doc is empty since 2012. ANd this example doesn't help me to understand how to send a message (not just replying) with a plugin.

Create a thread is more a workaround than a solution.

noraj commented 7 years ago

I find a way. For example waiting for a special private message or on :connect:

on :private, /^send_target/ do
    candy  = Cinch::Target.new("target-user", bot)
    candy.send("message")
  end

This will be nice to write someting like this in the doc.