LoginSignup
6
3

More than 5 years have passed since last update.

DiscordのBotにSplatoon2のステージ情報を教えてもらう

Last updated at Posted at 2018-01-18

Splatoon2のステージ情報を取得するDiscordのBotを作りました。

をそれぞれ使わせていただきました。

作ったもの

現在のステージ情報と次のステージ情報を教えてくれるBotです。
今回は.stgという発言に反応して教えてもらうようにしました。

イメージはこんな感じ。
やってることはSpla2 APIを叩いて、返ってきたものをちょっと加工してメッセージとして送信させているだけです。

スクリーンショット 2018-01-18 22.43.01.png

導入とか

Botの登録やdiscordrbのインストールの仕方、簡単なBotの作り方は省略します。

ソースコード

ソースコードはこんな感じ。
'User-Agent'のところはそれぞれご自身について書き換えてください。
形式は問わないとのことです。

Spla2 API
プログラムに組み込む際は、User-Agent をなるべく設定してください。
形式は問いませんが、連絡先が分かる形式だと嬉しいです。
(例: Ikabot/1.0 (twitter @m_on_yu) IkaGirl/2.0 (+https://example.com/) など)

bot.rb
require 'faraday'
require 'json'

bot = Discordrb::Commands::CommandBot.new token: '自分のトークン', client_id: 自分のクライアントID, prefix: '.'

times = [['現在', 'now'], ['次', 'next']]
rules = [['レギュラーマッチ', 'regular'], ['ガチマッチ', 'gachi'], ['リーグマッチ', 'league']]

bot.command(:stg) do |event|
  message = times.inject('') do |msg, (time_ja, time_en)|
    msg + "#{time_ja}のステージ情報\n" +
    rules.reduce("```\n") do |detail, (rule_ja, rule_en)|
      detail + "#{rule_ja} : #{call_splat_api(rule_en, time_en)}\n"
    end + '```'
  end
  event.send_message(message)
end

def self.call_splat_api(rule, time)
  conn = Faraday.new('https://spla2.yuu26.com')
  response = conn.get do |req|
    req.url "/#{rule}/#{time}"
    req.headers['User-Agent'] = 'Roboty (Twitter: @otuhs_d)'
  end
  if response.success?
    result = JSON.parse(response.body)['result'][0]
    result['maps'].inject("#{result['rule']}\n") { |msg, map| msg + "* #{map}\n" }
  else
      '情報の取得に失敗しました'
  end
end

bot.run

Gemfileにgem 'faraday'の追加も忘れずに。

Gemfile
gem 'discordrb'
gem 'faraday'

おわりに

Spla2 APIではサーモンランの情報も提供されているので、同様にして作ってみてはいかがでしょうか。
Botを常時動かしたい場合は

を一例にどうぞ(ダイレクトマーケティング)

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3