Completed
Push — master ( d748e0...dfe896 )
by Viktor
02:34
created

bot.NewContext   A

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nop 15
dl 0
loc 20
rs 9.4
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
}
32
33
// NewContext create new context
34
func NewContext(botID string, discord *discordgo.Session, guild *discordgo.Guild, textChannel *discordgo.Channel,
35
	user *discordgo.User, message *discordgo.MessageCreate, conf *Config, cmdHandler *CommandHandler,
36
	sessions *SessionManager, youtube *Youtube, botMsg *BotMessages, dataType *DataType, dbWorker *DBWorker, guilds GuildsMap, botCron *cron.Cron) *Context {
37
	ctx := new(Context)
38
	ctx.BotID = botID
39
	ctx.Discord = discord
40
	ctx.Guild = guild
41
	ctx.TextChannel = textChannel
42
	ctx.User = user
43
	ctx.Message = message
44
	ctx.Conf = conf
45
	ctx.CmdHandler = cmdHandler
46
	ctx.Sessions = sessions
47
	ctx.Youtube = youtube
48
	ctx.BotMsg = botMsg
49
	ctx.Data = dataType
50
	ctx.DB = dbWorker
51
	ctx.Guilds = guilds
52
	ctx.Cron = botCron
53
	return ctx
54
}
55
56
// Loc returns translated string by key
57
func (ctx *Context) Loc(key string) string {
58
	// Check if translation exist
59
60
	if len(ctx.Conf.Locales[ctx.GetGuild().Language][key]) == 0 {
61
		return ctx.Conf.Locales["en"][key]
62
	}
63
	return ctx.Conf.Locales[ctx.GetGuild().Language][key]
64
}
65
66
// WeatherCode returns unicode symbol of weather font icon
67
func (ctx *Context) WeatherCode(code string) string {
68
	return ctx.Conf.WeatherCodes[code]
69
}
70
71
// GetVoiceChannel returns user voice channel
72
func (ctx *Context) GetVoiceChannel() *discordgo.Channel {
73
	if ctx.VoiceChannel != nil {
74
		return ctx.VoiceChannel
75
	}
76
77
78
	for _, state := range ctx.Guild.VoiceStates {
79
		if state.UserID == ctx.User.ID {
80
			// Check voice permissions
81
			perm, err := ctx.Discord.State.UserChannelPermissions(ctx.BotID, state.ChannelID)
82
			if err != nil {
83
				fmt.Printf("Error whilst getting bot permissions in guild \"%v\", %v\n", ctx.Guild.ID, err)
84
				return nil
85
			}
86
87
			if 	perm&discordgo.PermissionVoiceConnect != discordgo.PermissionVoiceConnect ||
88
				perm&discordgo.PermissionVoiceSpeak != discordgo.PermissionVoiceSpeak ||
89
				perm&0x00000400 != 0x00000400{
90
				fmt.Printf("Voice permissions denied on guild \"%v\"\n", ctx.Guild.ID)
91
				return nil
92
			}
93
94
			channel, _ := ctx.Discord.State.Channel(state.ChannelID)
95
			ctx.VoiceChannel = channel
96
			return channel
97
		}
98
	}
99
	return nil
100
}
101
102
func (ctx *Context) GetGuild() *GuildData {
0 ignored issues
show
introduced by
exported method Context.GetGuild should have comment or be unexported
Loading history...
103
	if _, ok := ctx.Guilds[ctx.Guild.ID]; !ok {
104
		newData := &GuildData{
105
			ID:          ctx.Guild.ID,
106
			WeatherCity: ctx.Conf.Weather.City,
107
			NewsCounty:  ctx.Conf.News.Country,
108
			Language:    ctx.Conf.General.Language,
109
			Timezone:    ctx.Conf.General.Timezone,
110
			EmbedColor:  ctx.Conf.General.EmbedColor,
111
		}
112
		ctx.DB.DBSession.DB(ctx.DB.DBName).C("guilds").Insert(newData)
113
		ctx.Guilds[ctx.Guild.ID] = newData
114
		return ctx.Guilds[ctx.Guild.ID]
115
	}
116
	return ctx.Guilds[ctx.Guild.ID]
117
}
118