command_bot 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/helper.rb +49 -49
- data/lib/command_bot/bot.rb +120 -117
- data/lib/command_bot/version.rb +6 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0967f34f9d65155da4467d04c8dbcaf5c1772d925a73f6892dab25e257e3440
|
4
|
+
data.tar.gz: 543fc95cc45e431bfaaf0a6c0ae93a40b8afa5239b622982c8a9f3ffd2f2e053
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74a2a881ef1ba216a3be8f0bb24b0b3eef659bb81b38258535db3b41af5f7fb034656d6bfbff0c56e616b75d740ee1d9bc9e1ff391bb22230745dc23c205cbc8
|
7
|
+
data.tar.gz: '08e77136146597ebe9d774a8d143c07c7ae0e318cd725c918ff9e10ff3a88215ee51c906c6db5f01f81d0b7cec1f0b3fb69f6cad85010fcf487d6f64a1eb5f6f'
|
data/examples/helper.rb
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
4
|
-
require 'command_bot'
|
5
|
-
|
6
|
-
PingCommand = CommandBot::Command.new(
|
7
|
-
name: 'ping',
|
8
|
-
aliases: ['p'],
|
9
|
-
data: {
|
10
|
-
description: 'ping-pong',
|
11
|
-
description_long: 'Ping-pong.'
|
12
|
-
}
|
13
|
-
) do |_bot, _arguments, _options,
|
14
|
-
'pong'
|
15
|
-
end
|
16
|
-
|
17
|
-
HelpCommand = CommandBot::Command.new(
|
18
|
-
name: 'help',
|
19
|
-
aliases: ['h'],
|
20
|
-
data: {
|
21
|
-
description: 'show help message',
|
22
|
-
description_long: 'Show help message for specified command. Shows list of ' \
|
23
|
-
'command if atgument is not specified.',
|
24
|
-
another_info: 'you can pass any data here'
|
25
|
-
}
|
26
|
-
) do |bot, arguments, _options,
|
27
|
-
commands = if arguments.empty?
|
28
|
-
bot.commands
|
29
|
-
else
|
30
|
-
bot.commands.select { |e| arguments.include? e.name }
|
31
|
-
end
|
32
|
-
|
33
|
-
help_line = if arguments.empty?
|
34
|
-
proc do |e|
|
35
|
-
"#{e.name} (also known as #{e.aliases.join(', ')}) " \
|
36
|
-
"- #{e.data[:description]}"
|
37
|
-
end
|
38
|
-
else
|
39
|
-
proc do |e|
|
40
|
-
"#{e.name} (also known as #{e.aliases.join(', ')}) " \
|
41
|
-
"- #{e.data[:description]}\n#{e.data[:description_long]}"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
<<~HELP
|
46
|
-
Commands list:
|
47
|
-
#{commands.map(&help_line).join("\n")}
|
48
|
-
HELP
|
49
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
4
|
+
require 'command_bot'
|
5
|
+
|
6
|
+
PingCommand = CommandBot::Command.new(
|
7
|
+
name: 'ping',
|
8
|
+
aliases: ['p'],
|
9
|
+
data: {
|
10
|
+
description: 'ping-pong',
|
11
|
+
description_long: 'Ping-pong.'
|
12
|
+
}
|
13
|
+
) do |_bot, _arguments, _options, _data|
|
14
|
+
'pong'
|
15
|
+
end
|
16
|
+
|
17
|
+
HelpCommand = CommandBot::Command.new(
|
18
|
+
name: 'help',
|
19
|
+
aliases: ['h'],
|
20
|
+
data: {
|
21
|
+
description: 'show help message',
|
22
|
+
description_long: 'Show help message for specified command. Shows list of ' \
|
23
|
+
'command if atgument is not specified.',
|
24
|
+
another_info: 'you can pass any data here'
|
25
|
+
}
|
26
|
+
) do |bot, arguments, _options, _data|
|
27
|
+
commands = if arguments.empty?
|
28
|
+
bot.commands
|
29
|
+
else
|
30
|
+
bot.commands.select { |e| arguments.include? e.name }
|
31
|
+
end
|
32
|
+
|
33
|
+
help_line = if arguments.empty?
|
34
|
+
proc do |e|
|
35
|
+
"#{e.name} (also known as #{e.aliases.join(', ')}) " \
|
36
|
+
"- #{e.data[:description]}"
|
37
|
+
end
|
38
|
+
else
|
39
|
+
proc do |e|
|
40
|
+
"#{e.name} (also known as #{e.aliases.join(', ')}) " \
|
41
|
+
"- #{e.data[:description]}\n#{e.data[:description_long]}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
<<~HELP
|
46
|
+
Commands list:
|
47
|
+
#{commands.map(&help_line).join("\n")}
|
48
|
+
HELP
|
49
|
+
end
|
data/lib/command_bot/bot.rb
CHANGED
@@ -1,117 +1,120 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'logger'
|
4
|
-
|
5
|
-
module CommandBot
|
6
|
-
# Main class.
|
7
|
-
class Bot
|
8
|
-
# Initialize new bot.
|
9
|
-
# @paran identifier [CommandIdentifier] defaults to {CommandIdentifier.default}.
|
10
|
-
# @param logger [Logger]
|
11
|
-
# @param log_level [Logger::Severity]
|
12
|
-
# @param command_not_found [Proc] process to execute when command can't be
|
13
|
-
# identified. Accepts single argument of {CommandCall}. Defaults to returning +nil+.
|
14
|
-
def initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil)
|
15
|
-
@identifier = identifier || CommandIdentifier.default
|
16
|
-
@command_not_found = command_not_found || proc { |_command_call| nil }
|
17
|
-
|
18
|
-
@commands = []
|
19
|
-
|
20
|
-
log_formatter = proc do |severity, datetime, progname, msg|
|
21
|
-
"[#{datetime}] #{severity} - #{progname || object_id}:\t #{msg}\n"
|
22
|
-
end
|
23
|
-
@logger = logger || Logger.new(STDOUT, formatter: log_formatter)
|
24
|
-
@logger.level = log_level
|
25
|
-
end
|
26
|
-
|
27
|
-
# @return [Array<Command>]
|
28
|
-
attr_reader :commands
|
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
|
-
# @return [
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module CommandBot
|
6
|
+
# Main class.
|
7
|
+
class Bot
|
8
|
+
# Initialize new bot.
|
9
|
+
# @paran identifier [CommandIdentifier] defaults to {CommandIdentifier.default}.
|
10
|
+
# @param logger [Logger]
|
11
|
+
# @param log_level [Logger::Severity]
|
12
|
+
# @param command_not_found [Proc] process to execute when command can't be
|
13
|
+
# identified. Accepts single argument of {CommandCall}. Defaults to returning +nil+.
|
14
|
+
def initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil)
|
15
|
+
@identifier = identifier || CommandIdentifier.default
|
16
|
+
@command_not_found = command_not_found || proc { |_command_call| nil }
|
17
|
+
|
18
|
+
@commands = []
|
19
|
+
|
20
|
+
log_formatter = proc do |severity, datetime, progname, msg|
|
21
|
+
"[#{datetime}] #{severity} - #{progname || object_id}:\t #{msg}\n"
|
22
|
+
end
|
23
|
+
@logger = logger || Logger.new(STDOUT, formatter: log_formatter)
|
24
|
+
@logger.level = log_level
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Array<Command>]
|
28
|
+
attr_reader :commands
|
29
|
+
|
30
|
+
# @return [CommandIdentifier]
|
31
|
+
attr_reader :identifier
|
32
|
+
|
33
|
+
# @param name [String]
|
34
|
+
# @return [Command, nil]
|
35
|
+
def find_command(name)
|
36
|
+
logger.debug "Searching for command `#{name}`"
|
37
|
+
commands.find { |c| c.name_matches?(name) }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Add new commands.
|
41
|
+
# @param commands [Command]
|
42
|
+
def add_commands(*commands)
|
43
|
+
logger.debug "Adding #{commands.size} new commands"
|
44
|
+
all_aliases_array = all_aliases
|
45
|
+
commands.each { |c| add_command_unless_alias_is_in_array(c, all_aliases_array) }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Remove commands.
|
49
|
+
# @param commands [Command, String]
|
50
|
+
def remove_commands(*commands)
|
51
|
+
logger.debug "Removing #{commands.size} commands"
|
52
|
+
command_names = commands.map { |c| c.is_a?(String) ? c : c.name }
|
53
|
+
@commands.reject! { |ec| ec.name_matches?(*command_names) }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Handle message.
|
57
|
+
# @param text [String] message text.
|
58
|
+
# @param data [Hash] additional data which should be passed to handler procedure.
|
59
|
+
# @return [void, nil] result depends on handler of command.
|
60
|
+
def handle(text, data = {})
|
61
|
+
logger.debug "Handling text: `#{text}` with additional data: #{data}"
|
62
|
+
command_call = identify_command_call(text, data)
|
63
|
+
return nil if command_call.nil? # Not a command call, so not handling.
|
64
|
+
|
65
|
+
handle_command_call(command_call)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Hadnle {CommandCall}.
|
69
|
+
# @param command_call [CommandCall]
|
70
|
+
# @return [void, nil] result depends on handler of command.
|
71
|
+
def handle_command_call(command_call)
|
72
|
+
logger.debug "Hadnling command call: #{command_call}"
|
73
|
+
if command_call.command
|
74
|
+
execute_command_call(command_call)
|
75
|
+
else
|
76
|
+
command_not_found(command_call)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# @return [Logger]
|
83
|
+
attr_reader :logger
|
84
|
+
|
85
|
+
# @return [Array<String>]
|
86
|
+
def all_aliases
|
87
|
+
re = @commands.map(&:all_aliases)
|
88
|
+
re.flatten!
|
89
|
+
re
|
90
|
+
end
|
91
|
+
|
92
|
+
# @note modifies +all_aliases_array+
|
93
|
+
def add_command_unless_alias_is_in_array(command, all_aliases_array)
|
94
|
+
command_aliases = command.all_aliases
|
95
|
+
intersection = all_aliases_array & command_aliases
|
96
|
+
if intersection.empty?
|
97
|
+
@commands << command
|
98
|
+
all_aliases_array.concat(command_aliases)
|
99
|
+
else
|
100
|
+
logger.warn "Command #{command.name} will not be added due to name intersection!"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# @return [CommandCall, nil]
|
105
|
+
def identify_command_call(text, data)
|
106
|
+
@identifier.call(self, text, data)
|
107
|
+
end
|
108
|
+
|
109
|
+
# @return [void]
|
110
|
+
def execute_command_call(command_call)
|
111
|
+
command_call.execute
|
112
|
+
# NOTE: it is possible to execute it from here, but IHMO command_call should
|
113
|
+
# be able to execute itself.
|
114
|
+
end
|
115
|
+
|
116
|
+
def command_not_found(command_call)
|
117
|
+
@command_not_found.call(command_call)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/lib/command_bot/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CommandBot
|
4
|
-
# Library version.
|
5
|
-
VERSION = '1.0.
|
6
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CommandBot
|
4
|
+
# Library version.
|
5
|
+
VERSION = '1.0.1'
|
6
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fizvlad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|