discorb-view 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/discorb-view.gemspec +1 -1
- data/examples/menu.rb +2 -4
- data/lib/discorb/view/extension.rb +21 -20
- data/lib/discorb/view/version.rb +1 -1
- data/lib/discorb/view/view.rb +76 -74
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4756a69740ed5a64f1ea13104a3ee0980a5f95364e93a4150ebf91559e66552
|
4
|
+
data.tar.gz: f3de79402b1aa23d4215e84d6988aacd754304c9c80958d72a60beed92158530
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42b22a58f77541d9c0036402a7a33fc8e53aa1af08b3d2492bb4f8785ef9caffba65d30b1e8e4f62ecc8400feeb6aeea3c2f8d83ab8149772da7f037ac9c8eae
|
7
|
+
data.tar.gz: dc8248590703c3bf09854d61ac0851d70216d95c228dd82a53bce6a02b57fa0c3828795a901e677ce836f2b8fc3b03a7a3088cc4caa31c4b588190a1605e05b7
|
data/discorb-view.gemspec
CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.require_paths = ["lib"]
|
30
30
|
|
31
31
|
# Uncomment to register a new dependency of your gem
|
32
|
-
spec.add_dependency "discorb", "~> 0.
|
32
|
+
spec.add_dependency "discorb", "~> 0.10.3"
|
33
33
|
|
34
34
|
# For more information and examples about making a new gem, checkout our
|
35
35
|
# guide at: https://bundler.io/guides/creating_gem.html
|
data/examples/menu.rb
CHANGED
@@ -3,11 +3,9 @@ require "discorb/view"
|
|
3
3
|
|
4
4
|
client = Discorb::Client.new
|
5
5
|
|
6
|
-
client.
|
7
|
-
|
8
|
-
class MyMenu
|
9
|
-
extend Discorb::View::Base
|
6
|
+
client.load_extension Discorb::View::Extension
|
10
7
|
|
8
|
+
class MyMenu < Discorb::View::Base
|
11
9
|
@@texts = ["A", "B"]
|
12
10
|
|
13
11
|
def initialize
|
@@ -3,11 +3,7 @@ module Discorb::View
|
|
3
3
|
# An extension for using discorb-view.
|
4
4
|
# @note Client must extend this class to use discorb-view.
|
5
5
|
#
|
6
|
-
|
7
|
-
attr_accessor :views
|
8
|
-
|
9
|
-
extend Discorb::Extension
|
10
|
-
|
6
|
+
class Extension < Discorb::Extension
|
11
7
|
event :button_click do |interaction|
|
12
8
|
handle_interaction(interaction)
|
13
9
|
end
|
@@ -17,25 +13,30 @@ module Discorb::View
|
|
17
13
|
end
|
18
14
|
|
19
15
|
# @private
|
20
|
-
def self.
|
16
|
+
def self.inherited(base)
|
21
17
|
base.views = {}
|
22
18
|
end
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
20
|
+
# @private
|
21
|
+
def handle_interaction(interaction)
|
22
|
+
unless view = @client.views[interaction.message.id.to_s]
|
23
|
+
@client.log.warn "View: No handler for #{interaction.message.id.to_s}"
|
24
|
+
return
|
25
|
+
end
|
26
|
+
handler = view.class.components[interaction.custom_id.to_sym]
|
27
|
+
@client.log.debug "View: Handling #{interaction.custom_id} in #{interaction.message.id}"
|
28
|
+
view.interaction = interaction
|
29
|
+
update = view.instance_exec(interaction, &handler.block)
|
30
|
+
return unless update
|
31
|
+
@client.log.debug "View: Updating view #{interaction.message.id}"
|
32
|
+
view.render
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.loaded(client)
|
36
|
+
class << client
|
37
|
+
attr_accessor :views
|
38
38
|
end
|
39
|
+
client.views = {}
|
39
40
|
end
|
40
41
|
end
|
41
42
|
end
|
data/lib/discorb/view/version.rb
CHANGED
data/lib/discorb/view/view.rb
CHANGED
@@ -27,86 +27,87 @@ module Discorb::View
|
|
27
27
|
# @note You should not use this class directly.
|
28
28
|
# @abstract
|
29
29
|
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
class Base
|
31
|
+
class << self
|
32
|
+
# @return [Hash{Symbol => Discorb::Component}] The components.
|
33
|
+
attr_accessor :components
|
34
|
+
# @return [Array<ViewHandler>] The view handlers.
|
35
|
+
attr_accessor :views
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
37
|
+
# @private
|
38
|
+
def inherited(base)
|
39
|
+
base.prepend(Discorb::View::Base::Prepend)
|
40
|
+
base.components = {}
|
41
|
+
base.views = []
|
42
|
+
end
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
44
|
+
#
|
45
|
+
# Adds button component to the view.
|
46
|
+
#
|
47
|
+
# @param [Symbol] id The id of the button.
|
48
|
+
# @param [String] label The label of the button.
|
49
|
+
# @param [:primary, :secondary, :success, :danger] style The style of the button.
|
50
|
+
# @param [Discorb::Emoji, nil] emoji The emoji of the button.
|
51
|
+
# @yield The block to execute when the button is clicked.
|
52
|
+
# @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
|
53
|
+
#
|
54
|
+
def button(id, label, style = :secondary, emoji: nil, &block)
|
55
|
+
raise ArgumentError, "block required" unless block_given?
|
56
|
+
button = Discorb::Button.new(label, style, emoji: emoji, custom_id: id)
|
57
|
+
@components[id] = ComponentHandler.new(button, block)
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
60
|
+
#
|
61
|
+
# Adds select menu component to the view.
|
62
|
+
#
|
63
|
+
# @param [Symbol] id The id of the button.
|
64
|
+
# @param [String] label The label of the button.
|
65
|
+
# @param [String, nil] placeholder The placeholder of the select menu.
|
66
|
+
# @param [Integer, nil] min_length The minimum length of the select menu.
|
67
|
+
# @param [Integer, nil] max_length The max length of the select menu.
|
68
|
+
# @yield The block to execute when the menu is changed.
|
69
|
+
# @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
|
70
|
+
#
|
71
|
+
def select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil, &block)
|
72
|
+
raise ArgumentError, "block required" unless block_given?
|
73
|
+
options.map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) }
|
74
|
+
menu = Discorb::SelectMenu.new(id, options, placeholder: placeholder, min_values: min_values, max_values: max_values)
|
75
|
+
@components[id] = ComponentHandler.new(menu, block)
|
76
|
+
end
|
76
77
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
78
|
+
#
|
79
|
+
# Add view handler to the view.
|
80
|
+
#
|
81
|
+
# @param [Proc, nil] check The check of the view handler.
|
82
|
+
# The view handler will be executed when the check returns true, or check is nil.
|
83
|
+
# @yield The block to execute when the view handler is executed.
|
84
|
+
# @yieldparam [Discorb::View::Base::Prepend::Result] result The result of the view.
|
85
|
+
#
|
86
|
+
# @note There must be one handler with no check.
|
87
|
+
#
|
88
|
+
def view(check = nil, &block)
|
89
|
+
raise ArgumentError, "block required" unless block_given?
|
90
|
+
@views.insert(0, ViewHandler.new(check, block))
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
93
|
+
#
|
94
|
+
# Starts the view.
|
95
|
+
#
|
96
|
+
# @param [Discorb::Messageable] channel The channel to send the message to.
|
97
|
+
#
|
98
|
+
def start(channel, ...)
|
99
|
+
client = channel.instance_variable_get(:@client)
|
100
|
+
if @views.empty?
|
101
|
+
raise "No views defined"
|
102
|
+
elsif not @views.any? { |v| v.check.nil? }
|
103
|
+
raise "No fallback view defined"
|
104
|
+
elsif @views.filter { |v| v.check.nil? }.count > 1
|
105
|
+
raise "Multiple fallback views defined"
|
106
|
+
end
|
107
|
+
view = new(client, channel, ...)
|
108
|
+
view.start
|
105
109
|
end
|
106
|
-
view = new(client, channel, ...)
|
107
|
-
view.start
|
108
110
|
end
|
109
|
-
|
110
111
|
#
|
111
112
|
# Modules for the prepend.
|
112
113
|
#
|
@@ -125,6 +126,7 @@ module Discorb::View
|
|
125
126
|
@channel = channel
|
126
127
|
@message_id = nil
|
127
128
|
@stopped = false
|
129
|
+
@components = self.class.components.dup.map { |id, component| [id, ComponentHandler.new(component.object.dup, component.block)] }.to_h
|
128
130
|
@result = Result.new(nil, [], [])
|
129
131
|
super(...)
|
130
132
|
end
|
@@ -158,7 +160,7 @@ module Discorb::View
|
|
158
160
|
components = @result.components.map do |c|
|
159
161
|
case c
|
160
162
|
when Symbol
|
161
|
-
|
163
|
+
@components[c]&.object or raise ArgumentError "Unknown component ID #{c}"
|
162
164
|
when Button
|
163
165
|
c
|
164
166
|
else
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discorb-view
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sevenc-nanashi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: discorb
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.10.3
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.10.3
|
27
27
|
description:
|
28
28
|
email:
|
29
29
|
- sevenc-nanashi@sevenbot.jp
|