Skip to content

Instantly share code, notes, and snippets.

@funwarioisii
Created June 5, 2020 13:44
Show Gist options
  • Save funwarioisii/54caa8e974c2bc09592cbb3e8c887484 to your computer and use it in GitHub Desktop.
Save funwarioisii/54caa8e974c2bc09592cbb3e8c887484 to your computer and use it in GitHub Desktop.
Splatoon2のdiscord bot
require 'discordrb'
require 'dotenv'
require 'net/http'
require 'json'
require 'time'
Dotenv.load
bot = Discordrb::Commands::CommandBot.new(
token: ENV["BOT_TOKEN"],
client_id: ENV["CLIENT_ID"],
prefix: '/'
)
bot.command :syake do |event, *args|
coop_uri = URI("https://spla2.yuu26.com/coop/schedule")
coop_info = JSON.parse(Net::HTTP.get_response(coop_uri).body.to_s)
coop_start = Time.parse(coop_info["result"][0]["start"])
coop_end = Time.parse(coop_info["result"][0]["end"])
coop_stage = coop_info["result"][0]["stage"]
coop_weapons = coop_info["result"][0]["weapons"]
coop = <<~INFO
[鮭]
時間: #{coop_start.month}#{coop_start.day}#{coop_start.hour}:00 ~ #{coop_end.month}#{coop_end.day}#{coop_end.hour}
#{coop_stage["name"]}
#{coop_weapons.map{|w| w["name"]}.join ', '}
INFO
battle_uri = URI("https://spla2.yuu26.com/schedule")
battle_info = JSON.parse(Net::HTTP.get_response(battle_uri).body.to_s)
nawabari = "[ナワバリ]\n"
nawabari += battle_info["result"]["regular"][0..2].map {|b| <<~NAWABARI
ステージ:#{b["maps"].join(',')}
時間:#{Time.parse(b["start"]).hour}:00 ~
NAWABARI
}.join "\n"
gachi = "[ガチマ]\n"
gachi += battle_info["result"]["gachi"][0..2].map {|b| <<~GACHI
ステージ:#{b["maps"].join(",")}
ルール:#{b["rule"]}
時間:#{Time.parse(b["start"]).hour}:00 ~
GACHI
}.join "\n"
league = "[リグマ]\n"
league += battle_info["result"]["league"][0..2].map {|b| <<~LEAGUE
ステージ:#{b["maps"].join(",")}
ルール:#{b["rule"]}
時間:#{Time.parse(b["start"]).hour}:00 ~
LEAGUE
}.join "\n"
if args.size == 0
info = "#{coop}\n#{nawabari}\n#{gachi}\n#{league}"
event.send_message(info)
return
end
if args.include? "ガチマ"
event.send_message gachi
end
if args.include? "リグマ"
event.send_message league
end
if args.include? "ナワバリ"
event.send_message nawabari
end
if args.include? "鮭"
event.send_message coop
end
end
bot.run
@funwarioisii
Copy link
Author

Rubyの習作に作った
なんか最後のifが超雑なんだけど、複数引数に対応していい感じに返すやつあったら教えてください

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment