Skip to content

Commit

Permalink
added discordgo Session wrapper
Browse files Browse the repository at this point in the history
Ticket #21

	Adding a wrapper to encapsulate some additional functionality to discord session object.
  • Loading branch information
dmportella committed Sep 8, 2016
1 parent 4d2db70 commit 837ee1c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
11 changes: 11 additions & 0 deletions bot/discordsession.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions bot/discordsession_func.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion bot/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions bot/qilbot.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package bot

import "github.com/bwmarrin/discordgo"

// Qilbot representation of the instance of qilbot.
type Qilbot struct {
// Publics
Expand All @@ -13,7 +11,7 @@ type Qilbot struct {

stopChannel chan struct{}
config *QilbotConfig
session *discordgo.Session
session *DiscordSession
commands map[string]*CommandInformation
}

Expand Down
4 changes: 2 additions & 2 deletions bot/qilbot_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions plugins/common/common_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ 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)
},
},
{
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)
},
},
{
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)
},
},
Expand All @@ -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)
Expand All @@ -83,26 +83,22 @@ 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")
}
_, _ = 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())
}
}
12 changes: 6 additions & 6 deletions plugins/edsm/edsmplugin_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ 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)
},
},
{
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)
},
},
{
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)
},
},
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 837ee1c

Please sign in to comment.