Class: Discordrb::Webhooks::Builder
- Inherits:
-
Object
- Object
- Discordrb::Webhooks::Builder
- Defined in:
- lib/discordrb/webhooks/builder.rb
Overview
A class that acts as a builder for a webhook message object.
Instance Attribute Summary collapse
-
#allowed_mentions ⇒ Discordrb::AllowedMentions, Hash
Mentions that are allowed to ping in this message.
-
#avatar_url ⇒ String
The URL of an image file to be used as an avatar.
-
#content ⇒ String
The content of the message.
-
#embeds ⇒ Array<Embed>
readonly
The embeds attached to this message.
-
#file ⇒ File?
The file attached to this message.
-
#poll ⇒ Poll, ...
The poll attached to this message.
-
#tts ⇒ true, false
Whether this message should use TTS or not.
-
#username ⇒ String
The username the webhook will display as.
Instance Method Summary collapse
-
#<<(embed) ⇒ Object
Adds an embed to this message.
-
#add_embed(embed = nil) {|embed| ... } ⇒ Embed
Convenience method to add an embed using a block-style builder pattern.
-
#add_poll(poll = nil, **kwargs) {|poll| ... } ⇒ Poll::Builder, Poll
(also: #poll)
Convenience method to add a poll using a builder pattern.
-
#initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil, poll: nil) ⇒ Builder
constructor
A new instance of Builder.
-
#to_json_hash ⇒ Hash
A hash representation of the created message, for JSON format.
-
#to_multipart_hash ⇒ Hash
A hash representation of the created message, for multipart format.
Constructor Details
#initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil, poll: nil) ⇒ Builder
Returns a new instance of Builder.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/discordrb/webhooks/builder.rb', line 8 def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil, poll: nil) @content = content @username = username @avatar_url = avatar_url @tts = tts @file = file @embeds = @allowed_mentions = allowed_mentions @poll = poll end |
Instance Attribute Details
#allowed_mentions ⇒ Discordrb::AllowedMentions, Hash
Returns Mentions that are allowed to ping in this message.
95 96 97 |
# File 'lib/discordrb/webhooks/builder.rb', line 95 def allowed_mentions @allowed_mentions end |
#avatar_url ⇒ String
The URL of an image file to be used as an avatar. If this is not set, the default avatar from the webhook's settings will be used instead.
31 32 33 |
# File 'lib/discordrb/webhooks/builder.rb', line 31 def avatar_url @avatar_url end |
#content ⇒ String
The content of the message. May be 2000 characters long at most.
21 22 23 |
# File 'lib/discordrb/webhooks/builder.rb', line 21 def content @content end |
#embeds ⇒ Array<Embed> (readonly)
Returns the embeds attached to this message.
91 92 93 |
# File 'lib/discordrb/webhooks/builder.rb', line 91 def @embeds end |
#file ⇒ File?
Returns the file attached to this message.
88 89 90 |
# File 'lib/discordrb/webhooks/builder.rb', line 88 def file @file end |
#poll=(value) ⇒ Poll, ...
Returns The poll attached to this message.
99 100 101 |
# File 'lib/discordrb/webhooks/builder.rb', line 99 def poll=(value) @poll = value end |
#tts ⇒ true, false
Whether this message should use TTS or not. By default, it doesn't.
35 36 37 |
# File 'lib/discordrb/webhooks/builder.rb', line 35 def tts @tts end |
#username ⇒ String
The username the webhook will display as. If this is not set, the default username set in the webhook's settings will be used instead.
26 27 28 |
# File 'lib/discordrb/webhooks/builder.rb', line 26 def username @username end |
Instance Method Details
#<<(embed) ⇒ Object
Adds an embed to this message.
48 49 50 51 52 |
# File 'lib/discordrb/webhooks/builder.rb', line 48 def <<() raise ArgumentError, 'Embeds and files are mutually exclusive!' if @file @embeds << end |
#add_embed(embed = nil) {|embed| ... } ⇒ Embed
Convenience method to add an embed using a block-style builder pattern
62 63 64 65 66 67 |
# File 'lib/discordrb/webhooks/builder.rb', line 62 def ( = nil) ||= Embed.new yield() self << end |
#add_poll(poll = nil, **kwargs) {|poll| ... } ⇒ Poll::Builder, Poll Also known as: poll
Convenience method to add a poll using a builder pattern
78 79 80 81 82 83 |
# File 'lib/discordrb/webhooks/builder.rb', line 78 def add_poll(poll = nil, **kwargs) poll ||= Discordrb::Poll::Builder.new(**kwargs) yield(poll) if block_given? @poll = poll poll end |
#to_json_hash ⇒ Hash
Returns a hash representation of the created message, for JSON format.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/discordrb/webhooks/builder.rb', line 102 def to_json_hash { content: @content, username: @username, avatar_url: @avatar_url, tts: @tts, embeds: @embeds.map(&:to_hash), allowed_mentions: @allowed_mentions&.to_hash, poll: @poll&.to_h } end |
#to_multipart_hash ⇒ Hash
Returns a hash representation of the created message, for multipart format.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/discordrb/webhooks/builder.rb', line 115 def to_multipart_hash { content: @content, username: @username, avatar_url: @avatar_url, tts: @tts, file: @file, allowed_mentions: @allowed_mentions&.to_hash } end |