How to Write a Discord Bot in Ruby
Previously, I posted a framework that can be used to bridge the STEEM blockchain with Discord. Basically, you can use it to write your own Discord bot. And that's what @banjo is.
But if you're not that familiar with writing a Discord bot in Ruby, this how-to might be helpful to you. So here's how you can go about it.
First, you'll need to prepare your workstation to do development on a stable version of ruby
.
Install Ruby
- macOS - If you're on macOS, you can use this article to get up and running. Once you get to Step 4, you can skip the rest and return to this article: How To Configure Your Mac to do Ruby on Rails Development
- Windows - I don't recommend Windows, but it might work. I document how to get
drphil
to run on Windows (drphil.rb - Voting Bot - Windows Installation). Not sure if it's that helpful, but if you can follow those steps, you can doruby
development on Windows. The commands below are forbash
prompts, but they are usually the same in the Microsoft command line. Alternative, you could use @bitcoinparadise's guide, How to install Ubuntu on Windows 10, then use the Linux install steps. - Linux - I like
rvm
for providingruby
tools. You can install it from (rvm.io)[https://rvm.io/]. Alternatively, if you're not that familiar with Linux, you can use @orionsbelt's guide, Tutorial-How to run Luckyluke bot.- If you go the
rvm
route, run:rvm install ruby-2.2.5
source ~/.rvm/scripts/rvm
rvm use ruby-2.2.5
rvm default use ruby-2.2.5
- If you go the
Create a New Project
We're just going to create an empty folder for this. I'm assuming you know how to do this, but just in case:
$ mkdir mybot
$ cd mybot
Also make sure you have bundler
:
$ gem install bundler
As well, get imagemagick
and git
:
Linux
$ sudo apt-get install git imagemagick libmagickcore-dev libmagickwand-dev
Mac
$ sudo port install git imagemagick
Create a Gemfile
The Gemfile
is for organizing all of the libraries used by the bot.
source 'https://rubygems.org'
gem 'cosgrove'
Once this file exists, you can install the libraries:
$ bundle install
Add a config file called config.yml
:
:cosgrove:
:token:
:client_id:
:secure: set this
:upvote_weight: upvote_rules
:upvote_rules:
:channels:
:default:
:upvote_weight: 50.00 %
:general_text:
:channel_id: <Your Favorite Channel ID>
:upvote_weight: 100.00 %
:chain:
:steem_account:
:steem_posting_wif:
:golos_account:
:golos_posting_wif:
:steem_api_url: https://steemd.steemit.com
:golos_api_url: https://ws.golos.io
:discord:
:log_mode: info
You will need to request a token
and client_id
from Discord (see below).
Provide the accounts and wif
private postings keys if you want your bot to upvote posts.
You should change the secure
key using the output of SecureRandom
. Use ruby
to generate this key:
$ ruby -rrubygems -rbundler/setup -e "Bundler.require; puts ' :secure: ' + SecureRandom.hex(32)"
Bot Registration
- Request a new bot here: https://discordapp.com/developers/applications/me#top
- Register an
application
and create anapp bot user
. - Replace
APP_CLIENT_ID
with the App's Client ID in this URL: https://discordapp.com/oauth2/authorize?&client_id=APP_CLIENT_ID&scope=bot&permissions=153600 - Give that URL to the Discord server/guild owner and have them authorize the bot.
- Set the
token
andclient_id
in your bot config.
Now create a file called bot.rb
:
require 'rubygems'
require 'bundler/setup'
Bundler.require
bot = Cosgrove::Bot.new(prefix: '$')
bot.message(with_text: 'Ping!') do |event|
event.respond 'Pong!'
end
bot.run
And finally, to run the bot, type:
$ ruby bot.rb
While the bot is running this way, you can use any of the commands available to Cosgrove from Discord. The default commands are:
$slap [target] - does a slap on the target
$verify <account> [chain] - check account association with Discord users (chain default steem)
$register <account> [chain] - associate account with your Discord user (chain default steem)
$upvote [url] - upvote from bot; empty or ^ to upvote last steemit link
Also see: Introducing: Cosgrove - A STEEM Centric Discord Bot Framework
Added this to the constructor:
prefix: '$'
I forgot to add this to the original tutorial.
Where do i need to add this??
I'm really interested in this subject, resteeming to try it later =D
my laptop runs on window ten
As well
This is great @inertia Thanks for giving us this.. I am going to try to build one and see what happens
hiii @inertia, wonderful plan, nice work you have done, thanks for sharing that kind of helpful article.,,,, FOLLOW,,,,.
i just got in to discord. and I'm having a hard time using it.
Hm, I don't listen to them.
Great content
Nice tutorial....i love it.