This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
85 lines (73 loc) · 1.95 KB
/
bot.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
package karman
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/bwmarrin/discordgo"
"github.com/guregu/dynamo"
"io/ioutil"
"log"
"os"
"path"
)
type User struct {
User string `dynamo:"user"`
Karma int `dynamo:"karma"`
}
type Karman struct {
con *dynamo.DB
table *dynamo.Table
dg *discordgo.Session
log *log.Logger
}
func New(log *log.Logger) *Karman {
return &Karman{log: log}
}
func (b *Karman) Start() {
b.log.Println("Starting Karman...")
// start DynamoDB session
sess, err := session.NewSession()
if err != nil {
b.log.Println("Error connecting to DB:", err)
return
}
b.con = dynamo.New(sess, aws.NewConfig().WithRegion("us-west-2"))
temp := b.con.Table("Karma")
b.table = &temp
test := User{}
err = b.table.Get("user", "test").One(&test)
if err != nil {
b.log.Println("Error getting test value from DB:", err)
return
}
if test.Karma != 1337 {
b.log.Println("Test test's Karma was not 1337! Instead was", test.Karma)
return
}
b.log.Println("Successfully connected to DynamoDB!")
dat, err := ioutil.ReadFile(path.Join(os.Getenv("HOME"), "botfiles", "karman", "SECRET"))
if err != nil {
b.log.Println("Error reading secret key!", err)
return
}
dg, err := discordgo.New("Bot " + string(dat))
if err != nil {
b.log.Println("Error creating session!", err)
return
}
// start discord stuff
dg.AddHandler(b.ready)
dg.AddHandler(b.reactionAdd)
dg.AddHandler(b.reactionRemove)
dg.AddHandler(b.handleCommand)
err = dg.Open()
if err != nil {
b.log.Println("Error starting websocket:", err)
return
}
b.dg = dg
b.log.Println("Successfully connected to Discord!")
}
func (b *Karman) Stop() {
// DB Connection automatically closes?
b.dg.Close()
}