Module: Rubydium::Mixins::CommandMacros::ClassMethods

Defined in:
lib/rubydium/mixins/command_macros.rb

Instance Method Summary collapse

Instance Method Details

#help_messageObject



41
42
43
44
45
# File 'lib/rubydium/mixins/command_macros.rb', line 41

def help_message
  registered_commands.map { |command, info|
    "#{command} - #{info[:description]}"
  }.join("\n")
end

#inherited(subclass) ⇒ Object

Makes these actions work in inheritance as you would expect, i. e.:

class ParentBot < Rubydium::Bot

on_every_message :foo

end

class ChildBot < ParentBot

on_every_message :bar

end

class AnotherChildBot < ParentBot

on_every_message :baz

end

ParentBot.registered_on_every_message.map { _1 } # => [:foo] ChildBot.registered_on_every_message.map { _1 } # => [:foo, :bar] AnotherChildBot.registered_on_every_message.map { _1 } # => [:foo, :baz]



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubydium/mixins/command_macros.rb', line 30

def inherited(subclass)
  %w[
    @registered_on_mention
    @registered_on_every_message
    @registered_commands
  ].each do |var_name|
    current_value_copy = instance_variable_get(var_name).dup
    subclass.instance_variable_set(var_name, current_value_copy)
  end
end

#on_command(command, method_name = nil, aliases: [], description: nil, ignore_forwarded: true, &block) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubydium/mixins/command_macros.rb', line 77

def on_command(command, method_name=nil, aliases: [], description: nil, ignore_forwarded: true, &block)
  @registered_commands ||= {}
  action = (method_name || block)
  raise ArgumentError, "Provide either method name or a block" unless action

  parameters = {
    action: action,
    description: description,
    ignore_forwarded: ignore_forwarded
  }

  @registered_commands.merge!(
    [command, *aliases].map { |comm|
      [comm, parameters]
    }.to_h
  )
end

#on_every_message(method_name = nil, ignore_forwarded: true, &block) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
# File 'lib/rubydium/mixins/command_macros.rb', line 62

def on_every_message(method_name=nil, ignore_forwarded: true, &block)
  @registered_on_every_message ||= []
  action = (method_name || block)
  raise ArgumentError, "Provide either method name or a block" unless action

  @registered_on_every_message << {
    action: action,
    ignore_forwarded: ignore_forwarded
  }
end

#on_mention(method_name = nil, ignore_forwarded: true, &block) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
# File 'lib/rubydium/mixins/command_macros.rb', line 47

def on_mention(method_name=nil, ignore_forwarded: true, &block)
  @registered_on_mention ||= []
  action = (method_name || block)
  raise ArgumentError, "Provide either method name or a block" unless action

  @registered_on_mention << {
    action: action,
    ignore_forwarded: ignore_forwarded
  }
end

#registered_commandsObject



95
96
97
# File 'lib/rubydium/mixins/command_macros.rb', line 95

def registered_commands
  @registered_commands ||= {}
end

#registered_on_every_messageObject



73
74
75
# File 'lib/rubydium/mixins/command_macros.rb', line 73

def registered_on_every_message
  @registered_on_every_message ||= []
end

#registered_on_mentionObject



58
59
60
# File 'lib/rubydium/mixins/command_macros.rb', line 58

def registered_on_mention
  @registered_on_mention ||= []
end