discorb-view 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 630986b3d66bbcd57a098f84ca0d7baed2b7cf11100b6464e8d66b4eb6fbd08f
4
+ data.tar.gz: 8f830a89a62fe4a0acd98a54940ae830df2c3735061a00ba05d3408a66752a5a
5
+ SHA512:
6
+ metadata.gz: 7d8259899de9b82de7ec2a58178adcdaf2097079f7feb232f39e98718aa460e27f80831862ce0726e214b0ef42e2a28b43e1f91e3eab24cabe79dcf17caa4f17
7
+ data.tar.gz: 373f1ec214566f874243870bb037eb8bfb28cc292dbeba7d2497d79e2068f3c0f62e572bfa21211460b83cef255e3627220cf784fd298a7620bc4020f272af00
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-28
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in discorb-view.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 sevenc-nanashi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Discorb::View
2
+
3
+ A wrapper of discorb's interaction. This allows you to manage buttons with callbacks.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'discorb-view'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install discorb-view
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ client.extend Discorb::View::Extension # Add the extension to your client
25
+
26
+ class MyMenu
27
+ extend Discorb::View::Base
28
+
29
+ @@texts = ["A", "B"]
30
+
31
+ def initialize
32
+ @page = 0
33
+ end
34
+
35
+ button :left, "<", :primary do |interaction| # Define a button
36
+ @page -= 1
37
+ end
38
+
39
+ button :right, ">", :primary do |interaction|
40
+ @page += 1
41
+ end
42
+
43
+ button :quit, "Quit", :danger do |interaction|
44
+ stop! # Stop the view
45
+ end
46
+
47
+ view ->(interaction) { !((0...@@texts.length).include?(@page)) } do |result| # Define a view that will be shown when the page is out of range
48
+ result.content = "Out of range: Page #{@page + 1}"
49
+ end
50
+
51
+ view do |result| # Define a view that will be shown when the page is in range
52
+ result.content = @@texts[@page % @@texts.length]
53
+ result.embeds = []
54
+ result.components = [:left, :right, :quit]
55
+ end
56
+ end
57
+
58
+ class MyPager
59
+ extend Discorb::View::Base
60
+
61
+ @@pages = [
62
+ "Page 1 Content",
63
+ "Page 2 Content",
64
+ "Page 3 Content",
65
+ "Page 4 Content",
66
+ ]
67
+
68
+ def initialize
69
+ @page = 0
70
+ end
71
+
72
+ select_menu :page, [["Page 1", "1"], ["Page 2", "2"], ["Page 3", "3"], ["Page 4", "4"]], "Page" do |interaction|
73
+ @page = interaction.value.to_i - 1
74
+ end
75
+
76
+ button :quit, "Quit", :danger do |interaction|
77
+ stop!
78
+ end
79
+
80
+ view do |result|
81
+ result.content = @@pages[@page]
82
+ result.embeds = []
83
+ result.components = [:page, :quit]
84
+ end
85
+ end
86
+
87
+ client.on :message do |message|
88
+ next if message.author.bot?
89
+
90
+ MyMenu.start(message.channel) if message.content.downcase == "menu"
91
+
92
+ MyPager.start(message.channel) if message.content.downcase == "pager"
93
+ end
94
+
95
+ ```
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
100
+
101
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
102
+
103
+ ## Contributing
104
+
105
+ Bug reports and pull requests are welcome on GitHub at https://github.com/discorb-lib/discorb-view.
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "discorb/view"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/discorb/view/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "discorb-view"
7
+ spec.version = Discorb::View::VERSION
8
+ spec.authors = ["sevenc-nanashi"]
9
+ spec.email = ["sevenc-nanashi@sevenbot.jp"]
10
+
11
+ spec.summary = "View for Discorb"
12
+ spec.homepage = "https://github.com/discorb-lib/discorb-view"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/discorb-lib/discorb-view"
20
+ spec.metadata["changelog_uri"] = "https://github.com/discorb-lib/discorb-view/blob/main/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "discorb", "~> 0.9.6"
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
data/examples/menu.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "discorb"
2
+ require "discorb/view"
3
+
4
+ client = Discorb::Client.new
5
+
6
+ client.extend Discorb::View::Extension
7
+
8
+ class MyMenu
9
+ extend Discorb::View::Base
10
+
11
+ @@texts = ["A", "B"]
12
+
13
+ def initialize
14
+ @page = 0
15
+ end
16
+
17
+ button :left, "<", :primary do |interaction|
18
+ @page -= 1
19
+ end
20
+
21
+ button :right, ">", :primary do |interaction|
22
+ @page += 1
23
+ end
24
+
25
+ button :quit, "Quit", :danger do |interaction|
26
+ stop!
27
+ end
28
+
29
+ view ->(interaction) { !((0...@@texts.length).include?(@page)) } do |result|
30
+ result.content = "Out of range: Page #{@page + 1}"
31
+ end
32
+
33
+ view do |result|
34
+ result.content = @@texts[@page % @@texts.length]
35
+ result.embeds = []
36
+ result.components = [:left, :right, :quit]
37
+ end
38
+ end
39
+
40
+ client.once :standby do
41
+ puts "Ready!"
42
+ end
43
+
44
+ client.on :message do |message|
45
+ next unless message.content == "menu"
46
+
47
+ MyMenu.start(message.channel)
48
+ end
49
+
50
+ client.run ENV["DISCORD_BOT_TOKEN"]
data/examples/pager.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "discorb"
2
+ require "discorb/view"
3
+
4
+ client = Discorb::Client.new
5
+
6
+ client.extend Discorb::View::Extension
7
+
8
+ class MyPager
9
+ extend Discorb::View::Base
10
+
11
+ @@pages = [
12
+ "Page 1 Content",
13
+ "Page 2 Content",
14
+ "Page 3 Content",
15
+ "Page 4 Content",
16
+ ]
17
+
18
+ def initialize
19
+ @page = 0
20
+ end
21
+
22
+ select_menu :page, [["Page 1", "1"], ["Page 2", "2"], ["Page 3", "3"], ["Page 4", "4"]], "Page" do |interaction|
23
+ @page = interaction.value.to_i - 1
24
+ end
25
+
26
+ button :quit, "Quit", :danger do |interaction|
27
+ stop!
28
+ end
29
+
30
+ view do |result|
31
+ result.content = @@pages[@page]
32
+ result.embeds = []
33
+ result.components = [:page, :quit]
34
+ end
35
+ end
36
+
37
+ client.once :standby do
38
+ puts "Ready!"
39
+ end
40
+
41
+ client.on :message do |message|
42
+ next unless message.content == "menu"
43
+
44
+ MyPager.start(message.channel)
45
+ end
46
+
47
+ client.run ENV["DISCORD_BOT_TOKEN"]
@@ -0,0 +1,41 @@
1
+ module Discorb::View
2
+ #
3
+ # An extension for using discorb-view.
4
+ # @note Client must extend this class to use discorb-view.
5
+ #
6
+ module Extension
7
+ attr_accessor :views
8
+
9
+ extend Discorb::Extension
10
+
11
+ event :button_click do |interaction|
12
+ handle_interaction(interaction)
13
+ end
14
+
15
+ event :select_menu_select do |interaction|
16
+ handle_interaction(interaction)
17
+ end
18
+
19
+ # @private
20
+ def self.extended(base)
21
+ base.views = {}
22
+ end
23
+
24
+ class << self
25
+ # @private
26
+ def handle_interaction(interaction)
27
+ unless view = @client.views[interaction.message.id.to_s]
28
+ @client.log.warn "View: No handler for #{interaction.message.id.to_s}"
29
+ return
30
+ end
31
+ handler = view.class.components[interaction.custom_id.to_sym]
32
+ @client.log.debug "View: Handling #{interaction.custom_id} in #{interaction.message.id}"
33
+ view.interaction = interaction
34
+ update = view.instance_exec(interaction, &handler.block)
35
+ return unless update
36
+ @client.log.debug "View: Updating view #{interaction.message.id}"
37
+ view.render
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ module View
5
+ # @return [String] the version of the gem
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
@@ -0,0 +1,191 @@
1
+ require "securerandom"
2
+
3
+ module Discorb::View
4
+ #
5
+ # Handler for the components.
6
+ #
7
+ class ComponentHandler < Struct.new(:object, :block)
8
+ #
9
+ # Redirects method calls to the object.
10
+ #
11
+ def method_missing(method, ...)
12
+ object.send(method, ...)
13
+ end
14
+
15
+ def respond_to_missing?(method, include_private = false)
16
+ object.respond_to?(method, include_private)
17
+ end
18
+ end
19
+
20
+ #
21
+ # Handles the rendering of the components.
22
+ #
23
+ class ViewHandler < Struct.new(:check, :block); end
24
+
25
+ #
26
+ # Base class for the view.
27
+ # @note You should not use this class directly.
28
+ # @abstract
29
+ #
30
+ module Base
31
+ # @return [Hash{Symbol => Discorb::Component}] The components.
32
+ attr_accessor :components
33
+ # @return [Array<ViewHandler>] The view handlers.
34
+ attr_accessor :views
35
+
36
+ # @private
37
+ def self.extended(base)
38
+ base.prepend(Discorb::View::Base::Prepend)
39
+ base.components = {}
40
+ base.views = []
41
+ end
42
+
43
+ #
44
+ # Adds button component to the view.
45
+ #
46
+ # @param [Symbol] id The id of the button.
47
+ # @param [String] label The label of the button.
48
+ # @param [:primary, :secondary, :success, :danger] style The style of the button.
49
+ # @param [Discorb::Emoji, nil] emoji The emoji of the button.
50
+ # @yield The block to execute when the button is clicked.
51
+ # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
52
+ #
53
+ def button(id, label, style = :secondary, emoji: nil, &block)
54
+ raise ArgumentError, "block required" unless block_given?
55
+ button = Discorb::Button.new(label, style, emoji: emoji, custom_id: id)
56
+ @components[id] = ComponentHandler.new(button, block)
57
+ end
58
+
59
+ #
60
+ # Adds select menu component to the view.
61
+ #
62
+ # @param [Symbol] id The id of the button.
63
+ # @param [String] label The label of the button.
64
+ # @param [String, nil] placeholder The placeholder of the select menu.
65
+ # @param [Integer, nil] min_length The minimum length of the select menu.
66
+ # @param [Integer, nil] max_length The max length of the select menu.
67
+ # @yield The block to execute when the menu is changed.
68
+ # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
69
+ #
70
+ def select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil, &block)
71
+ raise ArgumentError, "block required" unless block_given?
72
+ options.map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) }
73
+ menu = Discorb::SelectMenu.new(id, options, placeholder: placeholder, min_values: min_values, max_values: max_values)
74
+ @components[id] = ComponentHandler.new(menu, block)
75
+ end
76
+
77
+ #
78
+ # Add view handler to the view.
79
+ #
80
+ # @param [Proc, nil] check The check of the view handler.
81
+ # The view handler will be executed when the check returns true, or check is nil.
82
+ # @yield The block to execute when the view handler is executed.
83
+ # @yieldparam [Discorb::View::Base::Prepend::Result] result The result of the view.
84
+ #
85
+ # @note There must be one handler with no check.
86
+ #
87
+ def view(check = nil, &block)
88
+ raise ArgumentError, "block required" unless block_given?
89
+ @views.insert(0, ViewHandler.new(check, block))
90
+ end
91
+
92
+ #
93
+ # Starts the view.
94
+ #
95
+ # @param [Discorb::Messageable] channel The channel to send the message to.
96
+ #
97
+ def start(channel, ...)
98
+ client = channel.instance_variable_get(:@client)
99
+ if @views.empty?
100
+ raise "No views defined"
101
+ elsif not @views.any? { |v| v.check.nil? }
102
+ raise "No fallback view defined"
103
+ elsif @views.filter { |v| v.check.nil? }.count > 1
104
+ raise "Multiple fallback views defined"
105
+ end
106
+ view = new(client, channel, ...)
107
+ view.start
108
+ end
109
+
110
+ #
111
+ # Modules for the prepend.
112
+ #
113
+ module Prepend
114
+ # @return [Discorb::MessageComponentInteraction] The interaction.
115
+ attr_writer :interaction
116
+
117
+ #
118
+ # The result for rendering the view.
119
+ #
120
+ class Result < Struct.new(:content, :embeds, :components); end
121
+
122
+ # @private
123
+ def initialize(client, channel, ...)
124
+ @client = channel.instance_variable_get(:@client)
125
+ @channel = channel
126
+ @message_id = nil
127
+ @stopped = false
128
+ @result = Result.new(nil, [], [])
129
+ super(...)
130
+ end
131
+
132
+ # @private
133
+ def start
134
+ render
135
+ @client.views[@message_id.to_s] = self
136
+ end
137
+
138
+ #
139
+ # Stops the view.
140
+ #
141
+ # @param [Boolean] disable Whether to disable the components.
142
+ # @param [Boolean] delete Whether to delete the message.
143
+ #
144
+ def stop!(disable: true, delete: false)
145
+ @client.views.delete(@message_id.to_s)
146
+ if disable
147
+ @stopped = true
148
+ render
149
+ end
150
+ @channel.delete_message!(@message_id) if delete
151
+ end
152
+
153
+ #
154
+ # Renders the view.
155
+ #
156
+ def render
157
+ instance_exec(@result, &actual_view.block)
158
+ components = @result.components.map do |c|
159
+ case c
160
+ when Symbol
161
+ self.class.components[c]&.object or raise ArgumentError "Unknown component ID #{c}"
162
+ when Button
163
+ c
164
+ else
165
+ raise ArgumentError "Component must be a Symbol or a Button"
166
+ end
167
+ end
168
+ if @stopped
169
+ components.each do |component|
170
+ component.disabled = true
171
+ end
172
+ end
173
+ if @interaction
174
+ @interaction.edit(@result.content, embeds: @result.embeds, components: components).wait
175
+ else
176
+ msg = @channel.post(@result.content, embeds: @result.embeds, components: components).wait
177
+ @message_id = msg.id
178
+ end
179
+ end
180
+
181
+ # @private
182
+ def actual_view
183
+ view = self.class.views.filter { |v| v.check }.find { |v| instance_exec(@interaction, &v.check) }
184
+ if view.nil?
185
+ view = self.class.views.find { |v| v.check.nil? }
186
+ end
187
+ view
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "view/version"
4
+ require_relative "view/view"
5
+ require_relative "view/extension"
6
+
7
+ module Discorb
8
+ module View
9
+ class Error < StandardError; end
10
+
11
+ # Your code goes here...
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discorb-view
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sevenc-nanashi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: discorb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.6
27
+ description:
28
+ email:
29
+ - sevenc-nanashi@sevenbot.jp
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - discorb-view.gemspec
43
+ - examples/menu.rb
44
+ - examples/pager.rb
45
+ - lib/discorb/view.rb
46
+ - lib/discorb/view/extension.rb
47
+ - lib/discorb/view/version.rb
48
+ - lib/discorb/view/view.rb
49
+ homepage: https://github.com/discorb-lib/discorb-view
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org
54
+ homepage_uri: https://github.com/discorb-lib/discorb-view
55
+ source_code_uri: https://github.com/discorb-lib/discorb-view
56
+ changelog_uri: https://github.com/discorb-lib/discorb-view/blob/main/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.0.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.2.22
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: View for Discorb
76
+ test_files: []