Requires Ruby 2.3 or later
A light wrapper around sending Discord webhooks and embeds.
Add this line to your application's Gemfile:
gem 'discord-notifier'
And then execute:
$ bundle
You can set defaults for Discord Notifier to use. For descriptions of what the fields do, refer to the Discord webhook documentation.
Discord::Notifier.setup do |config|
config.url = 'WEBHOOK_URL'
config.username = 'My Webhook Username'
config.avatar_url = ''
# Defaults to `false`
config.wait = true
end
If you're using Rails, place this in config/initializers/discord_notifier.rb
.
To send a standard string message, use
Discord::Notifier.message('Discord Notifier Webhook Notification')
To create and send an embed, use
embed = Discord::Embed.new do
title "Discord Ruby Notification"
author name: "Webhook Bot",
avatar_url: 'AUTHOR_AVATAR'
footer text: 'Notification via DiscordNotifier Gem',
icon_url: 'ICON_URL'
end
Discord::Notifier.message(embed)
For a list of embed fields and parameters you can use, refer to the official documentation. Please note that not all embed types and fields are supported through webhooks. You can read more about the exceptions here.
This gem provides the following API methods when creating an Embed:
Needs to be a hex string, ex #008000
Needs to be a 24 bit integer (ex, 0x008000
)
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation
Takes a hash of fields as specified in the Discord documentation. Each successive call adds a new field to the embed object.
To send an attachment, pass a File
object as a message.
image = File.open('attachment.png')
Discord::Notifier.message image
If you want to override the default configuration, you can pass new values when sending a webhook notification.
Discord::Notifier.message 'Custom URL and Avatar',
url: 'NEW_WEBHOOK_URL',
avatar_url: 'SECOND_AVATAR_URL'
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.