How to Write a Discord Bot in Ruby

in #radiator7 years ago (edited)

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

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

  1. Request a new bot here: https://discordapp.com/developers/applications/me#top
  2. Register an application and create an app bot user.
  3. 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
  4. Give that URL to the Discord server/guild owner and have them authorize the bot.
  5. Set the token and client_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

Sort:  

Added this to the constructor: prefix: '$'

bot = Cosgrove::Bot.new(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

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.