Module: ActiveMatrix::Bot::BuiltinCommands::ClassMethods

Defined in:
lib/active_matrix/bot/builtin_commands.rb

Instance Method Summary collapse

Instance Method Details

#register_builtin_commandsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/active_matrix/bot/builtin_commands.rb', line 19

def register_builtin_commands
  # Ping command - connectivity test
  command(
    :ping,
    desc: 'Test bot connectivity and response time'
  ) do
    start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

    response_parts = [
      '**Pong!**',
      '',
      'Bot is online and responding',
      "Server time: #{Time.current.strftime('%Y-%m-%d %H:%M:%S %Z')}"
    ]

    end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    response_time_ms = ((end_time - start_time) * 1000).round(2)
    response_parts.insert(2, "Response time: #{response_time_ms}ms")

    room.send_notice(response_parts.join("\n"))
  end

  # Version command - show bot version
  command(
    :version,
    desc: 'Show bot version information'
  ) do
    response_parts = [
      '**Version Information**',
      '',
      "ActiveMatrix: #{ActiveMatrix::VERSION}",
      "Ruby: #{RUBY_VERSION}",
      "Platform: #{RUBY_PLATFORM}"
    ]

    # Add application version if available
    response_parts << "Application: #{Rails.application.version}" if defined?(Rails) && Rails.application.respond_to?(:version)

    room.send_notice(response_parts.join("\n"))
  end

  # Status command - show bot status
  command(
    :status,
    desc: 'Show bot status and health information'
  ) do
    response_parts = [
      '**Bot Status**',
      '',
      'State: Online',
      "Uptime: #{format_uptime}",
      "User ID: #{client.mxid}",
      "Homeserver: #{client.api.homeserver}"
    ]

    # Add room count if available
    if client.respond_to?(:rooms)
      room_count = client.rooms.size
      response_parts << "Joined rooms: #{room_count}"
    end

    # Add metrics if available
    if defined?(ActiveMatrix::Metrics)
      metrics = ActiveMatrix::Metrics.instance.get_health_summary
      if metrics[:total_operations].positive?
        response_parts += [
          '',
          '**Metrics**',
          "Total operations: #{metrics[:total_operations]}",
          "Success rate: #{metrics[:overall_success_rate]}%"
        ]
      end
    end

    room.send_notice(response_parts.join("\n"))
  end

  # Time command - show current time
  command(
    :time,
    desc: 'Show current time in specified timezone',
    notes: 'Usage: !time [TIMEZONE]. Examples: !time UTC, !time America/New_York'
  ) do |timezone = nil|
    time = if timezone && defined?(ActiveSupport::TimeZone)
             tz = ActiveSupport::TimeZone[timezone]
             if tz
               tz.now
             else
               room.send_notice("Unknown timezone: #{timezone}. Using server time.")
               Time.current
             end
           else
             Time.current
           end

    formatted = time.strftime('%Y-%m-%d %H:%M:%S %Z')
    unix_timestamp = time.to_i

    response_parts = [
      '**Current Time**',
      '',
      formatted,
      "Unix timestamp: #{unix_timestamp}"
    ]

    room.send_notice(response_parts.join("\n"))
  end

  # Echo command - echo back message
  command(
    :echo,
    desc: 'Echo back the provided message'
  ) do |message = nil|
    if message.nil? || message.strip.empty?
      room.send_notice('Nothing to echo. Usage: !echo <message>')
    else
      room.send_text(message)
    end
  end

  # Rooms command - list joined rooms (admin only)
  command(
    :rooms,
    desc: 'List joined rooms',
    only: :admin
  ) do
    rooms_list = client.rooms.map do |r|
      name = r.display_name || r.id
      "- #{name}"
    end

    if rooms_list.empty?
      room.send_notice('Not joined to any rooms.')
    else
      response = [
        "**Joined Rooms** (#{rooms_list.size})",
        '',
        *rooms_list.first(20)
      ]

      response << "... and #{rooms_list.size - 20} more" if rooms_list.size > 20

      room.send_notice(response.join("\n"))
    end
  end
end