Post Data to Discord Webhooks

Resource Post Data to Discord Webhooks 1.0

Luka S.J.

Wastage of Time
Member
Joined
Mar 27, 2017
Posts
98
Luka S.J. submitted a new resource:

Post Data to Discord Webhooks - If you ever feel like annoying your server

Hidden Text



Hidden Text

...

Read more about this resource...
 

Nuri Yuri

.
Member
Joined
Sep 18, 2017
Posts
39
Interesting script :)
I wouldn't have thought about this :q

For those who use a normal Ruby version I made a translation of the script :p
Ruby:
require 'json'
require 'net/http'

ENV['SSL_CERT_FILE'] ||= './lib/cert.pem' if $0 == 'Game.rb' # Launched from PSDK

module DiscordWebhooks
  DISCORD_WEBHOOK_URI = URI('<INSERT WEBHOOK URL HERE!>')
 
  module_function
 
  # @overload post(data)
  #   Post data to the default DISCORD_WEBHOOK_URI
  #   @param data [Hash] the webhook data
  # @overload post(url, data)
  #   Post data to a Discord Webhook using an URL or an URI
  #   @param url [URI, String] the Discord Webhook
  #   @param data [Hash] the webhook data
  def post(url, data = url)
    url = DISCORD_WEBHOOK_URI if url.is_a?(Hash)
    url = URI(url) if url.is_a?(String)
    Net::HTTP.post(url, data.to_json, "Content-Type" => "application/json")
  end
end
It works the same way (you can also define a default URL so you just call DiscordWebhooks.post(hash) since the game is "supposed" to only communicate with one server).

I tested with this :
Ruby:
DiscordWebhooks.post(
  username: 'Nuri Yuri',
  avatar_url: 'https://yt3.ggpht.com/-ZQxRItl-UQA/AAAAAAAAAAI/AAAAAAAAAIc/OoeMfVC1D0E/s88-mo-c-c0xffffffff-rj-k-no/photo.jpg',
  content: 'Coucou :)',
  embeds: [
    {
      color: 0x961696, # Asking decimal but no need to convert when using 0x :)
      author: {
        name: 'Chat-Tan',
        url: 'https://pokemonworkshop.fr/',
        icon_url: 'https://cdn.discordapp.com/app-icons/186157007500345345/c695c6232a85260b7016684a12773f35.png'
      },
      title: 'Your Game',
      url: 'http://resources.luka-sj.com',
      description: 'Yuri sucessfully traded its [Pikachu](https://www.pokepedia.fr/Pikachu) for a [Meowth](https://www.pokepedia.fr/Miaouss).'
    }
  ]
)

The result is the following on Discord :
 

Luka S.J.

Wastage of Time
Member
Joined
Mar 27, 2017
Posts
98
Having access to all of these standard scripts/modules sure makes life more convenient. Your solution is much more elegant. I might look into the "pbPostData" and HTTP request handlers in Essentials to see if it's possible to make this more standalone. cURL was that "hacky" solution to get the job done with minimal effort (though I had to quickly come up with a proper Hash to JSON parser).
 

Nuri Yuri

.
Member
Joined
Sep 18, 2017
Posts
39
Yep.

If you work on the pbPostData make sure you fix the \r\n issue (actually it's only \n and since the HTTP standard specify that field separator is \r\n some servers just respond with 400 - Bad Request).
 
Top