Module: Discorb::View::Base::Prepend

Defined in:
lib/discorb/view/view.rb

Overview

Modules for the prepend.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#interaction=(value) ⇒ Discorb::MessageComponentInteraction (writeonly)

Returns The interaction.

Returns:

  • (Discorb::MessageComponentInteraction)

    The interaction.



116
117
118
# File 'lib/discorb/view/view.rb', line 116

def interaction=(value)
  @interaction = value
end

Instance Method Details

#actual_viewObject



184
185
186
187
188
189
190
# File 'lib/discorb/view/view.rb', line 184

def actual_view
  view = self.class.views.filter { |v| v.check }.find { |v| instance_exec(@interaction, &v.check) }
  if view.nil?
    view = self.class.views.find { |v| v.check.nil? }
  end
  view
end

#initialize(client, channel) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/discorb/view/view.rb', line 124

def initialize(client, channel, ...)
  @client = channel.instance_variable_get(:@client)
  @channel = channel
  @message_id = nil
  @stopped = false
  @components = self.class.components.dup.map { |id, component| [id, ComponentHandler.new(component.object.dup, component.block)] }.to_h
  @result = Result.new(nil, [], [])
  super(...)
end

#renderObject

Renders the view.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/discorb/view/view.rb', line 158

def render
  instance_exec(@result, &actual_view.block)
  components = @result.components.map do |c|
    case c
    when Symbol
      @components[c]&.object or raise ArgumentError "Unknown component ID #{c}"
    when Button
      c
    else
      raise ArgumentError "Component must be a Symbol or a Button"
    end
  end
  if @stopped
    components.each do |component|
      component.disabled = true
    end
  end
  if @interaction
    @interaction.edit(@result.content, embeds: @result.embeds, components: components).wait
  else
    msg = @channel.post(@result.content, embeds: @result.embeds, components: components).wait
    @message_id = msg.id
  end
end

#startObject



135
136
137
138
# File 'lib/discorb/view/view.rb', line 135

def start
  render
  @client.views[@message_id.to_s] = self
end

#stop!(disable: true, delete: false) ⇒ Object

Stops the view.

Parameters:

  • disable (Boolean) (defaults to: true)

    Whether to disable the components.

  • delete (Boolean) (defaults to: false)

    Whether to delete the message.



146
147
148
149
150
151
152
153
# File 'lib/discorb/view/view.rb', line 146

def stop!(disable: true, delete: false)
  @client.views.delete(@message_id.to_s)
  if disable
    @stopped = true
    render
  end
  @channel.delete_message!(@message_id) if delete
end