-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esports.go
106 lines (88 loc) · 2.78 KB
/
esports.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
var (
esportsUpdateLOLCommand = time.Now()
esportsUpdateVALCommand = time.Now()
)
func EsportsCommand(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
options := interaction.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
game := optionMap["game"].StringValue()
if optionMap["update"] != nil {
if !hasPermissions(interaction) {
return
}
update := optionMap["update"].BoolValue()
if update {
now = time.Now()
tomorrow = time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
var err error
switch game {
case "lol":
if time.Since(esportsUpdateLOLCommand) < time.Duration(5*time.Minute) {
respondWithMessage(interaction.Interaction, "Upcoming LOL games was updated recently, wait 5 minutes.")
return
}
err = updateLOLEsportsData()
case "val":
if time.Since(esportsUpdateVALCommand) < time.Duration(5*time.Minute) {
respondWithMessage(interaction.Interaction, "Upcoming VAL games was updated recently, wait 5 minutes.")
return
}
err = updateVALEsportsData()
default:
respondWithMessage(interaction.Interaction, "Game not found.")
return
}
if err != nil {
client.logger.Error(fmt.Sprintf("Error updating %v data: %v", strings.ToUpper(game), err))
respondWithMessage(interaction.Interaction, fmt.Sprintf("An error occured updating %v data.", strings.ToUpper(game)))
return
}
saveEsportsFile()
respondWithMessage(interaction.Interaction, "Updated data without any errors.")
return
}
}
switch game {
case "lol":
respondWithEmbed(interaction.Interaction, []*discordgo.MessageEmbed{createLOLMessageEmbed()})
case "val":
respondWithEmbed(interaction.Interaction, []*discordgo.MessageEmbed{createVALMessageEmbed()})
default:
respondWithMessage(interaction.Interaction, "Game not found.")
}
}
func updateEsportsData() {
now = time.Now()
tomorrow = time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
err := updateLOLEsportsData()
if err != nil {
client.logger.Error(fmt.Sprintf("error updating LOL data: %v", err))
}
err = updateVALEsportsData()
if err != nil {
client.logger.Error(fmt.Sprintf("error updating VAL data: %v", err))
}
esports.LastUpdateTimestamp = time.Now()
saveEsportsFile()
}
func postEsportsData() {
err := postLOLEsportsEmbed()
if err != nil {
client.logger.Error(fmt.Sprintf("error sending LOL embed: %v", err))
}
err = postVALEsportsEmbed()
if err != nil {
client.logger.Error(fmt.Sprintf("error sending VAL embed: %v", err))
}
esports.LastPostTimestamp = time.Now()
}