diff --git a/commanddispatcher/commanddispatcher.go b/commanddispatcher/commanddispatcher.go index d6e316c..978f11c 100644 --- a/commanddispatcher/commanddispatcher.go +++ b/commanddispatcher/commanddispatcher.go @@ -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 @@ -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, "" @@ -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 +} diff --git a/commanddispatcher/commanddispatcher_test.go b/commanddispatcher/commanddispatcher_test.go index ce17e5f..c0a4e07 100644 --- a/commanddispatcher/commanddispatcher_test.go +++ b/commanddispatcher/commanddispatcher_test.go @@ -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) + } + }) + } +} diff --git a/platform/bot.go b/platform/bot.go index 4148294..1eaa8ef 100644 --- a/platform/bot.go +++ b/platform/bot.go @@ -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() +} diff --git a/plugin/api.go b/plugin/api.go index 19b2f7f..0023b4f 100644 --- a/plugin/api.go +++ b/plugin/api.go @@ -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) diff --git a/plugin/customcommandsplugin/commandhandler.go b/plugin/customcommandsplugin/commandhandler.go index bd27005..63d79e5 100644 --- a/plugin/customcommandsplugin/commandhandler.go +++ b/plugin/customcommandsplugin/commandhandler.go @@ -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) @@ -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 } diff --git a/plugin/customcommandsplugin/help.go b/plugin/customcommandsplugin/help.go index 254019f..a0bec4c 100644 --- a/plugin/customcommandsplugin/help.go +++ b/plugin/customcommandsplugin/help.go @@ -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 ' to add the custom command '!customCommand'" - helpTextRemove = "Type '!customcommand remove to remove the custom command '!customCommand'" + helpText = "Use '%scustomcommand add' to add and '%scustomcommand remove' to remove a custom command" + helpTextAdd = "Type '%scustomcommand add ' to add the custom command '%scustomCommand'" + helpTextRemove = "Type '%scustomcommand remove 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) { diff --git a/plugin/customcommandsplugin/plugin_test.go b/plugin/customcommandsplugin/plugin_test.go index db0b472..1f7c1d1 100644 --- a/plugin/customcommandsplugin/plugin_test.go +++ b/plugin/customcommandsplugin/plugin_test.go @@ -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) @@ -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) diff --git a/plugin/giveawayplugin/commandhandler.go b/plugin/giveawayplugin/commandhandler.go index 11687c9..0061d6c 100644 --- a/plugin/giveawayplugin/commandhandler.go +++ b/plugin/giveawayplugin/commandhandler.go @@ -19,7 +19,7 @@ func parseTimeStringToDuration(timeStr string) (time.Duration, error) { } func (p *GiveawayPlugin) returnHelp(channelID string) { - p.returnMessage(channelID, "Type `"+command+" start