Passed
Push — master ( 5e8ce0...f38ffd )
by Viktor
01:33
created

bot.NewContext   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nop 16
dl 0
loc 21
rs 9.376
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
// GuildConf returns config of guild
74
func (ctx *Context) GuildConf() *GuildData {
75
	return ctx.Guilds.Guilds[ctx.Guild.ID]
76
}
77
78
// GetVoiceChannel returns user voice channel
79
func (ctx *Context) GetVoiceChannel() *discordgo.Channel {
80
	if ctx.VoiceChannel != nil {
81
		return ctx.VoiceChannel
82
	}
83
84
	for _, state := range ctx.Guild.VoiceStates {
85
		if state.UserID == ctx.User.ID {
86
			// Check voice permissions
87
			perm, err := ctx.Discord.State.UserChannelPermissions(ctx.BotID, state.ChannelID)
88
			if err != nil {
89
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Error whilst getting bot permissions on guild \"%v\": %v", ctx.Guild.ID, err))
90
				return nil
91
			}
92
93
			if perm&discordgo.PermissionVoiceConnect != discordgo.PermissionVoiceConnect ||
94
				perm&discordgo.PermissionVoiceSpeak != discordgo.PermissionVoiceSpeak ||
95
				perm&0x00000400 != 0x00000400 {
96
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Voice permissions denied on guild \"%v\"", ctx.Guild.ID))
97
				return nil
98
			}
99
100
			channel, _ := ctx.Discord.State.Channel(state.ChannelID)
101
			ctx.VoiceChannel = channel
102
			return channel
103
		}
104
	}
105
	return nil
106
}
107
108
// GetGuild return data about current guild
109
func (ctx *Context) GetGuild() *GuildData {
110
	if _, ok := ctx.Guilds.Guilds[ctx.Guild.ID]; !ok {
111
		newData := &GuildData{
112
			ID:          ctx.Guild.ID,
113
			WeatherCity: ctx.Conf.Weather.City,
114
			NewsCounty:  ctx.Conf.News.Country,
115
			Language:    ctx.Conf.General.Language,
116
			Timezone:    ctx.Conf.General.Timezone,
117
			EmbedColor:  ctx.Conf.General.EmbedColor,
118
		}
119
		_ = ctx.DB.DBSession.DB(ctx.DB.DBName).C("guilds").Insert(newData)
120
		ctx.Guilds.Guilds[ctx.Guild.ID] = newData
121
		return ctx.Guilds.Guilds[ctx.Guild.ID]
122
	}
123
	return ctx.Guilds.Guilds[ctx.Guild.ID]
124
}
125
126
// Log saves log in database
127
func (ctx *Context) Log(module, guildID, text string) {
128
	ctx.DB.Log(module, guildID, text)
129
	ctx.MetricsLog(module)
130
}
131
132
// Arg returns argument by index. If argument not exists returns empty string
133
func (ctx *Context) Arg(index int) string {
134
	if len(ctx.Args) > index {
135
		return ctx.Args[index]
136
	}
137
	return ""
138
}
139