Last active
March 22, 2018 01:48
-
-
Save dolpen/5e490276a63d27dafc8cf9dc00962624 to your computer and use it in GitHub Desktop.
GAS -> Discord にハイカラニュース流す奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webhook = (function(){ | |
var DiscordWebhook = function(id, token){ | |
this.id = id; | |
this.token = token; | |
}; | |
DiscordWebhook.prototype = { | |
_endpoint : function(){return 'https://discordapp.com/api/webhooks/'+this.id+'/'+this.token;}, | |
send : function(content){ | |
UrlFetchApp.fetch(this._endpoint(),{'method' : 'post','payload' : {'content' : content}}); | |
} | |
}; | |
return new DiscordWebhook( | |
'YOUR_DISCORD_WEBHOOK_ID', | |
'YOUR_DISCORD_WEBHOOK_TOKEN' | |
); | |
})(); | |
var info = (function(){ | |
var endpoint = 'https://splatoon.ink/schedule.json'; | |
var CityInfo = function(){ | |
this.cached = null; | |
}; | |
CityInfo.prototype = { | |
_fetch : function(){ | |
var resp = UrlFetchApp.fetch(endpoint,{'method' : 'get'}); | |
var data = JSON.parse(resp.getContentText()); | |
var now = (new Date()).getTime(); | |
return data.schedule.filter(function(item){ | |
return item.startTime <= now && item.endTime >= now; | |
})[0]; | |
}, | |
get : function(){ | |
if(this.cached != null)return this.cached; | |
var message = ''; | |
var info = this._fetch(); | |
if(!info){ | |
message += 'ニュースの取得に失敗しました' | |
} else { | |
message += 'レギュラーは ' + info.regular.maps[0]['nameJP'] + ' と ' + info.regular.maps[1]['nameJP'] + ' だよ。\n'; | |
message += 'ガチマッチは ' + info.ranked.maps[0]['nameJP'] + ' と ' + info.ranked.maps[1]['nameJP'] + ' の ' + info.ranked['rulesJP'] + ' だよ。\n'; | |
message += 'イカ、よろしくー'; | |
} | |
this.cached = message; | |
return message; | |
} | |
}; | |
return new CityInfo(); | |
})(); | |
function InkopolisNews() { | |
var news = info.get(); | |
webhook.send(news); | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment