discordrb-webhooks-blackst0ne 3.3.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a0b6c712e809afdf65333c51e3657686cba5de2cb7430da3042d192feb13a85
4
+ data.tar.gz: d3c1a56a281ed74c84c733aa4a49cbcdc1daa49f88657b0ee6503e52ad929914
5
+ SHA512:
6
+ metadata.gz: a00c1a246dbb2261c3eb8b4b4210bbd7c29116593f47a7157a405e702f3cd7ed4a0c574870b477999f24644aa8327bcc60eaff06f0b0a4bb5d460b6a968a9f7b
7
+ data.tar.gz: 3e6e331e99356a7ca73925b570d95774b3468e3cfd71645ec206a3ce909415d12d99cc2b2b50893e6ea81387f11cc142dec3e7d0b7be9c6573d6c846e205be7d
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'discordrb/webhooks/version'
4
+ require 'discordrb/webhooks/embeds'
5
+ require 'discordrb/webhooks/client'
6
+ require 'discordrb/webhooks/builder'
7
+
8
+ module Discordrb
9
+ # Webhook client
10
+ module Webhooks
11
+ end
12
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'discordrb/webhooks/embeds'
4
+
5
+ module Discordrb::Webhooks
6
+ # A class that acts as a builder for a webhook message object.
7
+ class Builder
8
+ def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [])
9
+ @content = content
10
+ @username = username
11
+ @avatar_url = avatar_url
12
+ @tts = tts
13
+ @file = file
14
+ @embeds = embeds
15
+ end
16
+
17
+ # The content of the message. May be 2000 characters long at most.
18
+ # @return [String] the content of the message.
19
+ attr_accessor :content
20
+
21
+ # The username the webhook will display as. If this is not set, the default username set in the webhook's settings
22
+ # will be used instead.
23
+ # @return [String] the username.
24
+ attr_accessor :username
25
+
26
+ # The URL of an image file to be used as an avatar. If this is not set, the default avatar from the webhook's
27
+ # settings will be used instead.
28
+ # @return [String] the avatar URL.
29
+ attr_accessor :avatar_url
30
+
31
+ # Whether this message should use TTS or not. By default, it doesn't.
32
+ # @return [true, false] the TTS status.
33
+ attr_accessor :tts
34
+
35
+ # Sets a file to be sent together with the message. Mutually exclusive with embeds; a webhook message can contain
36
+ # either a file to be sent or an embed.
37
+ # @param file [File] A file to be sent.
38
+ def file=(file)
39
+ raise ArgumentError, 'Embeds and files are mutually exclusive!' unless @embeds.empty?
40
+
41
+ @file = file
42
+ end
43
+
44
+ # Adds an embed to this message.
45
+ # @param embed [Embed] The embed to add.
46
+ def <<(embed)
47
+ raise ArgumentError, 'Embeds and files are mutually exclusive!' if @file
48
+
49
+ @embeds << embed
50
+ end
51
+
52
+ # Convenience method to add an embed using a block-style builder pattern
53
+ # @example Add an embed to a message
54
+ # builder.add_embed do |embed|
55
+ # embed.title = 'Testing'
56
+ # embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
57
+ # end
58
+ # @param embed [Embed, nil] The embed to start the building process with, or nil if one should be created anew.
59
+ # @return [Embed] The created embed.
60
+ def add_embed(embed = nil)
61
+ embed ||= Embed.new
62
+ yield(embed)
63
+ self << embed
64
+ embed
65
+ end
66
+
67
+ # @return [File, nil] the file attached to this message.
68
+ attr_reader :file
69
+
70
+ # @return [Array<Embed>] the embeds attached to this message.
71
+ attr_reader :embeds
72
+
73
+ # @return [Hash] a hash representation of the created message, for JSON format.
74
+ def to_json_hash
75
+ {
76
+ content: @content,
77
+ username: @username,
78
+ avatar_url: @avatar_url,
79
+ tts: @tts,
80
+ embeds: @embeds.map(&:to_hash)
81
+ }
82
+ end
83
+
84
+ # @return [Hash] a hash representation of the created message, for multipart format.
85
+ def to_multipart_hash
86
+ {
87
+ content: @content,
88
+ username: @username,
89
+ avatar_url: @avatar_url,
90
+ tts: @tts,
91
+ file: @file
92
+ }
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+ require 'json'
5
+
6
+ require 'discordrb/webhooks/builder'
7
+
8
+ module Discordrb::Webhooks
9
+ # A client for a particular webhook added to a Discord channel.
10
+ class Client
11
+ # Create a new webhook
12
+ # @param url [String] The URL to post messages to.
13
+ # @param id [Integer] The webhook's ID. Will only be used if `url` is not
14
+ # set.
15
+ # @param token [String] The webhook's authorisation token. Will only be used
16
+ # if `url` is not set.
17
+ def initialize(url: nil, id: nil, token: nil)
18
+ @url = url || generate_url(id, token)
19
+ end
20
+
21
+ # Executes the webhook this client points to with the given data.
22
+ # @param builder [Builder, nil] The builder to start out with, or nil if one should be created anew.
23
+ # @param wait [true, false] Whether Discord should wait for the message to be successfully received by clients, or
24
+ # whether it should return immediately after sending the message.
25
+ # @yield [builder] Gives the builder to the block to add additional steps, or to do the entire building process.
26
+ # @yieldparam builder [Builder] The builder given as a parameter which is used as the initial step to start from.
27
+ # @example Execute the webhook with an already existing builder
28
+ # builder = Discordrb::Webhooks::Builder.new # ...
29
+ # client.execute(builder)
30
+ # @example Execute the webhook by building a new message
31
+ # client.execute do |builder|
32
+ # builder.content = 'Testing'
33
+ # builder.username = 'discordrb'
34
+ # builder.add_embed do |embed|
35
+ # embed.timestamp = Time.now
36
+ # embed.title = 'Testing'
37
+ # embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
38
+ # end
39
+ # end
40
+ # @return [RestClient::Response] the response returned by Discord.
41
+ def execute(builder = nil, wait = false)
42
+ raise TypeError, 'builder needs to be nil or like a Discordrb::Webhooks::Builder!' unless
43
+ builder.respond_to?(:file) && builder.respond_to?(:to_multipart_hash) || builder.respond_to?(:to_json_hash) || builder.nil?
44
+
45
+ builder ||= Builder.new
46
+
47
+ yield builder if block_given?
48
+
49
+ if builder.file
50
+ post_multipart(builder, wait)
51
+ else
52
+ post_json(builder, wait)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def post_json(builder, wait)
59
+ RestClient.post(@url + (wait ? '?wait=true' : ''), builder.to_json_hash.to_json, content_type: :json)
60
+ end
61
+
62
+ def post_multipart(builder, wait)
63
+ RestClient.post(@url + (wait ? '?wait=true' : ''), builder.to_multipart_hash)
64
+ end
65
+
66
+ def generate_url(id, token)
67
+ "https://discordapp.com/api/v6/webhooks/#{id}/#{token}"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,250 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discordrb::Webhooks
4
+ # An embed is a multipart-style attachment to a webhook message that can have a variety of different purposes and
5
+ # appearances.
6
+ class Embed
7
+ def initialize(title: nil, description: nil, url: nil, timestamp: nil, colour: nil, color: nil, footer: nil,
8
+ image: nil, thumbnail: nil, video: nil, provider: nil, author: nil, fields: [])
9
+ @title = title
10
+ @description = description
11
+ @url = url
12
+ @timestamp = timestamp
13
+ self.colour = colour || color
14
+ @footer = footer
15
+ @image = image
16
+ @thumbnail = thumbnail
17
+ @video = video
18
+ @provider = provider
19
+ @author = author
20
+ @fields = fields
21
+ end
22
+
23
+ # @return [String, nil] title of the embed that will be displayed above everything else.
24
+ attr_accessor :title
25
+
26
+ # @return [String, nil] description for this embed
27
+ attr_accessor :description
28
+
29
+ # @return [String, nil] URL the title should point to
30
+ attr_accessor :url
31
+
32
+ # @return [Time, nil] timestamp for this embed. Will be displayed just below the title.
33
+ attr_accessor :timestamp
34
+
35
+ # @return [Integer, nil] the colour of the bar to the side, in decimal form
36
+ attr_reader :colour
37
+ alias_method :color, :colour
38
+
39
+ # Sets the colour of the bar to the side of the embed to something new.
40
+ # @param value [Integer, String, {Integer, Integer, Integer}, #to_i, nil] The colour in decimal, hexadecimal, R/G/B decimal, or nil to clear the embeds colour
41
+ # form.
42
+ def colour=(value)
43
+ if value.nil?
44
+ @colour = nil
45
+ elsif value.is_a? Integer
46
+ raise ArgumentError, 'Embed colour must be 24-bit!' if value >= 16_777_216
47
+
48
+ @colour = value
49
+ elsif value.is_a? String
50
+ self.colour = value.delete('#').to_i(16)
51
+ elsif value.is_a? Array
52
+ raise ArgumentError, 'Colour tuple must have three values!' if value.length != 3
53
+
54
+ self.colour = value[0] << 16 | value[1] << 8 | value[2]
55
+ else
56
+ self.colour = value.to_i
57
+ end
58
+ end
59
+
60
+ alias_method :color=, :colour=
61
+
62
+ # @example Add a footer to an embed
63
+ # embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: 'Hello', icon_url: 'https://i.imgur.com/j69wMDu.jpg')
64
+ # @return [EmbedFooter, nil] footer for this embed
65
+ attr_accessor :footer
66
+
67
+ # @see EmbedImage
68
+ # @example Add a image to an embed
69
+ # embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
70
+ # @return [EmbedImage, nil] image for this embed
71
+ attr_accessor :image
72
+
73
+ # @see EmbedThumbnail
74
+ # @example Add a thumbnail to an embed
75
+ # embed.thumbnail = Discordrb::Webhooks::EmbedThumbnail.new(url: 'https://i.imgur.com/xTG3a1I.jpg')
76
+ # @return [EmbedThumbnail, nil] thumbnail for this embed
77
+ attr_accessor :thumbnail
78
+
79
+ # @see EmbedAuthor
80
+ # @example Add a author to an embed
81
+ # embed.author = Discordrb::Webhooks::EmbedAuthor.new(name: 'meew0', url: 'https://github.com/meew0', icon_url: 'https://avatars2.githubusercontent.com/u/3662915?v=3&s=466')
82
+ # @return [EmbedAuthor, nil] author for this embed
83
+ attr_accessor :author
84
+
85
+ # Add a field object to this embed.
86
+ # @param field [EmbedField] The field to add.
87
+ def <<(field)
88
+ @fields << field
89
+ end
90
+
91
+ # Convenience method to add a field to the embed without having to create one manually.
92
+ # @see EmbedField
93
+ # @example Add a field to an embed, conveniently
94
+ # embed.add_field(name: 'A field', value: "The field's content")
95
+ # @param name [String] The field's name
96
+ # @param value [String] The field's value
97
+ # @param inline [true, false] Whether the field should be inlined
98
+ def add_field(name: nil, value: nil, inline: nil)
99
+ self << EmbedField.new(name: name, value: value, inline: inline)
100
+ end
101
+
102
+ # @return [Array<EmbedField>] the fields attached to this embed.
103
+ attr_accessor :fields
104
+
105
+ # @return [Hash] a hash representation of this embed, to be converted to JSON.
106
+ def to_hash
107
+ {
108
+ title: @title,
109
+ description: @description,
110
+ url: @url,
111
+ timestamp: @timestamp&.utc&.iso8601,
112
+ color: @colour,
113
+ footer: @footer&.to_hash,
114
+ image: @image&.to_hash,
115
+ thumbnail: @thumbnail&.to_hash,
116
+ video: @video&.to_hash,
117
+ provider: @provider&.to_hash,
118
+ author: @author&.to_hash,
119
+ fields: @fields.map(&:to_hash)
120
+ }
121
+ end
122
+ end
123
+
124
+ # An embed's footer will be displayed at the very bottom of an embed, together with the timestamp. An icon URL can be
125
+ # set together with some text to be displayed.
126
+ class EmbedFooter
127
+ # @return [String, nil] text to be displayed in the footer
128
+ attr_accessor :text
129
+
130
+ # @return [String, nil] URL to an icon to be showed alongside the text
131
+ attr_accessor :icon_url
132
+
133
+ # Creates a new footer object.
134
+ # @param text [String, nil] The text to be displayed in the footer.
135
+ # @param icon_url [String, nil] The URL to an icon to be showed alongside the text.
136
+ def initialize(text: nil, icon_url: nil)
137
+ @text = text
138
+ @icon_url = icon_url
139
+ end
140
+
141
+ # @return [Hash] a hash representation of this embed footer, to be converted to JSON.
142
+ def to_hash
143
+ {
144
+ text: @text,
145
+ icon_url: @icon_url
146
+ }
147
+ end
148
+ end
149
+
150
+ # An embed's image will be displayed at the bottom, in large format. It will replace a footer icon URL if one is set.
151
+ class EmbedImage
152
+ # @return [String, nil] URL of the image
153
+ attr_accessor :url
154
+
155
+ # Creates a new image object.
156
+ # @param url [String, nil] The URL of the image.
157
+ def initialize(url: nil)
158
+ @url = url
159
+ end
160
+
161
+ # @return [Hash] a hash representation of this embed image, to be converted to JSON.
162
+ def to_hash
163
+ {
164
+ url: @url
165
+ }
166
+ end
167
+ end
168
+
169
+ # An embed's thumbnail will be displayed at the right of the message, next to the description and fields. When clicked
170
+ # it will point to the embed URL.
171
+ class EmbedThumbnail
172
+ # @return [String, nil] URL of the thumbnail
173
+ attr_accessor :url
174
+
175
+ # Creates a new thumbnail object.
176
+ # @param url [String, nil] The URL of the thumbnail.
177
+ def initialize(url: nil)
178
+ @url = url
179
+ end
180
+
181
+ # @return [Hash] a hash representation of this embed thumbnail, to be converted to JSON.
182
+ def to_hash
183
+ {
184
+ url: @url
185
+ }
186
+ end
187
+ end
188
+
189
+ # An embed's author will be shown at the top to indicate who "authored" the particular event the webhook was sent for.
190
+ class EmbedAuthor
191
+ # @return [String, nil] name of the author
192
+ attr_accessor :name
193
+
194
+ # @return [String, nil] URL the name should link to
195
+ attr_accessor :url
196
+
197
+ # @return [String, nil] URL of the icon to be displayed next to the author
198
+ attr_accessor :icon_url
199
+
200
+ # Creates a new author object.
201
+ # @param name [String, nil] The name of the author.
202
+ # @param url [String, nil] The URL the name should link to.
203
+ # @param icon_url [String, nil] The URL of the icon to be displayed next to the author.
204
+ def initialize(name: nil, url: nil, icon_url: nil)
205
+ @name = name
206
+ @url = url
207
+ @icon_url = icon_url
208
+ end
209
+
210
+ # @return [Hash] a hash representation of this embed author, to be converted to JSON.
211
+ def to_hash
212
+ {
213
+ name: @name,
214
+ url: @url,
215
+ icon_url: @icon_url
216
+ }
217
+ end
218
+ end
219
+
220
+ # A field is a small block of text with a header that can be relatively freely layouted with other fields.
221
+ class EmbedField
222
+ # @return [String, nil] name of the field, displayed in bold at the top of the field.
223
+ attr_accessor :name
224
+
225
+ # @return [String, nil] value of the field, displayed in normal text below the name.
226
+ attr_accessor :value
227
+
228
+ # @return [true, false] whether the field should be displayed inline with other fields.
229
+ attr_accessor :inline
230
+
231
+ # Creates a new field object.
232
+ # @param name [String, nil] The name of the field, displayed in bold at the top of the field.
233
+ # @param value [String, nil] The value of the field, displayed in normal text below the name.
234
+ # @param inline [true, false] Whether the field should be displayed inline with other fields.
235
+ def initialize(name: nil, value: nil, inline: false)
236
+ @name = name
237
+ @value = value
238
+ @inline = inline
239
+ end
240
+
241
+ # @return [Hash] a hash representation of this embed field, to be converted to JSON.
242
+ def to_hash
243
+ {
244
+ name: @name,
245
+ value: @value,
246
+ inline: @inline
247
+ }
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Webhook support for discordrb
4
+ module Discordrb
5
+ module Webhooks
6
+ # The current version of discordrb-webhooks.
7
+ VERSION = '3.3.0'
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discordrb-webhooks-blackst0ne
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.3.0
5
+ platform: ruby
6
+ authors:
7
+ - blackst0ne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: A client for Discord's webhooks to fit alongside [discordrb](https://rubygems.org/gems/discordrb).
28
+ email:
29
+ - ''
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/discordrb/webhooks.rb
35
+ - lib/discordrb/webhooks/builder.rb
36
+ - lib/discordrb/webhooks/client.rb
37
+ - lib/discordrb/webhooks/embeds.rb
38
+ - lib/discordrb/webhooks/version.rb
39
+ homepage: https://github.com/blackst0ne/discordrb
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.7
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.7.6
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Webhook client for discordrb. Fork by blackst0ne
63
+ test_files: []