動機
- なぜGAS?
- いろいろ簡単だった
- 環境構築不要
- JavaScript
- 日時で動かせる
- 無料
- いろいろ簡単だった
- なぜベイスターズ?
- 弱かった
- 新監督
- 戦力の流出
今年はなぜかオリックスが強かった
- 弱かった
流れ
ベイスターズの試合の日、かつ敗北した日に開幕から現在までの勝敗をカウントする
↓
試合スケジュールはGoogle Calenderから取得
↓
正規表現で勝ち、負け、引き分け、試合中止を出すカウントする
↓
結果をdiscordで教える
ベイスターズの試合日
Googleカレンダーがあるのでそれを拝借
https://calendar.google.com/calendar/u/0/embed?src=bnBiXzNfJTU5b2tvaGFtYSslNDRlJTRlJTQxKyU0MmF5JTUzdGFycyNzcG9ydHNAZ3JvdXAudi5jYWxlbmRhci5nb29nbGUuY29t
カレンダーを読み込む
CalendarApp.getCalendarById(id)
で読み込み、getEvents(開幕日から今日)
で回す
const bsCalId = "npb_3_%59okohama+%44e%4e%41+%42ay%53tars#sports@group.v.calendar.google.com"
const bsCal = CalendarApp.getCalendarById(id)
const today = new Date()
bsCal.getEvents(new Date("2021-03-26 00:00:00"), today).map(l => {
if (gameResult(l.getTitle()) === "W") {
countWin++
} else if (gameResult(l.getTitle()) === "L") {
countLose++
}
})
試合結果を正規表現でごにょごにょする
試合前
BayStars @ Swallows
試合後
BayStars (33) @ Swallows (4)
で記載されるので、
WDLN
の4つのどれかを返す
// in : BayStars (33) @ Swallows (4)
// out : W D L N
function gameResult (game) {
const bsSocre = game.replace(/.*BayStars \(([0-9]*)\).*/g,"$1")
const otherSocre = game.replace(/BayStars \([0-9]*\)/g,"").replace(/.*\(([0-9]*)\).*/g,"$1")
// 試合中止
if (game === bsSocre || game === otherSocre) {
return "N"
} else if (bsSocre - otherSocre > 0) {
return "W"
} else if (bsSocre - otherSocre < 0) {
return "L"
}
return "D"
}
GASからdiscordにpost
qiitaにいっぱい載ってるので略
割と簡単にできた
function post(url, message) {
const payload = {
'content' : message,
'parse' : 'full',
}
const params = {
'method' : 'post',
'payload' : payload,
'muteHttpExceptions': true,
}
UrlFetchApp.fetch(url, params)
}
GASを日時で動かす
トリガー
↓
トリガーを追加
↓
実行する関数を選択、時間ベースのトリガーのタイプを選択(時間を選択)