diff --git a/bot/discordsession.go b/bot/discordsession.go new file mode 100644 index 0000000..4b6c44c --- /dev/null +++ b/bot/discordsession.go @@ -0,0 +1,11 @@ +package bot + +import ( + "github.com/bwmarrin/discordgo" +) + +// DiscordSession Discord wrapper for discordgo. +// This allows us to decorate inject methods into discordgo.Session object. +type DiscordSession struct { + *discordgo.Session +} diff --git a/bot/discordsession_func.go b/bot/discordsession_func.go new file mode 100644 index 0000000..e92063c --- /dev/null +++ b/bot/discordsession_func.go @@ -0,0 +1,14 @@ +package bot + +import ( + "github.com/bwmarrin/discordgo" +) + +// IsOwnerOfGuild Check that the author of the message is the onwer of the guild. +func (s *DiscordSession) IsOwnerOfGuild(m *discordgo.MessageCreate) bool { + channel, _ := s.Channel(m.ChannelID) + + guild, _ := s.Guild(channel.GuildID) + + return guild.OwnerID == m.Author.ID +} diff --git a/bot/plugin.go b/bot/plugin.go index dd83cfc..2cd1ca8 100644 --- a/bot/plugin.go +++ b/bot/plugin.go @@ -19,7 +19,7 @@ type CommandInformation struct { Command string Template string Description string - Execute func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) + Execute func(s *DiscordSession, m *discordgo.MessageCreate, commandText string) } // IPlugin interface used by qilbot. diff --git a/bot/qilbot.go b/bot/qilbot.go index 2f4a92c..095b615 100644 --- a/bot/qilbot.go +++ b/bot/qilbot.go @@ -1,7 +1,5 @@ package bot -import "github.com/bwmarrin/discordgo" - // Qilbot representation of the instance of qilbot. type Qilbot struct { // Publics @@ -13,7 +11,7 @@ type Qilbot struct { stopChannel chan struct{} config *QilbotConfig - session *discordgo.Session + session *DiscordSession commands map[string]*CommandInformation } diff --git a/bot/qilbot_func.go b/bot/qilbot_func.go index 6abfaac..70746b1 100644 --- a/bot/qilbot_func.go +++ b/bot/qilbot_func.go @@ -21,7 +21,7 @@ func New(config *QilbotConfig) (bot *Qilbot, err error) { logging.Error.Println("Could not create discord session, ", ok) err = ok } else { - bot.session = dg + bot.session = &DiscordSession{dg} } // Get the account information. @@ -52,7 +52,7 @@ func (qilbot *Qilbot) discordCreateMessage(s *discordgo.Session, m *discordgo.Me commandCalled := matches[1] if command, ok := qilbot.commands[commandCalled]; ok { - go command.Execute(s, m, matches[2]) + go command.Execute(qilbot.session, m, matches[2]) } } } diff --git a/plugins/common/common_func.go b/plugins/common/common_func.go index 6048b58..9969b76 100644 --- a/plugins/common/common_func.go +++ b/plugins/common/common_func.go @@ -26,7 +26,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "plugins", Template: "plugins", Description: "Display a list of plugins enabled on qilbot.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.pluginsCommand(s, m, commandText) }, }, @@ -34,7 +34,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "help", Template: "!help or !help *command*", Description: "Display a list of commands available to qilbot and more information about specific commands.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.helpCommand(s, m, commandText) }, }, @@ -42,7 +42,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "set", Template: "!set *variable* on|off", Description: "Changes the settings of qilbot at runtime.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.setCommand(s, m, commandText) }, }, @@ -59,7 +59,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { return } -func (plugin *Plugin) helpCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) helpCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { var buffer bytes.Buffer logging.Info.Println("comand text", commandText) @@ -83,7 +83,7 @@ func (plugin *Plugin) helpCommand(s *discordgo.Session, m *discordgo.MessageCrea _, _ = s.ChannelMessageSend(channel.ID, buffer.String()) } -func (plugin *Plugin) pluginsCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) pluginsCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { var buffer bytes.Buffer for _, item := range plugin.Plugin.Qilbot.Plugins { buffer.WriteString(item.GetHelpText() + "\n") @@ -91,18 +91,14 @@ func (plugin *Plugin) pluginsCommand(s *discordgo.Session, m *discordgo.MessageC _, _ = s.ChannelMessageSend(m.ChannelID, buffer.String()) } -func (plugin *Plugin) setCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) setCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { var buffer bytes.Buffer - channel, _ := s.Channel(m.ChannelID) - - guild, _ := s.Guild(channel.GuildID) - - if guild.OwnerID != m.Author.ID { - buffer.WriteString("Only the Server owner can change the bot settings...") + if s.IsOwnerOfGuild(m) { + buffer.WriteString("This would have done something.") _, _ = s.ChannelMessageSend(m.ChannelID, buffer.String()) } else { - buffer.WriteString("This would have done something.") + buffer.WriteString("Only the Server owner can change the bot settings...") _, _ = s.ChannelMessageSend(m.ChannelID, buffer.String()) } } diff --git a/plugins/edsm/edsmplugin_func.go b/plugins/edsm/edsmplugin_func.go index 74a4a77..940fce6 100644 --- a/plugins/edsm/edsmplugin_func.go +++ b/plugins/edsm/edsmplugin_func.go @@ -30,7 +30,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "distance", Template: "distance **sys1** / **sys2**", Description: "Uses the coords in EDSM to calculate the distance between the two star systems.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.distanceCommand(s, m, commandText) }, }, @@ -38,7 +38,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "sphere", Template: "sphere **sys1** 14.33ly", Description: "Returns a list of systems within a specified distance to specified system.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.sphereCommand(s, m, commandText) }, }, @@ -46,7 +46,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { Command: "locate", Template: "locate **Commander Name**", Description: "Returns the location of a commander in EDSM.", - Execute: func(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { + Execute: func(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { plugin.locateCommand(s, m, commandText) }, }, @@ -64,7 +64,7 @@ func NewPlugin(qilbot *bot.Qilbot) (plugin *Plugin) { return } -func (plugin *Plugin) locateCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) locateCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { var buffer bytes.Buffer if cmdrPos, ok1 := plugin.api.GetPosition(commandText); ok1 == nil && cmdrPos.MSGNum == 100 { @@ -88,7 +88,7 @@ func (plugin *Plugin) locateCommand(s *discordgo.Session, m *discordgo.MessageCr } } -func (plugin *Plugin) sphereCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) sphereCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { placeMatches := regexMatchSphereCommand(commandText) logging.Trace.Println(placeMatches) @@ -142,7 +142,7 @@ func (plugin *Plugin) sphereCommand(s *discordgo.Session, m *discordgo.MessageCr } } -func (plugin *Plugin) distanceCommand(s *discordgo.Session, m *discordgo.MessageCreate, commandText string) { +func (plugin *Plugin) distanceCommand(s *bot.DiscordSession, m *discordgo.MessageCreate, commandText string) { placeMatches := regexMatchDistanceCommand(commandText) if len(placeMatches) >= 3 {