Module: OnyxCord::REST::Webhook

Defined in:
lib/onyxcord/rest/routes/webhook.rb

Overview

API calls for Webhook object

Class Method Summary collapse

Class Method Details

.attachment_payload(attachments) ⇒ Object

Build attachment metadata payload for multipart uploads. Returns an array of { id:, filename: } hashes.



11
12
13
# File 'lib/onyxcord/rest/routes/webhook.rb', line 11

def attachment_payload(attachments)
  OnyxCord::MessagePayload.attachment_payload(attachments)
end

.delete_webhook(token, webhook_id, reason = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/onyxcord/rest/routes/webhook.rb', line 106

def delete_webhook(token, webhook_id, reason = nil)
  OnyxCord::REST.request(
    :webhooks_wid,
    webhook_id,
    :delete,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}",
    Authorization: token,
    'X-Audit-Log-Reason': reason
  )
end

.multipart_body(body, attachments) ⇒ Object

Build multipart body with named file fields and JSON payload.



16
17
18
# File 'lib/onyxcord/rest/routes/webhook.rb', line 16

def multipart_body(body, attachments)
  OnyxCord::MessagePayload.multipart_body(body, attachments)
end

.token_delete_message(webhook_token, webhook_id, message_id) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/onyxcord/rest/routes/webhook.rb', line 168

def token_delete_message(webhook_token, webhook_id, message_id)
  OnyxCord::REST.request(
    :webhooks_wid_messages,
    webhook_id,
    :delete,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}/messages/#{message_id}"
  )
end

.token_delete_webhook(webhook_token, webhook_id, reason = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/onyxcord/rest/routes/webhook.rb', line 119

def token_delete_webhook(webhook_token, webhook_id, reason = nil)
  OnyxCord::REST.request(
    :webhooks_wid,
    webhook_id,
    :delete,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}",
    'X-Audit-Log-Reason': reason
  )
end

.token_edit_message(webhook_token, webhook_id, message_id, content = nil, embeds = nil, allowed_mentions = nil, components = nil, attachments = nil, flags = nil, poll = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/onyxcord/rest/routes/webhook.rb', line 142

def token_edit_message(webhook_token, webhook_id, message_id, content = nil, embeds = nil, allowed_mentions = nil, components = nil, attachments = nil, flags = nil, poll = nil)
  components = OnyxCord::MessageComponents.payload(components) unless components.nil?
  flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components)
  OnyxCord::MessagePayload.validate!(content: content, embeds: embeds, components: components, flags: flags, attachments: attachments, poll: poll)
  body = OnyxCord::MessagePayload.edit_body(content, embeds).merge({ allowed_mentions: allowed_mentions, components: components, attachments: attachments ? attachment_payload(attachments) : nil, flags: flags, poll: poll }.compact)

  body = if attachments
           multipart_body(body, attachments)
         else
           body.to_json
         end

  headers = { content_type: :json } unless attachments

  OnyxCord::REST.request(
    :webhooks_wid_messages,
    webhook_id,
    :patch,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}/messages/#{message_id}",
    body,
    headers
  )
end

.token_execute_webhook(webhook_token, webhook_id, wait = false, content = nil, username = nil, avatar_url = nil, tts = nil, file = nil, embeds = nil, allowed_mentions = nil, flags = nil, components = nil, attachments = nil, poll = nil) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/onyxcord/rest/routes/webhook.rb', line 45

def token_execute_webhook(webhook_token, webhook_id, wait = false, content = nil, username = nil, avatar_url = nil, tts = nil, file = nil, embeds = nil, allowed_mentions = nil, flags = nil, components = nil, attachments = nil, poll = nil)
  raise ArgumentError, 'cannot mix file and attachments' if file && attachments

  components = OnyxCord::MessageComponents.payload(components) unless components.nil?
  flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components)
  OnyxCord::MessagePayload.validate!(content: content, embeds: embeds, components: components, flags: flags, attachments: attachments, poll: poll)
  body = { content: content, username: username, avatar_url: avatar_url, tts: tts == true ? true : nil, embeds: embeds&.map(&:to_hash), allowed_mentions: allowed_mentions, flags: flags, components: components&.any? ? components : nil, attachments: attachments ? attachment_payload(attachments) : nil, poll: poll }.compact

  body = if file
           { file: file, payload_json: body.to_json }
         elsif attachments
           multipart_body(body, attachments)
         else
           body.to_json
         end

  headers = { content_type: :json } unless file || attachments
  with_components = components&.any? || nil
  query = URI.encode_www_form({ wait: wait, with_components: with_components }.compact)

  OnyxCord::REST.request(
    :webhooks_wid,
    webhook_id,
    :post,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}?#{query}",
    body,
    headers
  )
end

.token_get_message(webhook_token, webhook_id, message_id) ⇒ Object

Get a message that was created by the webhook corresponding to the provided token. https://discord.com/developers/docs/resources/webhook#get-webhook-message



131
132
133
134
135
136
137
138
# File 'lib/onyxcord/rest/routes/webhook.rb', line 131

def token_get_message(webhook_token, webhook_id, message_id)
  OnyxCord::REST.request(
    :webhooks_wid_messages_mid,
    webhook_id,
    :get,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}/messages/#{message_id}"
  )
end

.token_update_webhook(webhook_token, webhook_id, data, reason = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/onyxcord/rest/routes/webhook.rb', line 92

def token_update_webhook(webhook_token, webhook_id, data, reason = nil)
  OnyxCord::REST.request(
    :webhooks_wid,
    webhook_id,
    :patch,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}",
    data.to_json,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.token_webhook(webhook_token, webhook_id) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/onyxcord/rest/routes/webhook.rb', line 34

def token_webhook(webhook_token, webhook_id)
  OnyxCord::REST.request(
    :webhooks_wid,
    nil,
    :get,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}/#{webhook_token}"
  )
end

.update_webhook(token, webhook_id, data, reason = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/onyxcord/rest/routes/webhook.rb', line 77

def update_webhook(token, webhook_id, data, reason = nil)
  OnyxCord::REST.request(
    :webhooks_wid,
    webhook_id,
    :patch,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}",
    data.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.webhook(token, webhook_id) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/onyxcord/rest/routes/webhook.rb', line 22

def webhook(token, webhook_id)
  OnyxCord::REST.request(
    :webhooks_wid,
    nil,
    :get,
    "#{OnyxCord::REST.api_base}/webhooks/#{webhook_id}",
    Authorization: token
  )
end