💎

[使ってみた] discorb + Heroku + GitHub

2021/09/20に公開

概要

  • discorb is a Discord API wrapper for Ruby.

https://github.com/discorb-lib/discorb

使用するサービス

まず、上記サービスのアカウントを用意してください。

GitHubRepositoryを作る

  1. New -> Repository name -> MyFirstDiscorbBot -> Public -> Create repository
echo "# MyFirstDiscorbBot" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/kaiyu-tech/MyFirstDiscorbBot.git
git push -u origin main
Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

ruby "~> 3.0"

gem "discorb"
bundle install

bundle lock --add-platform x86_64-linux
main.rb
require "discorb"

client = Discorb::Client.new

client.once :ready do
  puts "Logged in as #{client.user}"
end

client.on :message do |message|
  next if message.author.bot?
  next unless message.content == "/ping"

  message.channel.post("pong!")
end

client.run(ENV["DISCORD_BOT_TOKEN"])
Procfile
worker: bundle exec ruby main.rb

Discord Developer PortalBot Acountを作る

  1. Applications -> New Application -> NAME -> MyFirstDiscorbBot -> Create
  2. Bot -> Add Bot -> Yes, do it!
  3. OAuth2 -> [x]bot -> [x]applications.commands -> Copy
  4. MyFirstDiscorbBotをサーバーに招待

HerokuApplicationを作る

  1. Create new app -> App name -> my-first-discorb-bot -> Create app
  2. Deploy -> GitHub
  3. Setting -> Reveal Config Vars -> KEY: DISCORD_BOT_TOKEN -> VALUE: ※1 -> Add

※1 Discord Developer Portal -> Bot -> TOKEN -> Copy

  1. Deployment method -> GitHub -> App connected to GitHub
  2. App connected to GitHub -> repo-name -> MyFirstDiscorbBot -> Connect
  3. Automatic deploys -> main -> Enable Automatic Deploys
brew install heroku/brew/heroku

heroku login --interactive
heroku ps:scale worker=1 -a my-first-discorb-bot

これで招待したサーバーのBotが動作します。

Discussion