Passed
Push — master ( fb5b4f...b9f2ca )
by Viktor
01:40
created

bot.NewContext   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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