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

main.commandHandler   C

Complexity

Conditions 9

Size

Total Lines 53
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 44
nop 2
dl 0
loc 53
rs 6.4906
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package main
2
3
import (
4
	"fmt"
5
	"gopkg.in/robfig/cron.v2"
6
	"os"
7
	"os/signal"
8
	"strings"
9
	"syscall"
10
11
	"github.com/FlameInTheDark/dtbot/bot"
12
	"github.com/FlameInTheDark/dtbot/cmd"
13
	"github.com/bwmarrin/discordgo"
14
)
15
16
var (
17
	conf *bot.Config
18
	// CmdHandler bot command handler
19
	CmdHandler *bot.CommandHandler
20
	// Sessions bot session manager
21
	Sessions *bot.SessionManager
22
	botId    string
0 ignored issues
show
introduced by
var botId should be botID
Loading history...
23
	youtube  *bot.Youtube
24
	botMsg   *bot.BotMessages
25
	dataType *bot.DataType
26
	dbWorker *bot.DBWorker
27
	guilds   bot.GuildsMap
28
	botCron  *cron.Cron
29
)
30
31
func main() {
32
	botCron = cron.New()
33
	conf = bot.LoadConfig()
34
	CmdHandler = bot.NewCommandHandler()
35
	registerCommands()
36
	Sessions = bot.NewSessionManager()
37
	youtube = &bot.Youtube{Conf: conf}
38
	botMsg = bot.NewMessagesMap()
39
	dataType = bot.NewDataType()
40
	discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
41
	if err != nil {
42
		fmt.Println("Create session error, ", err)
43
		return
44
	}
45
	usr, err := discord.User("@me")
46
	if err != nil {
47
		fmt.Println("Error obtaining account details,", err)
48
		return
49
	}
50
	botId = usr.ID
51
	discord.AddHandler(commandHandler)
52
	discord.AddHandler(func(discord *discordgo.Session, ready *discordgo.Ready) {
53
		discord.UpdateStatus(0, conf.General.Game)
54
		guilds := discord.State.Guilds
55
		fmt.Println("Ready with", len(guilds), "guilds.")
56
	})
57
58
	err = discord.Open()
59
	if err != nil {
60
		fmt.Printf("Connection open error: %v", err)
61
		return
62
	}
63
	defer discord.Close()
64
	fmt.Println("Bot is now running.")
65
66
	sc := make(chan os.Signal, 1)
67
	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
68
	dbWorker = bot.NewDBSession(conf.General.DatabaseName)
69
	guilds = dbWorker.InitGuilds(discord, conf)
70
	botCron.Start()
71
	defer botCron.Stop()
72
	defer dbWorker.DBSession.Close()
73
	<-sc
74
}
75
76
// Handle discord messages
77
func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
78
	user := message.Author
79
	if user.ID == botId || user.Bot {
80
		return
81
	}
82
	args := strings.Split(message.Content, " ")
83
	name := strings.ToLower(args[0])
84
	command, found := CmdHandler.Get(name)
85
	if !found {
86
		return
87
	}
88
	channel, err := discord.State.Channel(message.ChannelID)
89
	if err != nil {
90
		fmt.Println("Error getting channel,", err)
91
		return
92
	}
93
	guild, err := discord.State.Guild(channel.GuildID)
94
	if err != nil {
95
		fmt.Println("Error getting guild,", err)
96
		return
97
	}
98
	// Checking permissions
99
	perm, err := discord.State.UserChannelPermissions(botId, message.ChannelID)
100
	if err != nil {
101
		fmt.Printf("Error whilst getting bot permissions in guild \"%v\", %v\n", guild.ID ,err)
102
		return
103
	}
104
105
	if perm&discordgo.PermissionSendMessages != discordgo.PermissionSendMessages ||
106
		perm&discordgo.PermissionAttachFiles != discordgo.PermissionAttachFiles{
107
		fmt.Printf("Permissions denied on guild \"%v\"\n", guild.ID)
108
		return
109
	}
110
111
	ctx := bot.NewContext(
112
		botId,
113
		discord,
114
		guild,
115
		channel,
116
		user,
117
		message,
118
		conf,
119
		CmdHandler,
120
		Sessions,
121
		youtube,
122
		botMsg,
123
		dataType,
124
		dbWorker,
125
		guilds,
126
		botCron)
127
	ctx.Args = args[1:]
128
	c := *command
129
	c(*ctx)
130
}
131
132
// Adds bot commands
133
func registerCommands() {
134
	CmdHandler.Register("!r", cmd.PlayerCommand)
135
	CmdHandler.Register("!w", cmd.WeatherCommand)
136
	CmdHandler.Register("!t", cmd.TranslateCommand)
137
	CmdHandler.Register("!n", cmd.NewsCommand)
138
	CmdHandler.Register("!c", cmd.CurrencyCommand)
139
	CmdHandler.Register("!y", cmd.YoutubeCommand)
140
	CmdHandler.Register("!v", cmd.VoiceCommand)
141
	CmdHandler.Register("!b", cmd.BotCommand)
142
	CmdHandler.Register("!play", cmd.YoutubeShortCommand)
143
	CmdHandler.Register("!d", cmd.DebugCommand)
144
	CmdHandler.Register("!p", cmd.PollCommand)
145
	CmdHandler.Register("!dice", cmd.DiceCommand)
146
	CmdHandler.Register("!help", cmd.HelpCommand)
147
	CmdHandler.Register("!cron", cmd.CronCommand)
148
}
149