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)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rubydium/mixins/command_macros.rb', line 101

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].to_h do |comm|
      [comm, parameters]
    end
  )
end

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

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
# File 'lib/rubydium/mixins/command_macros.rb', line 86

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)


71
72
73
74
75
76
77
78
79
80
# File 'lib/rubydium/mixins/command_macros.rb', line 71

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

#on_shutdown(method_name = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
# File 'lib/rubydium/mixins/command_macros.rb', line 59

def on_shutdown(method_name=nil, &block)
  @registered_on_shutdown ||= []
  action = method_name || block
  raise ArgumentError, 'Provide either method name or a block' unless action

  @registered_on_shutdown << action
end

#on_startup(method_name = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


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

def on_startup(method_name=nil, &block)
  @registered_on_startup ||= []
  action = method_name || block
  raise ArgumentError, 'Provide either method name or a block' unless action

  @registered_on_startup << action
end

#registered_commandsObject



120
121
122
# File 'lib/rubydium/mixins/command_macros.rb', line 120

def registered_commands
  @registered_commands ||= {}
end

#registered_on_every_messageObject



97
98
99
# File 'lib/rubydium/mixins/command_macros.rb', line 97

def registered_on_every_message
  @registered_on_every_message ||= []
end

#registered_on_mentionObject



82
83
84
# File 'lib/rubydium/mixins/command_macros.rb', line 82

def registered_on_mention
  @registered_on_mention ||= []
end

#registered_on_shutdownObject



67
68
69
# File 'lib/rubydium/mixins/command_macros.rb', line 67

def registered_on_shutdown
  @registered_on_shutdown ||= []
end

#registered_on_startupObject



55
56
57
# File 'lib/rubydium/mixins/command_macros.rb', line 55

def registered_on_startup
  @registered_on_startup ||= []
end