Websites/app/logical/requests/discord_webhook.rb
2024-05-02 22:04:43 -05:00

46 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Requests
class DiscordWebhook
include HTTParty
base_uri "https://discord.com/api/webhooks"
attr_reader :id, :token
def initialize(id:, token:)
@id = id
@token = token
end
def execute(body)
r = self.class.post("/#{id}/#{token}?wait=false", {
body: body.to_json,
headers: {
"Content-Type" => "application/json",
},
})
Rails.logger.warn("Discord webhook failed: #{r.code} #{r.body}") unless r.success?
r
end
def edit(message_id, body)
r = self.class.patch("/#{id}/#{token}/#{message_id}?wait=false", {
body: body.to_json,
headers: {
"Content-Type" => "application/json",
},
})
Rails.logger.warn("Discord webhook edit failed: #{r.code} #{r.body}") unless r.success?
r
end
def delete
self.class.delete("/#{id}/#{token}")
end
def self.is_deleted(response)
response.code == 404 && JSON.parse(response.body)["code"] == 10_015
end
end
end