Passed
Push — master ( 193344...d1e92e )
by Viktor
01:34
created

bot.*Context.GuildConf   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package bot
2
3
import (
4
	"fmt"
5
	"github.com/bwmarrin/discordgo"
6
	"gopkg.in/robfig/cron.v2"
7
)
8
9
// Context : Bot context structure
10
type Context struct {
11
	BotID string
12
13
	Discord      *discordgo.Session
14
	Guild        *discordgo.Guild
15
	VoiceChannel *discordgo.Channel
16
	TextChannel  *discordgo.Channel
17
	User         *discordgo.User
18
	Message      *discordgo.MessageCreate
19
	Args         []string
20
21
	DB   *DBWorker
22
	Cron *cron.Cron
23
24
	Conf       *Config
25
	CmdHandler *CommandHandler
26
	Sessions   *SessionManager
27
	Youtube    *Youtube
28
	BotMsg     *BotMessages
29
	Data       *DataType
30
	Guilds     *GuildsMap
31
	Twitch     *Twitch
32
}
33
34
// NewContext create new context
35
func NewContext(botID string, discord *discordgo.Session, guild *discordgo.Guild, textChannel *discordgo.Channel,
36
	user *discordgo.User, message *discordgo.MessageCreate, conf *Config, cmdHandler *CommandHandler,
37
	sessions *SessionManager, youtube *Youtube, botMsg *BotMessages, dataType *DataType, dbWorker *DBWorker, guilds *GuildsMap, botCron *cron.Cron, twitch *Twitch) *Context {
38
	ctx := new(Context)
39
	ctx.BotID = botID
40
	ctx.Discord = discord
41
	ctx.Guild = guild
42
	ctx.TextChannel = textChannel
43
	ctx.User = user
44
	ctx.Message = message
45
	ctx.Conf = conf
46
	ctx.CmdHandler = cmdHandler
47
	ctx.Sessions = sessions
48
	ctx.Youtube = youtube
49
	ctx.BotMsg = botMsg
50
	ctx.Data = dataType
51
	ctx.DB = dbWorker
52
	ctx.Guilds = guilds
53
	ctx.Cron = botCron
54
	ctx.Twitch = twitch
55
	return ctx
56
}
57
58
// Loc returns translated string by key
59
func (ctx *Context) Loc(key string) string {
60
	// Check if translation exist
61
62
	if len(ctx.Conf.Locales[ctx.GetGuild().Language][key]) == 0 {
63
		return ctx.Conf.Locales["en"][key]
64
	}
65
	return ctx.Conf.Locales[ctx.GetGuild().Language][key]
66
}
67
68
// WeatherCode returns unicode symbol of weather font icon
69
func (ctx *Context) WeatherCode(code string) string {
70
	return ctx.Conf.WeatherCodes[code]
71
}
72
73
func (ctx *Context) GuildConf() *GuildData {
0 ignored issues
show
introduced by
exported method Context.GuildConf should have comment or be unexported
Loading history...
74
	return ctx.Guilds.Guilds[ctx.Guild.ID]
75
}
76
77
// GetVoiceChannel returns user voice channel
78
func (ctx *Context) GetVoiceChannel() *discordgo.Channel {
79
	if ctx.VoiceChannel != nil {
80
		return ctx.VoiceChannel
81
	}
82
83
	for _, state := range ctx.Guild.VoiceStates {
84
		if state.UserID == ctx.User.ID {
85
			// Check voice permissions
86
			perm, err := ctx.Discord.State.UserChannelPermissions(ctx.BotID, state.ChannelID)
87
			if err != nil {
88
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Error whilst getting bot permissions on guild \"%v\": %v", ctx.Guild.ID, err))
89
				return nil
90
			}
91
92
			if perm&discordgo.PermissionVoiceConnect != discordgo.PermissionVoiceConnect ||
93
				perm&discordgo.PermissionVoiceSpeak != discordgo.PermissionVoiceSpeak ||
94
				perm&0x00000400 != 0x00000400 {
95
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Voice permissions denied on guild \"%v\"", ctx.Guild.ID))
96
				return nil
97
			}
98
99
			channel, _ := ctx.Discord.State.Channel(state.ChannelID)
100
			ctx.VoiceChannel = channel
101
			return channel
102
		}
103
	}
104
	return nil
105
}
106
107
// GetGuild return data about current guild
108
func (ctx *Context) GetGuild() *GuildData {
109
	if _, ok := ctx.Guilds.Guilds[ctx.Guild.ID]; !ok {
110
		newData := &GuildData{
111
			ID:          ctx.Guild.ID,
112
			WeatherCity: ctx.Conf.Weather.City,
113
			NewsCounty:  ctx.Conf.News.Country,
114
			Language:    ctx.Conf.General.Language,
115
			Timezone:    ctx.Conf.General.Timezone,
116
			EmbedColor:  ctx.Conf.General.EmbedColor,
117
		}
118
		_ = ctx.DB.DBSession.DB(ctx.DB.DBName).C("guilds").Insert(newData)
119
		ctx.Guilds.Guilds[ctx.Guild.ID] = newData
120
		return ctx.Guilds.Guilds[ctx.Guild.ID]
121
	}
122
	return ctx.Guilds.Guilds[ctx.Guild.ID]
123
}
124
125
// Log saves log in database
126
func (ctx *Context) Log(module, guildID, text string) {
127
	ctx.DB.Log(module, guildID, text)
128
	ctx.MetricsLog(module)
129
}
130