Completed
Push — master ( 43fa80...2d35ab )
by Viktor
02:15
created

bot/context.go   A

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 83
dl 0
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A bot.*Context.WeatherCode 0 2 1
A bot.*Context.Loc 0 7 2
A bot.NewContext 0 20 1
A bot.*Context.GetGuild 0 15 2
C bot.*Context.GetVoiceChannel 0 27 9
A bot.*Context.Log 0 3 1
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
	for _, state := range ctx.Guild.VoiceStates {
78
		if state.UserID == ctx.User.ID {
79
			// Check voice permissions
80
			perm, err := ctx.Discord.State.UserChannelPermissions(ctx.BotID, state.ChannelID)
81
			if err != nil {
82
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Error whilst getting bot permissions on guild \"%v\": %v", ctx.Guild.ID, err))
83
				return nil
84
			}
85
86
			if perm&discordgo.PermissionVoiceConnect != discordgo.PermissionVoiceConnect ||
87
				perm&discordgo.PermissionVoiceSpeak != discordgo.PermissionVoiceSpeak ||
88
				perm&0x00000400 != 0x00000400 {
89
				ctx.DB.Log("Voice", ctx.Guild.ID, fmt.Sprintf("Voice permissions denied on guild \"%v\"", ctx.Guild.ID))
90
				return nil
91
			}
92
93
			channel, _ := ctx.Discord.State.Channel(state.ChannelID)
94
			ctx.VoiceChannel = channel
95
			return channel
96
		}
97
	}
98
	return nil
99
}
100
101
func (ctx *Context) GetGuild() *GuildData {
0 ignored issues
show
introduced by
exported method Context.GetGuild should have comment or be unexported
Loading history...
102
	if _, ok := ctx.Guilds[ctx.Guild.ID]; !ok {
103
		newData := &GuildData{
104
			ID:          ctx.Guild.ID,
105
			WeatherCity: ctx.Conf.Weather.City,
106
			NewsCounty:  ctx.Conf.News.Country,
107
			Language:    ctx.Conf.General.Language,
108
			Timezone:    ctx.Conf.General.Timezone,
109
			EmbedColor:  ctx.Conf.General.EmbedColor,
110
		}
111
		ctx.DB.DBSession.DB(ctx.DB.DBName).C("guilds").Insert(newData)
112
		ctx.Guilds[ctx.Guild.ID] = newData
113
		return ctx.Guilds[ctx.Guild.ID]
114
	}
115
	return ctx.Guilds[ctx.Guild.ID]
116
}
117
118
func (ctx *Context) Log(module, guildID, text string) {
0 ignored issues
show
introduced by
exported method Context.Log should have comment or be unexported
Loading history...
119
	ctx.DB.Log(module, guildID, text)
120
	ctx.MetricsLog(module)
121
}
122