Passed
Push — master ( 77df28...2905b9 )
by Viktor
01:32
created

bot.*DBWorker.GetTwitchStreams   B

Complexity

Conditions 7

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nop 1
dl 0
loc 16
rs 8
c 0
b 0
f 0
1
package bot
2
3
import (
4
	"fmt"
5
	"github.com/bwmarrin/discordgo"
6
	"github.com/globalsign/mgo"
7
	"github.com/globalsign/mgo/bson"
8
	"os"
9
	"time"
10
)
11
12
type DBWorker struct {
0 ignored issues
show
introduced by
exported type DBWorker should have comment or be unexported
Loading history...
13
	DBSession *mgo.Session
14
	DBName    string
15
}
16
17
type dbLog struct {
18
	Date   time.Time
19
	Text   string
20
	Module string
21
	Guild  string
22
}
23
24
type GuildData struct {
0 ignored issues
show
introduced by
exported type GuildData should have comment or be unexported
Loading history...
25
	ID          string
26
	WeatherCity string
27
	NewsCounty  string
28
	Language    string
29
	Timezone    int
30
	EmbedColor  int
31
}
32
33
type GuildsMap map[string]*GuildData
0 ignored issues
show
introduced by
exported type GuildsMap should have comment or be unexported
Loading history...
34
35
func NewDBSession(dbname string) *DBWorker {
0 ignored issues
show
introduced by
exported function NewDBSession should have comment or be unexported
Loading history...
36
	session, err := mgo.Dial(os.Getenv("MONGO_CONN"))
37
	if err != nil {
38
		fmt.Printf("Mongo connection error: %v", err)
39
	}
40
	count, err := session.DB("dtbot").C("logs").Count()
41
	if err != nil {
42
		fmt.Println("DB_ERR: ", err)
43
	}
44
	fmt.Printf("Mongo connected\nLogs in base: %v\n", count)
45
	return &DBWorker{DBSession: session, DBName: dbname}
46
}
47
48
// InitGuilds initialize guilds in database
49
func (db *DBWorker) InitGuilds(sess *discordgo.Session, conf *Config) GuildsMap {
50
	var data = make(GuildsMap)
51
	var loaded, initialized = 0, 0
52
	for _, guild := range sess.State.Guilds {
53
		count, err := db.DBSession.DB(db.DBName).C("guilds").Find(bson.M{"id": guild.ID}).Count()
54
		if err != nil {
55
			fmt.Println("Mongo: ", err)
56
		}
57
		if count == 0 {
58
			newData := &GuildData{
59
				ID:          guild.ID,
60
				WeatherCity: conf.Weather.City,
61
				NewsCounty:  conf.News.Country,
62
				Language:    conf.General.Language,
63
				Timezone:    conf.General.Timezone,
64
				EmbedColor:  conf.General.EmbedColor,
65
			}
66
			_ = db.DBSession.DB(db.DBName).C("guilds").Insert(newData)
67
			data[guild.ID] = newData
68
			initialized++
69
		} else {
70
			var newData = &GuildData{}
71
			_ = db.DBSession.DB(db.DBName).C("guilds").Find(bson.M{"id": guild.ID}).One(newData)
72
			if err != nil {
73
				fmt.Println("Mongo: ", err)
74
				continue
75
			}
76
			data[guild.ID] = newData
77
			loaded++
78
		}
79
	}
80
	fmt.Printf("Guilds loaded [%v], initialized [%v]\n", loaded, initialized)
81
	return data
82
}
83
84
// Log saves log in database
85
func (db *DBWorker) Log(module, guildID, text string) {
86
	_ = db.DBSession.DB(db.DBName).C("logs").Insert(dbLog{Date: time.Now(), Text: text, Module: module, Guild: guildID})
87
}
88
89
// LogGet returns last N log rows
90
func (db *DBWorker) LogGet(count int) []dbLog {
0 ignored issues
show
introduced by
exported method LogGet returns unexported type []bot.dbLog, which can be annoying to use
Loading history...
91
	var log = make([]dbLog, count)
92
	_ = db.DBSession.DB(db.DBName).C("logs").Find(nil).Sort("-$natural").Limit(count).All(&log)
93
	return log
94
}
95
96
func (db *DBWorker) Guilds() *mgo.Collection {
0 ignored issues
show
introduced by
exported method DBWorker.Guilds should have comment or be unexported
Loading history...
97
	return db.DBSession.DB(db.DBName).C("guilds")
98
}
99
100
func (db *DBWorker) GetTwitchStreams(guildID string) map[string]*TwitchStream {
0 ignored issues
show
introduced by
exported method DBWorker.GetTwitchStreams should have comment or be unexported
Loading history...
101
	streams := []TwitchStream{}
102
	err := db.DBSession.DB(db.DBName).C("streams").Find(bson.M{"guild": guildID}).All(&streams)
103
	if err != nil {
104
		fmt.Println("Mongo: ", err)
105
	}
106
	if len(streams) > 0 {
107
		for _,s := range streams {
108
			fmt.Println(s.Login, " : ", s.Guild)
109
		}
110
	}
111
	var newMap = make(map[string]*TwitchStream)
112
	for _, s := range streams {
113
		newMap[s.Login] = &s
114
	}
115
	return newMap
116
}
117
118
func (db *DBWorker) UpdateStream(stream *TwitchStream) {
0 ignored issues
show
introduced by
exported method DBWorker.UpdateStream should have comment or be unexported
Loading history...
119
	err := db.DBSession.DB(db.DBName).C("streams").
120
		Update(
121
			bson.M{"guild": stream.Guild, "login": stream.Login},
122
			bson.M{"$set": bson.M{"isonline": stream.IsOnline}})
123
	if err != nil {
124
		fmt.Println(err.Error())
125
	}
126
}
127
128
func (db *DBWorker) AddStream(stream *TwitchStream) {
0 ignored issues
show
introduced by
exported method DBWorker.AddStream should have comment or be unexported
Loading history...
129
	_ = db.DBSession.DB(db.DBName).C("streams").Insert(stream)
130
}
131
132
func (db *DBWorker) RemoveStream(stream *TwitchStream) {
0 ignored issues
show
introduced by
exported method DBWorker.RemoveStream should have comment or be unexported
Loading history...
133
	_ = db.DBSession.DB(db.DBName).C("streams").Remove(bson.M{"login": stream.Login, "guild": stream.Guild})
134
}
135