-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
69 lines (57 loc) · 1.85 KB
/
helpers.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
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"os"
"regexp"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func extractPlusMinusEventData(message string) []string {
regex := *regexp.MustCompile(`@([!-=?-~]+)>?\s*(\+{2}|-{2}|—{1}|={2})`)
res := regex.FindAllStringSubmatch(message, -1)
println("Message: " + message)
if res != nil {
println("Extracted Subject: " + res[0][1] + " Extracted Operation: " + res[0][2])
return []string{res[0][1], res[0][2]}
}
return nil
}
func dbCall() *mgo.Session {
dialInfo := &mgo.DialInfo{
Addrs: []string{fmt.Sprintf("%s.documents.azure.com:10255", mongoDBDatabase)}, // Get HOST + PORT
Timeout: 60 * time.Second,
Database: mongoDBDatabase, // It can be anything
Username: mongoDBDatabase, // Username
Password: mongoDBPassword, // PASSWORD
DialServer: func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), &tls.Config{})
},
}
// Create a session which maintains a pool of socket connections
session, err := mgo.DialWithInfo(dialInfo)
if err != nil {
fmt.Printf("Can't connect, go error %v\n", err)
os.Exit(1)
}
// SetSafe changes the session safety mode.
// If the safe parameter is nil, the session is put in unsafe mode, and writes become fire-and-forget,
// without error checking. The unsafe mode is faster since operations won't hold on waiting for a confirmation.
// http://godoc.org/labix.org/v2/mgo#Session.SetMode.
session.SetSafe(&mgo.Safe{})
return session.Clone()
}
func returnItemID(item string, guild string) []bson.ObjectId {
itemS := PointItem{}
collection := session.DB(mongoDBDatabase).C("PointItems")
println(collection.FullName)
err := collection.Find(bson.M{"item": item}).Select(bson.M{"guild": guild}).One(&itemS)
if err != nil {
log.Output(0, "Error locating record")
return nil
}
return []bson.ObjectId{itemS.Id}
}