generated from gogolcorp/go-yave
/
main.go
108 lines (100 loc) · 3.01 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
103
104
105
106
107
108
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/blyndusk/cofy/app/commands"
"github.com/blyndusk/cofy/app/core"
"github.com/blyndusk/cofy/app/helpers"
"github.com/blyndusk/cofy/app/middlewares"
"github.com/blyndusk/cofy/app/services"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
func init() {
// get Discord token
Token := helpers.EnvVar("BOT_TOKEN")
// create a variable named "t", as the bot token
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
}
func main() {
pingApi()
setupSession()
}
func pingApi() {
type Ping struct {
Message string
}
var ping Ping
// get /ping route
resp, err := http.Get(fmt.Sprintf("%s/ping", core.ApiUrl))
helpers.ExitOnError("error while connecting through API", err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &ping)
// if GET /ping => "pong"
if ping.Message == "pong" {
log.Info("[API] :: UP :: [API]")
}
}
func setupSession() {
token := os.Getenv("BOT_TOKEN")
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + token)
helpers.ExitOnError("error creating Discord session,", err)
// Register the messageCreate func as a callback for MessageCreate events.
dg.AddHandler(messageCreationHandler)
dg.AddHandler(MessageReactionHandler)
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
helpers.ExitOnError("Error while connection,", err)
// Wait here until CTRL-C or other term signal is received.
log.Info("[BOT] :: UP :: [BOT]")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
dg.Close()
}
func messageCreationHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// if the message author is the bot itself, return to avoid infinite loops
if m.Author.ID == s.State.User.ID {
return
}
// if the message is not a command
if !strings.HasPrefix(m.Content, core.Prefix) {
// systematically get user each time someone create a message
user := middlewares.GetUser(s, m.Author.ID)
// if the user is not found, create a new user in db
services.HandleUserNotFound(s, m, user)
// then update gains
services.HandleGains(s, m, user)
} else {
// setup command handlers
commands.ProfileCommandHandler(s, m)
commands.InfoCommandHandler(s, m)
commands.DevCommandHandler(s, m)
}
}
func MessageReactionHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// if the message contain one of the case
matched, _ := regexp.MatchString(`coffee|coffe|cofee|cofe|cafe|café`, m.Content)
if matched == true {
// if the message author is the bot itself, return to avoid infinite loops
if m.Author.ID == s.State.User.ID {
return
}
// react with coffee emoji
s.MessageReactionAdd(m.ChannelID, m.ID, "☕")
}
}