-
Notifications
You must be signed in to change notification settings - Fork 92
/
builder.rb
102 lines (86 loc) · 3.33 KB
/
builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
require 'discordrb/webhooks/embeds'
module Discordrb::Webhooks
# A class that acts as a builder for a webhook message object.
class Builder
def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [], allowed_mentions: nil)
@content = content
@username = username
@avatar_url = avatar_url
@tts = tts
@file = file
@embeds = embeds
@allowed_mentions = allowed_mentions
end
# The content of the message. May be 2000 characters long at most.
# @return [String] the content of the message.
attr_accessor :content
# 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.
# @return [String] the username.
attr_accessor :username
# 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.
# @return [String] the avatar URL.
attr_accessor :avatar_url
# Whether this message should use TTS or not. By default, it doesn't.
# @return [true, false] the TTS status.
attr_accessor :tts
# Sets a file to be sent together with the message. Mutually exclusive with embeds; a webhook message can contain
# either a file to be sent or an embed.
# @param file [File] A file to be sent.
def file=(file)
raise ArgumentError, 'Embeds and files are mutually exclusive!' unless @embeds.empty?
@file = file
end
# Adds an embed to this message.
# @param embed [Embed] The embed to add.
def <<(embed)
raise ArgumentError, 'Embeds and files are mutually exclusive!' if @file
@embeds << embed
end
# Convenience method to add an embed using a block-style builder pattern
# @example Add an embed to a message
# builder.add_embed do |embed|
# embed.title = 'Testing'
# embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
# end
# @param embed [Embed, nil] The embed to start the building process with, or nil if one should be created anew.
# @return [Embed] The created embed.
def add_embed(embed = nil)
embed ||= Embed.new
yield(embed)
self << embed
embed
end
# @return [File, nil] the file attached to this message.
attr_reader :file
# @return [Array<Embed>] the embeds attached to this message.
attr_reader :embeds
# @return [Discordrb::AllowedMentions, Hash] Mentions that are allowed to ping in this message.
# @see https://discord.com/developers/docs/resources/channel#allowed-mentions-object
attr_accessor :allowed_mentions
# @return [Hash] a hash representation of the created message, for JSON format.
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
}
end
# @return [Hash] a hash representation of the created message, for multipart format.
def to_multipart_hash
{
content: @content,
username: @username,
avatar_url: @avatar_url,
tts: @tts,
file: @file,
allowed_mentions: @allowed_mentions&.to_hash
}
end
end
end