-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (93 loc) · 2.25 KB
/
main.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
package main
import (
"github.com/robfig/cron"
"fmt"
"github.com/bwmarrin/discordgo"
"errors"
"log"
"net/http"
"os"
scraper "Discord-Bot-CP-Spider/scraper"
)
var (
token string
contests []scraper.Contest
)
func getToken() (string, error) {
t := os.Getenv("BOT_TOKEN")
if t == "" {
return "", errors.New("Invalid Token")
}
return t, nil
}
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":3000"
}
func main() {
c := cron.New()
contests = scraper.Scrape()
c.AddFunc("0,30 * * * *", func() {
contests = scraper.Scrape()
})
c.Start()
defer c.Stop()
token, err := getToken()
port := getPort()
if err != nil {
fmt.Println(err.Error())
return
}
dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println(err.Error())
return
}
dg.AddHandler(messageHandler)
err = dg.Open()
defer dg.Close()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Bot is Running")
log.Fatal(http.ListenAndServe(port, nil))
}
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if m.Content == "!schedule" {
s.ChannelMessageSend(m.ChannelID, "**Here's Your Contests List:**")
for _, body := range contests {
var baseColor int
switch body.Status {
case "PAST":
baseColor = 16711680
case "RUNNING":
baseColor = 16776960
case "COMING":
baseColor = 65280
}
embed := new(discordgo.MessageEmbed)
embed.URL = body.Link
embed.Color = baseColor
embed.Title = body.Title + "\n"
embed.Description = "**(" + body.Status + ")**"
inlineFields := []*discordgo.MessageEmbedField{
{Name: "Duration", Value: body.Duration, Inline: true},
{Name: "Time Left", Value: body.Timeleft, Inline: true},
}
embed.Fields = inlineFields
s.ChannelMessageSendEmbed(m.ChannelID, embed)
}
} else if m.Content == "!help" {
commandList := "**Here's List of Commands:**\n\n"
commandList += "**1. !help (to see list of commands)**\n"
commandList += "**2. !schedule (to see contests list)**\n"
s.ChannelMessageSend(m.ChannelID, commandList)
}
}