Class: OnyxCord::Webhooks::Builder
- Inherits:
-
Object
- Object
- OnyxCord::Webhooks::Builder
- Defined in:
- lib/onyxcord/webhooks/builder.rb
Overview
A class that acts as a builder for a webhook message object.
Instance Attribute Summary collapse
-
#allowed_mentions ⇒ OnyxCord::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.
-
#flags ⇒ Integer
Message flags to send with this webhook payload.
-
#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.
-
#components_v2! ⇒ Builder
(also: #has_components!)
Enable Discord’s Components V2 message flag.
-
#components_v2? ⇒ true, false
Whether Components V2 is enabled on this payload.
-
#initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil, poll: nil, flags: 0) ⇒ 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, flags: 0) ⇒ Builder
Returns a new instance of Builder.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/onyxcord/webhooks/builder.rb', line 9 def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil, poll: nil, flags: 0) @content = content @username = username @avatar_url = avatar_url @tts = tts @file = file @embeds = @allowed_mentions = allowed_mentions @poll = poll @flags = flags end |
Instance Attribute Details
#allowed_mentions ⇒ OnyxCord::AllowedMentions, Hash
Returns Mentions that are allowed to ping in this message.
115 116 117 |
# File 'lib/onyxcord/webhooks/builder.rb', line 115 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.
33 34 35 |
# File 'lib/onyxcord/webhooks/builder.rb', line 33 def avatar_url @avatar_url end |
#content ⇒ String
The content of the message. May be 2000 characters long at most.
23 24 25 |
# File 'lib/onyxcord/webhooks/builder.rb', line 23 def content @content end |
#embeds ⇒ Array<Embed> (readonly)
Returns the embeds attached to this message.
111 112 113 |
# File 'lib/onyxcord/webhooks/builder.rb', line 111 def @embeds end |
#file ⇒ File?
Returns the file attached to this message.
108 109 110 |
# File 'lib/onyxcord/webhooks/builder.rb', line 108 def file @file end |
#flags ⇒ Integer
Message flags to send with this webhook payload.
41 42 43 |
# File 'lib/onyxcord/webhooks/builder.rb', line 41 def flags @flags end |
#poll=(value) ⇒ Poll, ...
Returns The poll attached to this message.
119 120 121 |
# File 'lib/onyxcord/webhooks/builder.rb', line 119 def poll=(value) @poll = value end |
#tts ⇒ true, false
Whether this message should use TTS or not. By default, it doesn’t.
37 38 39 |
# File 'lib/onyxcord/webhooks/builder.rb', line 37 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.
28 29 30 |
# File 'lib/onyxcord/webhooks/builder.rb', line 28 def username @username end |
Instance Method Details
#<<(embed) ⇒ Object
Adds an embed to this message.
68 69 70 71 72 |
# File 'lib/onyxcord/webhooks/builder.rb', line 68 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
82 83 84 85 86 87 |
# File 'lib/onyxcord/webhooks/builder.rb', line 82 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
98 99 100 101 102 103 |
# File 'lib/onyxcord/webhooks/builder.rb', line 98 def add_poll(poll = nil, **kwargs) poll ||= OnyxCord::Poll::Builder.new(**kwargs) yield(poll) if block_given? @poll = poll poll end |
#components_v2! ⇒ Builder Also known as: has_components!
Enable Discord’s Components V2 message flag.
45 46 47 48 |
# File 'lib/onyxcord/webhooks/builder.rb', line 45 def components_v2! @flags = @flags.to_i | OnyxCord::MessageComponents::IS_COMPONENTS_V2 self end |
#components_v2? ⇒ true, false
Returns whether Components V2 is enabled on this payload.
53 54 55 |
# File 'lib/onyxcord/webhooks/builder.rb', line 53 def components_v2? (@flags.to_i & OnyxCord::MessageComponents::IS_COMPONENTS_V2).positive? end |
#to_json_hash ⇒ Hash
Returns a hash representation of the created message, for JSON format.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/onyxcord/webhooks/builder.rb', line 122 def to_json_hash data = { 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 } data[:flags] = @flags if @flags.to_i.positive? data end |
#to_multipart_hash ⇒ Hash
Returns a hash representation of the created message, for multipart format.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/onyxcord/webhooks/builder.rb', line 137 def to_multipart_hash data = { content: @content, username: @username, avatar_url: @avatar_url, tts: @tts, file: @file, allowed_mentions: @allowed_mentions&.to_hash } data[:flags] = @flags if @flags.to_i.positive? data end |