Skip to content

Commit

Permalink
Merge pull request #46 from torlenor/41-api-get-callprefix
Browse files Browse the repository at this point in the history
Introduce API GetCallPrefix()
  • Loading branch information
torlenor authored May 22, 2020
2 parents cb0eb9a + 29cca37 commit d37f639
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 43 deletions.
9 changes: 7 additions & 2 deletions commanddispatcher/commanddispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *CommandDispatcher) Unregister(cmd string) {
delete(c.receivers, cmd)
}

// OnPost feeds a post to the CommandDispatcher which will then do its magic
// OnPost feeds a post to the CommandDispatcher which will then do its magic.
func (c *CommandDispatcher) OnPost(post model.Post) {
if len(post.Content) < 2 {
return
Expand All @@ -78,7 +78,7 @@ func (c *CommandDispatcher) OnPost(post model.Post) {
}
}

// IsHelp returns true and the help message if the post contains a help command
// IsHelp returns true and the help message if the post contains a help command.
func (c *CommandDispatcher) IsHelp(post model.Post) (bool, string) {
if len(post.Content) < 2 {
return false, ""
Expand Down Expand Up @@ -108,3 +108,8 @@ func (c *CommandDispatcher) getHelpText() string {
helptext += "\nNote: Some of them are only available for mods."
return helptext
}

// GetCallPrefix returns the current call prefix.
func (c *CommandDispatcher) GetCallPrefix() string {
return c.callPrefix
}
112 changes: 112 additions & 0 deletions commanddispatcher/commanddispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,115 @@ func TestCommandDispatcher(t *testing.T) {
dispatcher.Unregister(expectedCommand)
assert.Equal(0, len(dispatcher.receivers))
}

func TestCommandDispatcher_GetCallPrefix(t *testing.T) {
type fields struct {
callPrefix string
receivers map[string]receiver
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "prefix !",
fields: fields{callPrefix: "!"},
want: "!",
},
{
name: "prefix abc",
fields: fields{callPrefix: "abc"},
want: "abc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommandDispatcher{
callPrefix: tt.fields.callPrefix,
receivers: tt.fields.receivers,
}
if got := c.GetCallPrefix(); got != tt.want {
t.Errorf("CommandDispatcher.GetCallPrefix() = %v, want %v", got, tt.want)
}
})
}
}

