-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.js
73 lines (61 loc) · 2.83 KB
/
fixtures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { Command } = require('discord.js-commando')
const { RichEmbed } = require('discord.js')
const _ = require('lodash')
const moment = require('moment-timezone')
const fplapi = require('./../../fpl/api')
module.exports = class FixturesCommand extends Command {
constructor(client) {
super(client, {
group: 'fpl',
name: 'fixtures',
memberName: 'fixtures',
description: 'Get the latest fixtures in GMT+8 time',
args: [{
key: 'gameweek',
prompt: 'What is the game week ?',
type: 'integer',
default: ''
}]
})
}
run(message, {gameweek}) {
const timezone = process.env.BOT_TIMEZONE
Promise.all([fplapi.events(), fplapi.teams()]).then((responses) => {
const teams = responses[1]
const weeks = responses[0]
if(!gameweek)
gameweek = _.filter(weeks, function (week) {
return (week.is_current && !week.finished) || week.is_next
})[0].id
fplapi.fixtures(gameweek).then((response) => {
let fixtures = response.data,
gameStr = '', teamGames = []
fixtures.forEach((fixture) => {
var matchedTeams = {}
for (var i = 0; i < teams.length; i++) {
if (teams[i].id === fixture.team_h)
matchedTeams.home = teams[i].short_name
else if (teams[i].id === fixture.team_a)
matchedTeams.away = teams[i].short_name
if (matchedTeams.away && matchedTeams.home)
break
}
gameStr += `**${matchedTeams.home}** vs **${matchedTeams.away}**\n${moment(fixture.kickoff_time).tz(timezone).format('hh:mm A DD MMM YYYY')}\n\n`
teamGames.push(matchedTeams.home, matchedTeams.away)
})
let doubleGameTeams = _.filter(teamGames, (val, i, iteratee) => _.includes(iteratee, val, i + 1));
if (doubleGameTeams.length)
gameStr += `**Double Game Teams : \n${doubleGameTeams.join(', ')}**\n\n`
const embed = new RichEmbed()
.setTitle(`Game Week ${gameweek}`)
.setColor(65415)
.setFooter('fantasy.premierleague.com', 'https://fantasy.premierleague.com/static/libsass/plfpl/dist/img/facebook-share.png')
.addField('----------------------', gameStr)
return message.channel.send(embed)
}).catch((error) => {
console.log(error)
return message.reply("something occurred. Please try again later.")
})
})
}
}