-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (68 loc) · 1.9 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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"slices"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
var (
GuildId string
Token string
session *discordgo.Session
db *sql.DB
)
func init() {
flag.Parse()
var err error
err = godotenv.Load(".env")
if err != nil {
log.Fatalf("couldn't read .env: %s", err)
}
GuildId := os.Getenv("DISCORD_GUILD")
Token := os.Getenv("DISCORD_TOKEN")
dbUser := os.Getenv("POSTGRES_USER")
dbPassword := os.Getenv("POSTGRES_PASSWORD")
dbName := os.Getenv("POSTGRES_DB")
dbPort := os.Getenv("POSTGRES_PORT")
if slices.Contains([]string{GuildId, Token, dbUser, dbPassword, dbName, dbPort}, "") {
log.Fatal("FAILED RETRIEVING ENVIRONMENT VARIABLES")
}
session, err = discordgo.New("Bot " + Token)
if err != nil {
log.Fatalf("failed to authorize bot: %s", err)
}
db, err = sql.Open("postgres", fmt.Sprintf("host=127.0.0.1 port=%s user=%s password=%s dbname=%s sslmode=disable", dbPort, dbUser, dbPassword, dbName))
if err != nil {
log.Fatalf("failed to establish database connection: %s", err)
}
if err = db.Ping(); err != nil {
log.Fatalf("failed to reach database: %s", err)
}
log.Println("succesfully established database connection")
}
func main() {
session.Identify.Intents = discordgo.IntentsAll
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("logged in as %s", session.State.User.String())
})
if err := session.Open(); err != nil {
log.Fatalf("failed to start bot client: %s", err)
}
session.UpdateCustomStatus("vai corinthians!!!!")
session.AddHandler(iqIncreaseHandler)
session.AddHandler(semPutariaHandler)
registerSlashCommands()
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
<-sigch
clearSlashCommands()
if err := session.Close(); err != nil {
log.Printf("failed closing session: %s", err)
}
}