discordrb-webhooks 0.1.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.
- checksums.yaml +7 -0
- data/lib/discordrb/webhooks.rb +12 -0
- data/lib/discordrb/webhooks/builder.rb +91 -0
- data/lib/discordrb/webhooks/client.rb +72 -0
- data/lib/discordrb/webhooks/embeds.rb +220 -0
- data/lib/discordrb/webhooks/version.rb +9 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e33e989ed634c58e7bec8ee4b270addbaf2afb1
|
4
|
+
data.tar.gz: d75947052d5ba2f4047622e65d3a83a829cfbc05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0093a27a7257c39ebb0f27d756be7c682e58a2479d7ec6a7bdaca72a1d251d36794ec5b503cd4ddc1d738f5fa91ae2170aa69fd1c334a944eac73078e54e96f9'
|
7
|
+
data.tar.gz: 5781b554604a09ea88afb284c4cb522c16581ac4855974f71c54462a67ae89cce117f5c23c62ba2ba5bc0be3d1198e5ae346036b3b58e9207d7f50e06ee6540d
|
@@ -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,91 @@
|
|
1
|
+
require 'discordrb/webhooks/embeds'
|
2
|
+
|
3
|
+
module Discordrb::Webhooks
|
4
|
+
# A class that acts as a builder for a webhook message object.
|
5
|
+
class Builder
|
6
|
+
def initialize(content: '', username: nil, avatar_url: nil, tts: false, file: nil, embeds: [])
|
7
|
+
@content = content
|
8
|
+
@username = username
|
9
|
+
@avatar_url = avatar_url
|
10
|
+
@tts = tts
|
11
|
+
@file = file
|
12
|
+
@embeds = embeds
|
13
|
+
end
|
14
|
+
|
15
|
+
# The content of the message. May be 2000 characters long at most.
|
16
|
+
# @return [String] the content of the message.
|
17
|
+
attr_accessor :content
|
18
|
+
|
19
|
+
# The username the webhook will display as. If this is not set, the default username set in the webhook's settings
|
20
|
+
# will be used instead.
|
21
|
+
# @return [String] the username.
|
22
|
+
attr_accessor :username
|
23
|
+
|
24
|
+
# The URL of an image file to be used as an avatar. If this is not set, the default avatar from the webhook's
|
25
|
+
# settings will be used instead.
|
26
|
+
# @return [String] the avatar URL.
|
27
|
+
attr_accessor :avatar_url
|
28
|
+
|
29
|
+
# Whether this message should use TTS or not. By default, it doesn't.
|
30
|
+
# @return [true, false] the TTS status.
|
31
|
+
attr_accessor :tts
|
32
|
+
|
33
|
+
# Sets a file to be sent together with the message. Mutually exclusive with embeds; a webhook message can contain
|
34
|
+
# either a file to be sent or an embed.
|
35
|
+
# @param file [File] A file to be sent.
|
36
|
+
def file=(file)
|
37
|
+
raise ArgumentError, 'Embeds and files are mutually exclusive!' unless @embeds.empty?
|
38
|
+
@file = file
|
39
|
+
end
|
40
|
+
|
41
|
+
# Adds an embed to this message.
|
42
|
+
# @param embed [Embed] The embed to add.
|
43
|
+
def <<(embed)
|
44
|
+
raise ArgumentError, 'Embeds and files are mutually exclusive!' if @file
|
45
|
+
@embeds << embed
|
46
|
+
end
|
47
|
+
|
48
|
+
# Convenience method to add an embed using a block-style builder pattern
|
49
|
+
# @example Add an embed to a message
|
50
|
+
# builder.add_embed do |embed|
|
51
|
+
# embed.title = 'Testing'
|
52
|
+
# embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
|
53
|
+
# end
|
54
|
+
# @param embed [Embed, nil] The embed to start the building process with, or nil if one should be created anew.
|
55
|
+
# @return [Embed] The created embed.
|
56
|
+
def add_embed(embed = nil)
|
57
|
+
embed ||= Embed.new
|
58
|
+
yield(embed)
|
59
|
+
self << embed
|
60
|
+
embed
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [File, nil] the file attached to this message.
|
64
|
+
attr_reader :file
|
65
|
+
|
66
|
+
# @return [Array<Embed>] the embeds attached to this message.
|
67
|
+
attr_reader :embeds
|
68
|
+
|
69
|
+
# @return [Hash] a hash representation of the created message, for JSON format.
|
70
|
+
def to_json_hash
|
71
|
+
{
|
72
|
+
content: @content,
|
73
|
+
username: @username,
|
74
|
+
avatar_url: @avatar_url,
|
75
|
+
tts: @tts,
|
76
|
+
embeds: @embeds.map(&:to_hash)
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
# @return [Hash] a hash representation of the created message, for multipart format.
|
81
|
+
def to_multipart_hash
|
82
|
+
{
|
83
|
+
content: @content,
|
84
|
+
username: @username,
|
85
|
+
avatar_url: @avatar_url,
|
86
|
+
tts: @tts,
|
87
|
+
file: @file
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
require 'discordrb/webhooks/builder'
|
5
|
+
|
6
|
+
module Discordrb::Webhooks
|
7
|
+
# A client for a particular webhook added to a Discord channel.
|
8
|
+
class Client
|
9
|
+
# Create a new webhook
|
10
|
+
# @param url [String] The URL to post messages to.
|
11
|
+
# @param id [Integer] The webhook's ID. Will only be used if `url` is not
|
12
|
+
# set.
|
13
|
+
# @param token [String] The webhook's authorisation token. Will only be used
|
14
|
+
# if `url` is not set.
|
15
|
+
def initialize(url: nil, id: nil, token: nil)
|
16
|
+
@url = if url
|
17
|
+
url
|
18
|
+
else
|
19
|
+
generate_url(id, token)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Executes the webhook this client points to with the given data.
|
24
|
+
# @param builder [Builder, nil] The builder to start out with, or nil if one should be created anew.
|
25
|
+
# @param wait [true, false] Whether Discord should wait for the message to be successfully received by clients, or
|
26
|
+
# whether it should return immediately after sending the message.
|
27
|
+
# @yield [builder] Gives the builder to the block to add additional steps, or to do the entire building process.
|
28
|
+
# @yieldparam builder [Builder] The builder given as a parameter which is used as the initial step to start from.
|
29
|
+
# @example Execute the webhook with an already existing builder
|
30
|
+
# builder = Discordrb::Webhooks::Builder.new # ...
|
31
|
+
# client.execute(builder)
|
32
|
+
# @example Execute the webhook by building a new message
|
33
|
+
# client.execute do |builder|
|
34
|
+
# builder.content = 'Testing'
|
35
|
+
# builder.username = 'discordrb'
|
36
|
+
# builder.add_embed do |embed|
|
37
|
+
# embed.timestamp = Time.now
|
38
|
+
# embed.title = 'Testing'
|
39
|
+
# embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
# @return [RestClient::Response] the response returned by Discord.
|
43
|
+
def execute(builder = nil, wait = false)
|
44
|
+
raise TypeError, 'builder needs to be nil or like a Discordrb::Webhooks::Builder!' unless
|
45
|
+
builder.respond_to?(:file) && builder.respond_to?(:to_multipart_hash) || builder.respond_to?(:to_json_hash) || builder.nil?
|
46
|
+
|
47
|
+
builder ||= Builder.new
|
48
|
+
|
49
|
+
yield builder if block_given?
|
50
|
+
|
51
|
+
if builder.file
|
52
|
+
post_multipart(builder, wait)
|
53
|
+
else
|
54
|
+
post_json(builder, wait)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def post_json(builder, wait)
|
61
|
+
RestClient.post(@url + (wait ? '?wait=true' : ''), builder.to_json_hash.to_json, content_type: :json)
|
62
|
+
end
|
63
|
+
|
64
|
+
def post_multipart(builder, wait)
|
65
|
+
RestClient.post(@url + (wait ? '?wait=true' : ''), builder.to_multipart_hash)
|
66
|
+
end
|
67
|
+
|
68
|
+
def generate_url(id, token)
|
69
|
+
"https://discordapp.com/api/v6/webhooks/#{id}/#{token}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
module Discordrb::Webhooks
|
2
|
+
# An embed is a multipart-style attachment to a webhook message that can have a variety of different purposes and
|
3
|
+
# appearances.
|
4
|
+
class Embed
|
5
|
+
def initialize(title: nil, description: nil, url: nil, timestamp: nil, colour: nil, color: nil, footer: nil,
|
6
|
+
image: nil, thumbnail: nil, video: nil, provider: nil, author: nil, fields: [])
|
7
|
+
@title = title
|
8
|
+
@description = description
|
9
|
+
@url = url
|
10
|
+
@timestamp = timestamp
|
11
|
+
@colour = colour || color
|
12
|
+
@footer = footer
|
13
|
+
@image = image
|
14
|
+
@thumbnail = thumbnail
|
15
|
+
@video = video
|
16
|
+
@provider = provider
|
17
|
+
@author = author
|
18
|
+
@fields = fields
|
19
|
+
end
|
20
|
+
|
21
|
+
# The title of this embed that will be displayed above everything else.
|
22
|
+
# @return [String]
|
23
|
+
attr_accessor :title
|
24
|
+
|
25
|
+
# The description for this embed.
|
26
|
+
# @return [String]
|
27
|
+
attr_accessor :description
|
28
|
+
|
29
|
+
# The URL the title should point to.
|
30
|
+
# @return [String]
|
31
|
+
attr_accessor :url
|
32
|
+
|
33
|
+
# The timestamp for this embed. Will be displayed just below the title.
|
34
|
+
# @return [Time]
|
35
|
+
attr_accessor :timestamp
|
36
|
+
|
37
|
+
# @return [Integer] the colour of the bar to the side, in decimal form.
|
38
|
+
attr_reader :colour
|
39
|
+
alias_method :color, :colour
|
40
|
+
|
41
|
+
# Sets the colour of the bar to the side of the embed to something new.
|
42
|
+
# @param value [Integer, String, {Integer, Integer, Integer}] The colour in decimal, hexadecimal, or R/G/B decimal
|
43
|
+
# form.
|
44
|
+
def colour=(value)
|
45
|
+
if value.is_a? Integer
|
46
|
+
raise ArgumentError, 'Embed colour must be 24-bit!' if value >= 16_777_216
|
47
|
+
@colour = value
|
48
|
+
elsif value.is_a? String
|
49
|
+
self.colour = value.delete('#').to_i(16)
|
50
|
+
elsif value.is_a? Array
|
51
|
+
raise ArgumentError, 'Colour tuple must have three values!' if value.length != 3
|
52
|
+
self.colour = value[0] << 16 | value[1] << 8 | value[2]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
alias_method :color=, :colour=
|
57
|
+
|
58
|
+
# The footer for this embed.
|
59
|
+
# @example Add a footer to an embed
|
60
|
+
# embed.footer = Discordrb::Webhooks::EmbedFooter.new(text: 'Hello', icon_url: 'https://i.imgur.com/j69wMDu.jpg')
|
61
|
+
# @return [EmbedFooter]
|
62
|
+
attr_accessor :footer
|
63
|
+
|
64
|
+
# The image for this embed.
|
65
|
+
# @see EmbedImage
|
66
|
+
# @example Add a image to an embed
|
67
|
+
# embed.image = Discordrb::Webhooks::EmbedImage.new(url: 'https://i.imgur.com/PcMltU7.jpg')
|
68
|
+
# @return [EmbedImage]
|
69
|
+
attr_accessor :image
|
70
|
+
|
71
|
+
# The thumbnail for this embed.
|
72
|
+
# @see EmbedThumbnail
|
73
|
+
# @example Add a thumbnail to an embed
|
74
|
+
# embed.thumbnail = Discordrb::Webhooks::EmbedThumbnail.new(url: 'https://i.imgur.com/xTG3a1I.jpg')
|
75
|
+
# @return [EmbedThumbnail]
|
76
|
+
attr_accessor :thumbnail
|
77
|
+
|
78
|
+
# The author for this embed.
|
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]
|
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_reader :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 && @timestamp.utc.iso8601,
|
112
|
+
color: @colour,
|
113
|
+
footer: @footer && @footer.to_hash,
|
114
|
+
image: @image && @image.to_hash,
|
115
|
+
thumbnail: @thumbnail && @thumbnail.to_hash,
|
116
|
+
video: @video && @video.to_hash,
|
117
|
+
provider: @provider && @provider.to_hash,
|
118
|
+
author: @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
|
+
# Creates a new footer object.
|
128
|
+
# @param text [String, nil] The text to be displayed in the footer.
|
129
|
+
# @param icon_url [String, nil] The URL to an icon to be showed alongside the text.
|
130
|
+
def initialize(text: nil, icon_url: nil)
|
131
|
+
@text = text
|
132
|
+
@icon_url = icon_url
|
133
|
+
end
|
134
|
+
|
135
|
+
# @return [Hash] a hash representation of this embed footer, to be converted to JSON.
|
136
|
+
def to_hash
|
137
|
+
{
|
138
|
+
text: @text,
|
139
|
+
icon_url: @icon_url
|
140
|
+
}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# An embed's image will be displayed at the bottom, in large format. It will replace a footer icon URL if one is set.
|
145
|
+
class EmbedImage
|
146
|
+
# Creates a new image object.
|
147
|
+
# @param url [String, nil] The URL of the image.
|
148
|
+
def initialize(url: nil)
|
149
|
+
@url = url
|
150
|
+
end
|
151
|
+
|
152
|
+
# @return [Hash] a hash representation of this embed image, to be converted to JSON.
|
153
|
+
def to_hash
|
154
|
+
{
|
155
|
+
url: @url
|
156
|
+
}
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# An embed's thumbnail will be displayed at the right of the message, next to the description and fields. When clicked
|
161
|
+
# it will point to the embed URL.
|
162
|
+
class EmbedThumbnail
|
163
|
+
# Creates a new thumbnail object.
|
164
|
+
# @param url [String, nil] The URL of the thumbnail.
|
165
|
+
def initialize(url: nil)
|
166
|
+
@url = url
|
167
|
+
end
|
168
|
+
|
169
|
+
# @return [Hash] a hash representation of this embed thumbnail, to be converted to JSON.
|
170
|
+
def to_hash
|
171
|
+
{
|
172
|
+
url: @url
|
173
|
+
}
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# An embed's author will be shown at the top to indicate who "authored" the particular event the webhook was sent for.
|
178
|
+
class EmbedAuthor
|
179
|
+
# Creates a new author object.
|
180
|
+
# @param name [String, nil] The name of the author.
|
181
|
+
# @param url [String, nil] The URL the name should link to.
|
182
|
+
# @param icon_url [String, nil] The URL of the icon to be displayed next to the author.
|
183
|
+
def initialize(name: nil, url: nil, icon_url: nil)
|
184
|
+
@name = name
|
185
|
+
@url = url
|
186
|
+
@icon_url = icon_url
|
187
|
+
end
|
188
|
+
|
189
|
+
# @return [Hash] a hash representation of this embed author, to be converted to JSON.
|
190
|
+
def to_hash
|
191
|
+
{
|
192
|
+
name: @name,
|
193
|
+
url: @url,
|
194
|
+
icon_url: @icon_url
|
195
|
+
}
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# A field is a small block of text with a header that can be relatively freely layouted with other fields.
|
200
|
+
class EmbedField
|
201
|
+
# Creates a new field object.
|
202
|
+
# @param name [String, nil] The name of the field, displayed in bold at the top.
|
203
|
+
# @param value [String, nil] The value of the field, displayed in normal text below the name.
|
204
|
+
# @param inline [true, false] Whether the field should be displayed in-line with other fields.
|
205
|
+
def initialize(name: nil, value: nil, inline: nil)
|
206
|
+
@name = name
|
207
|
+
@value = value
|
208
|
+
@inline = inline
|
209
|
+
end
|
210
|
+
|
211
|
+
# @return [Hash] a hash representation of this embed field, to be converted to JSON.
|
212
|
+
def to_hash
|
213
|
+
{
|
214
|
+
name: @name,
|
215
|
+
value: @value,
|
216
|
+
inline: @inline
|
217
|
+
}
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: discordrb-webhooks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- meew0
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-29 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '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/meew0/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.1.0
|
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.6.10
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Webhook client for discordrb
|
63
|
+
test_files: []
|
64
|
+
has_rdoc:
|