func TestCommandDispatcher_IsHelp(t *testing.T) {
type fields struct {
callPrefix string
receivers map[string]receiver
}
type args struct {
post model.Post
}
tests := []struct {
name string
fields fields
args args
want bool
want1 string
}{
{
name: "Default call prefix: is help",
fields: fields{callPrefix: "!"},
args: args{
post: model.Post{Content: "!help"},
},
want: true,
want1: "The following commands are available: \nNote: Some of them are only available for mods.",
},
{
name: "Default call prefix: is not help",
fields: fields{callPrefix: "!"},
args: args{
post: model.Post{Content: "!nothelp"},
},
want: false,
want1: "",
},
{
name: "Default call prefix: also not help",
fields: fields{callPrefix: "!"},
args: args{
post: model.Post{Content: "!helpabcabc"},
},
want: false,
want1: "",
},
{
name: "Other call prefix: is help",
fields: fields{callPrefix: "abc"},
args: args{
post: model.Post{Content: "abchelp"},
},
want: true,
want1: "The following commands are available: \nNote: Some of them are only available for mods.",
},
{
name: "Other call prefix: is not help",
fields: fields{callPrefix: "abc"},
args: args{
post: model.Post{Content: "abcnothelp"},
},
want: false,
want1: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &CommandDispatcher{
callPrefix: tt.fields.callPrefix,
receivers: tt.fields.receivers,
}
got, got1 := c.IsHelp(tt.args.post)
if got != tt.want {
t.Errorf("CommandDispatcher.IsHelp() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("CommandDispatcher.IsHelp() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
5 changes: 5 additions & 0 deletions platform/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ func (b *BotImpl) UnRegisterCommand(command string) error {
b.Dispatcher.Unregister(command)
return nil
}

// GetCallPrefix returns the current command call prefix.
func (b *BotImpl) GetCallPrefix() string {
return b.Dispatcher.GetCallPrefix()
}
3 changes: 3 additions & 0 deletions plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type API interface {
// UnRegisterCommand unregisters a command previously registered via RegisterCommand.
UnRegisterCommand(command string) error

// GetCallPrefix returns the current command call prefix.
GetCallPrefix() string

// GetUsers a list of all users the bot knows.
GetUsers() ([]model.User, error)

Expand Down
4 changes: 2 additions & 2 deletions plugin/customcommandsplugin/commandhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func splitCommand(text string) (c string, customCommand string, msg string, err
return
}

// onCommand handles a !customcommand command.
// onCommand handles a customcommand command.
func (p *CustomCommandsPlugin) onCommand(content string, post model.Post) {
if content == "add" {
p.returnHelpAdd(post.ChannelID)
Expand All @@ -152,7 +152,7 @@ func (p *CustomCommandsPlugin) onCommand(content string, post model.Post) {

c, customCommand, message, err := splitCommand(content)
if err != nil {
p.API.LogError(fmt.Sprintf("Error parsing !customcommand command '%s': %s", post.Content, err))
p.API.LogError(fmt.Sprintf("Error parsing %scustomcommand command '%s': %s", p.API.GetCallPrefix(), content, err))
p.returnHelp(post.ChannelID)
return
}
Expand Down
18 changes: 11 additions & 7 deletions plugin/customcommandsplugin/help.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package customcommandsplugin

import "github.com/torlenor/redseligg/model"
import (
"fmt"

"github.com/torlenor/redseligg/model"
)

const (
helpText = "Use '!customcommand add' to add and '!customcommand remove' to remove a custom command"
helpTextAdd = "Type '!customcommand add <customCommand> <message text>' to add the custom command '!customCommand'"
helpTextRemove = "Type '!customcommand remove <customCommand> to remove the custom command '!customCommand'"
helpText = "Use '%scustomcommand add' to add and '%scustomcommand remove' to remove a custom command"
helpTextAdd = "Type '%scustomcommand add <customCommand> <message text>' to add the custom command '%scustomCommand'"
helpTextRemove = "Type '%scustomcommand remove <customCommand> to remove the custom command '%scustomCommand'"
)

func (p *CustomCommandsPlugin) returnHelp(channelID string) {
p.returnMessage(channelID, helpText)
p.returnMessage(channelID, fmt.Sprintf(helpText, p.API.GetCallPrefix(), p.API.GetCallPrefix()))
}

func (p *CustomCommandsPlugin) returnHelpAdd(channelID string) {
p.returnMessage(channelID, helpTextAdd)
p.returnMessage(channelID, fmt.Sprintf(helpTextAdd, p.API.GetCallPrefix(), p.API.GetCallPrefix()))
}

func (p *CustomCommandsPlugin) returnHelpRemove(channelID string) {
p.returnMessage(channelID, helpTextRemove)
p.returnMessage(channelID, fmt.Sprintf(helpTextRemove, p.API.GetCallPrefix(), p.API.GetCallPrefix()))
}

func (p *CustomCommandsPlugin) returnMessage(channelID, msg string) {
Expand Down
6 changes: 3 additions & 3 deletions plugin/customcommandsplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestCustomCommandsPlugin_HelpTextAndInvalidCommands(t *testing.T) {
postToPlugin.Content = "!" + command
expectedPostFromPlugin := model.Post{
ChannelID: "CHANNEL ID",
Content: helpText,
Content: fmt.Sprintf(helpText, api.GetCallPrefix(), p.API.GetCallPrefix()),
IsPrivate: false,
}
p.OnCommand(command, "", postToPlugin)
Expand All @@ -113,14 +113,14 @@ func TestCustomCommandsPlugin_HelpTextAndInvalidCommands(t *testing.T) {

api.Reset()
postToPlugin.Content = "!customcommand add"
expectedPostFromPlugin.Content = helpTextAdd
expectedPostFromPlugin.Content = fmt.Sprintf(helpTextAdd, api.GetCallPrefix(), p.API.GetCallPrefix())
p.OnCommand(command, "add", postToPlugin)
assert.Equal(true, api.WasCreatePostCalled)
assert.Equal(expectedPostFromPlugin, api.LastCreatePostPost)

api.Reset()
postToPlugin.Content = "!customcommand remove"
expectedPostFromPlugin.Content = helpTextRemove
expectedPostFromPlugin.Content = fmt.Sprintf(helpTextRemove, api.GetCallPrefix(), p.API.GetCallPrefix())
p.OnCommand(command, "remove", postToPlugin)
assert.Equal(true, api.WasCreatePostCalled)
assert.Equal(expectedPostFromPlugin, api.LastCreatePostPost)
Expand Down
6 changes: 3 additions & 3 deletions plugin/giveawayplugin/commandhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func parseTimeStringToDuration(timeStr string) (time.Duration, error) {
}

func (p *GiveawayPlugin) returnHelp(channelID string) {
p.returnMessage(channelID, "Type `"+command+" start <time> <secretword> [winners] [prize]` to start a new giveaway.")
p.returnMessage(channelID, fmt.Sprintf("Type `%s%s start <time> <secretword> [winners] [prize]` to start a new giveaway.", p.API.GetCallPrefix(), command))
}

func (p *GiveawayPlugin) returnMessage(channelID, msg string) {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (p *GiveawayPlugin) onCommandGEnd(post model.Post) {
return
}

p.returnMessage(post.ChannelID, "No giveaway running. Use giveaway start command to start a new one.")
p.returnMessage(post.ChannelID, fmt.Sprintf("No giveaway running. Use `%s%s start` command to start a new one.", p.API.GetCallPrefix(), command))
}

func (p *GiveawayPlugin) onCommandGReroll(post model.Post) {
Expand Down Expand Up @@ -126,5 +126,5 @@ func (p *GiveawayPlugin) onCommandGReroll(post model.Post) {
return
}

p.returnMessage(post.ChannelID, "No previous giveaway in that channel. Use `"+command+" start` command to start a new one.")
p.returnMessage(post.ChannelID, fmt.Sprintf("No previous giveaway in that channel. Use `%s%s start` command to start a new one.", p.API.GetCallPrefix(), command))
}
6 changes: 3 additions & 3 deletions plugin/giveawayplugin/giveaway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestGiveawayPluginHelpTextAndInvalidCommands(t *testing.T) {
IsPrivate: false,
}

expectedHelpMessage := "Type `giveaway start <time> <secretword> [winners] [prize]` to start a new giveaway."
expectedHelpMessage := "Type `!giveaway start <time> <secretword> [winners] [prize]` to start a new giveaway."

api.Reset()
postToPlugin.Content = contentCommandHelp
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGiveawayPluginCreateAndEndGiveaway(t *testing.T) {
postToPlugin.Content = contentCommandEnd
expectedPostFromPlugin := model.Post{
ChannelID: "CHANNEL ID",
Content: "No giveaway running. Use giveaway start command to start a new one.",
Content: "No giveaway running. Use `!giveaway start` command to start a new one.",
IsPrivate: false,
}
p.OnCommand(commandGiveaway, "end", postToPlugin)
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestGiveawayPluginCreateAndEndGiveawayAndReroll(t *testing.T) {
postToPlugin.Content = contentCommandReroll
expectedPostFromPlugin := model.Post{
ChannelID: "CHANNEL ID",
Content: "No previous giveaway in that channel. Use `giveaway start` command to start a new one.",
Content: "No previous giveaway in that channel. Use `!giveaway start` command to start a new one.",
IsPrivate: false,
}
p.OnCommand(commandGiveaway, "reroll", postToPlugin)
Expand Down
3 changes: 3 additions & 0 deletions plugin/mockapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (b *MockAPI) RegisterCommand(p Hooks, command string) error { return nil }
// UnRegisterCommand unregisters a command previously registered via RegisterCommand.
func (b *MockAPI) UnRegisterCommand(command string) error { return nil }

// GetCallPrefix returns the current command call prefix.
func (b *MockAPI) GetCallPrefix() string { return "!" }

// GetUsers a list of all users the bot knows.
func (b *MockAPI) GetUsers() ([]model.User, error) { return nil, nil }

Expand Down
10 changes: 5 additions & 5 deletions plugin/quotesplugin/commandhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ var now = time.Now
const (
identFieldList = "list"

helpText = "Type `" + command + " add <your quote>` to add a new quote."
helpTextRemove = "Type `" + command + " remove <your quote>` or `" + command + " remove (ID)` to remove a quote."
helpText = "Type `%s" + command + " add <your quote>` to add a new quote."
helpTextRemove = "Type `%s" + command + " remove <your quote>` or `%s" + command + " remove (ID)` to remove a quote."
)

func (p *QuotesPlugin) returnHelp(channelID string) {
p.returnMessage(channelID, helpText)
p.returnMessage(channelID, fmt.Sprintf(helpText, p.API.GetCallPrefix()))
}

func (p *QuotesPlugin) returnHelpRemove(channelID string) {
p.returnMessage(channelID, helpTextRemove)
p.returnMessage(channelID, fmt.Sprintf(helpTextRemove, p.API.GetCallPrefix(), p.API.GetCallPrefix()))
}

func (p *QuotesPlugin) returnMessage(channelID, msg string) {
Expand Down Expand Up @@ -170,7 +170,7 @@ func (p *QuotesPlugin) onCommandQuoteRemove(argument string, post model.Post) {
func (p *QuotesPlugin) onCommandQuote(argument string, post model.Post) {
currentList := p.getQuotesList()
if len(currentList.UUIDs) == 0 {
p.returnMessage(post.ChannelID, "No quotes found. Use the command `"+command+" add <your quote>` to add a new one.")
p.returnMessage(post.ChannelID, fmt.Sprintf("No quotes found. Use the command `%s%s add <your quote>` to add a new one.", p.API.GetCallPrefix(), command))
return
}

Expand Down
6 changes: 3 additions & 3 deletions plugin/quotesplugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestQuotesPlugin_HelpTextAndInvalidCommands(t *testing.T) {
postToPlugin.Content = "!" + commandAdd
expectedPostFromPlugin := model.Post{
ChannelID: "CHANNEL ID",
Content: helpText,
Content: fmt.Sprintf(helpText, api.GetCallPrefix()),
IsPrivate: false,
}
p.OnCommand(commandQuote, "add", postToPlugin)
Expand All @@ -94,7 +94,7 @@ func TestQuotesPlugin_HelpTextAndInvalidCommands(t *testing.T) {
postToPlugin.Content = "!" + commandRemove
expectedPostFromPlugin = model.Post{
ChannelID: "CHANNEL ID",
Content: helpTextRemove,
Content: fmt.Sprintf(helpTextRemove, api.GetCallPrefix(), api.GetCallPrefix()),
IsPrivate: false,
}
p.OnCommand(commandQuote, "remove", postToPlugin)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestQuotesPlugin_GetQuote(t *testing.T) {

expectedPostFromPlugin := model.Post{
ChannelID: "CHANNEL ID",
Content: "No quotes found. Use the command `" + command + " add <your quote>` to add a new one.",
Content: "No quotes found. Use the command `!" + command + " add <your quote>` to add a new one.",
IsPrivate: false,
}
p.OnCommand(commandQuote, "", postToPlugin)
Expand Down
Loading

0 comments on commit d37f639

Please sign in to comment.