Passed
Push — master ( 955abe...d4f0da )
by Viktor
01:52
created

bot.*Context.GetGuildUser   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 1
dl 0
loc 7
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
	Albion     *AlbionUpdater
33
	BlackList  *BlackListStruct
34
}
35
36
// NewContext create new context
37
func NewContext(botID string, discord *discordgo.Session, guild *discordgo.Guild, textChannel *discordgo.Channel,
38
	user *discordgo.User, message *discordgo.MessageCreate, conf *Config, cmdHandler *CommandHandler,
39
	sessions *SessionManager, youtube *Youtube, botMsg *BotMessages, dataType *DataType, dbWorker *DBWorker,
40
	guilds *GuildsMap, botCron *cron.Cron, twitch *Twitch, albion *AlbionUpdater, blacklist *BlackListStruct) *Context {
41
	ctx := new(Context)
42
	ctx.BotID = botID
43
	ctx.Discord = discord
44
	ctx.Guild = guild
45
	ctx.TextChannel = textChannel
46
	ctx.User = user
47
	ctx.Message = message
48
	ctx.Conf = conf
49
	ctx.CmdHandler = cmdHandler
50
	ctx.Sessions = sessions
51
	ctx.Youtube = youtube
52
	ctx.BotMsg = botMsg
53
	ctx.Data = dataType
54
	ctx.DB = dbWorker
55
	ctx.Guilds = guilds
56
	ctx.Cron = botCron
57
	ctx.Twitch = twitch
58
	ctx.Albion = albion
59
	ctx.BlackList = blacklist
60
	return ctx
61
}
62
63
// Loc returns translated string by key
64
func (ctx *Context) Loc(key string) string {
65
	// Check if translation exist
66
67
	if len(ctx.Conf.Locales[ctx.GetGuild().Language][key]) == 0 {
68
		return ctx.Conf.Locales["en"][key]
69
	}
70
	return ctx.Conf.Locales[ctx.GetGuild().Language][key]
71
}
72
73
func (ctx *Context) GetGuildUser(id string) *discordgo.User {
0 ignored issues
show
introduced by
exported method Context.GetGuildUser should have comment or be unexported
Loading history...
74
	for i, m := range ctx.Guild.Members {
75
		if m.User.ID == id {
76
			return ctx.Guild.Members[i].User
77
		}
78
	}
79
	return nil
80
}
81
82
// WeatherCode returns unicode symbol of weather font icon
83
func (ctx *Context) WeatherCode(code string) string {
84
	return ctx.Conf.WeatherCodes[code]
85
}
86
87
// GuildConf returns config of guild
88
func (ctx *Context) GuildConf() *GuildData {
89
	return ctx.Guilds.Guilds[ctx.Guild.ID]
90
}
91
92
// GetVoiceChannel returns user voice channel
93
func (ctx *Context) GetVoiceChannel() *discordgo.Channel {
94
	if ctx.VoiceChannel != nil {
95
		return ctx.VoiceChannel
96
	}
97
98
	for _, state := range ctx.Guild.VoiceStates {
99
		if state.UserID == ctx.User.ID {
100
			// Check voice permissions
101
			perm, err := ctx.Discord.State.UserChannelPermissions(ctx.BotID, state.ChannelID)
102
			if err != nil {
103
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Error whilst getting bot permissions on guild \"%v\": %v", ctx.Guild.ID, err))
104
				return nil
105
			}
106
107
			if perm&discordgo.PermissionVoiceConnect != discordgo.PermissionVoiceConnect ||
108
				perm&discordgo.PermissionVoiceSpeak != discordgo.PermissionVoiceSpeak ||
109
				perm&0x00000400 != 0x00000400 {
110
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Voice permissions denied on guild \"%v\"", ctx.Guild.ID))
111
				return nil
112
			}
113
114
			channel, _ := ctx.Discord.State.Channel(state.ChannelID)
115
			ctx.VoiceChannel = channel
116
			return channel
117
		}
118
	}
119
	return nil
120
}
121
122
// GetGuild return data about current guild
123
func (ctx *Context) GetGuild() *GuildData {
124
	if _, ok := ctx.Guilds.Guilds[ctx.Guild.ID]; !ok {
125
		newData := &GuildData{
126
			ID:          ctx.Guild.ID,
127
			WeatherCity: ctx.Conf.Weather.City,
128
			NewsCounty:  ctx.Conf.News.Country,
129
			Language:    ctx.Conf.General.Language,
130
			Timezone:    ctx.Conf.General.Timezone,
131
			EmbedColor:  ctx.Conf.General.EmbedColor,
132
		}
133
		_ = ctx.DB.DBSession.DB(ctx.DB.DBName).C("guilds").Insert(newData)
134
		ctx.Guilds.Guilds[ctx.Guild.ID] = newData
135
		return ctx.Guilds.Guilds[ctx.Guild.ID]
136
	}
137
	return ctx.Guilds.Guilds[ctx.Guild.ID]
138
}
139
140
// Log saves log in database
141
func (ctx *Context) Log(module, guildID, text string) {
142
	ctx.DB.Log(module, guildID, text)
143
	ctx.MetricsLog(module)
144
}
145
146
// Arg returns argument by index. If argument not exists returns empty string
147
func (ctx *Context) Arg(index int) string {
148
	if len(ctx.Args) > index {
149
		return ctx.Args[index]
150
	}
151
	return ""
152
}
153