Permalink
Browse files

Improved help substantially, allowed it to be set to private only. Im…

…proved reminders.
  • Loading branch information...
1 parent 8fe2384 commit 46d4fcd8f4a6fed99b65232b4e66bbfa5492df0c @iopred committed Apr 21, 2016
View
@@ -62,6 +62,7 @@ func (b *Bot) RegisterService(service Service) {
Service: service,
Plugins: make(map[string]Plugin, 0),
}
+ b.RegisterPlugin(service, NewHelpPlugin())
}
// RegisterPlugin registers a plugin on a service.
View
@@ -77,9 +77,6 @@ func main() {
// Generally CommandPlugins don't hold state, so we share one instance of the command plugin for all services.
cp := bruxism.NewCommandPlugin()
- cp.AddCommand("help", bruxism.HelpCommand, bruxism.HelpHelp)
- cp.AddCommand("command", bruxism.HelpCommand, nil)
- cp.AddCommand("commands", bruxism.HelpCommand, nil)
cp.AddCommand("invite", inviteplugin.InviteCommand, inviteplugin.InviteHelp)
cp.AddCommand("join", inviteplugin.InviteCommand, nil)
cp.AddCommand("stats", statsplugin.StatsCommand, statsplugin.StatsHelp)
@@ -24,21 +24,17 @@ type comicPlugin struct {
func (p *comicPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
help := []string{
- bruxism.CommandHelp(service, "comic", "[<1-10>]", "Creates a comic from recent messages.")[0],
- bruxism.CommandHelp(service, "customcomic", "[<id>:] <text> | [<id>:] <text>", "Creates a custom comic.")[0],
- bruxism.CommandHelp(service, "customcomicsimple", "[<id>:] <text> | [<id>:] <text>", "Creates a simple custom comic.")[0],
+ bruxism.CommandHelp(service, "comic", "[1-10]", "Creates a comic from recent messages, or a number of messages if provided.")[0],
}
if detailed {
- ticks := ""
- if service.Name() == bruxism.DiscordServiceName {
- ticks = "`"
- }
help = append(help, []string{
+ bruxism.CommandHelp(service, "customcomic", "[id:] <text> | [id:] <text>", "Creates a custom comic.")[0],
+ bruxism.CommandHelp(service, "customcomicsimple", "[id:] <text> | [id:] <text>", "Creates a simple custom comic.")[0],
"Examples:",
- fmt.Sprintf("%s%scomic 5%s - Creates a comic from the last 5 messages.", ticks, service.CommandPrefix(), ticks),
- fmt.Sprintf("%s%scustomcomic Hello | 1: World | Yeah!%s - Creates a comic with 3 lines, with the second line being spoken by a different character.", ticks, service.CommandPrefix(), ticks),
- fmt.Sprintf("%s%scustomcomicsimple Foo | 1: Bar%s - Creates a comic with 2 lines, both spoken by different characters.", ticks, service.CommandPrefix(), ticks),
+ bruxism.CommandHelp(service, "comic", "5", "Creates a comic from the last 5 messages")[0],
+ bruxism.CommandHelp(service, "customcomic", "Hello | 1: World | Yeah", "Creates a comic with 3 lines, with the second line being spoken by a different character.")[0],
+ bruxism.CommandHelp(service, "customcomicsimple", "Foo | 1: Bar", "Creates a comic with 2 lines, both spoken by different characters.")[0],
}...)
}
View
@@ -133,7 +133,7 @@ func (p *CommandPlugin) Help(bot *Bot, service Service, message Message, detaile
// Message handler.
// Iterates over the registered commands and executes them if the message matches.
func (p *CommandPlugin) Message(bot *Bot, service Service, message Message) {
- // defer MessageRecover()
+ defer MessageRecover()
if !service.IsMe(message) {
for commandString, command := range p.commands {
if MatchesCommand(service, commandString, message) {
View
@@ -228,6 +228,11 @@ func (d *Discord) PrivateMessage(userID, message string) error {
return d.SendMessage(c.ID, message)
}
+// SupportsPrivateMessages returns whether the service supports private messages.
+func (d *Discord) SupportsPrivateMessages() bool {
+ return true
+}
+
// SupportsMultiline returns whether the service supports multiline messages.
func (d *Discord) SupportsMultiline() bool {
return true
@@ -42,7 +42,7 @@ func avatarHelpFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.M
if detailed {
return nil
}
- return bruxism.CommandHelp(service, "avatar", "[<@username>]", "Returns a big version of an avatar.")
+ return bruxism.CommandHelp(service, "avatar", "[@username]", "Returns a big version of your avatar, or a users avatar if provided.")
}
// New creates a new discordavatar plugin.
View
@@ -1,14 +1,27 @@
package bruxism
import (
+ "encoding/json"
"fmt"
"log"
"sort"
"strings"
)
-// HelpHelp returns help for the help plugin.
-func HelpHelp(bot *Bot, service Service, message Message) (string, string) {
+type helpPlugin struct {
+ Private map[string]bool
+}
+
+func (p *helpPlugin) Name() string {
+ return "Help"
+}
+
+func (p *helpPlugin) Help(bot *Bot, service Service, message Message, detailed bool) []string {
+ privs := service.SupportsPrivateMessages() && !service.IsPrivate(message) && (service.IsBotOwner(message) || service.IsModerator(message))
+ if detailed && !privs {
+ return nil
+ }
+
ticks := ""
if service.Name() == DiscordServiceName {
ticks = "`"
@@ -17,58 +30,125 @@ func HelpHelp(bot *Bot, service Service, message Message) (string, string) {
commands := []string{}
for _, plugin := range bot.Services[service.Name()].Plugins {
- t := plugin.Help(bot, service, message, true)
+ hasDetailed := false
- if t != nil && len(t) > 0 {
+ if plugin == p {
+ hasDetailed = privs
+ } else {
+ t := plugin.Help(bot, service, message, true)
+ hasDetailed = t != nil && len(t) > 0
+ }
+
+ if hasDetailed {
commands = append(commands, strings.ToLower(plugin.Name()))
}
}
sort.Strings(commands)
- return "[<topic>]", fmt.Sprintf("Returns generic help or help for a specific topic. Available topics: %s%s%s", ticks, strings.Join(commands, ", "), ticks)
+ help := []string{
+ CommandHelp(service, "help", "[topic]", fmt.Sprintf("Returns help for a specific topic. Available topics: %s%s%s", ticks, strings.Join(commands, ", "), ticks))[0],
+ }
+
+ if detailed {
+ help = append(help, []string{
+ CommandHelp(service, "setprivatehelp", "", "Sets help text to be sent through private messages in this channel.")[0],
+ CommandHelp(service, "setpublichelp", "", "Sets the default help behavior for this channel.")[0],
+ }...)
+ }
+
+ return help
}
-// HelpCommand is a command for returning help text for all registered plugins on a service.
-func HelpCommand(bot *Bot, service Service, message Message, command string, parts []string) {
- help := []string{}
+func (p *helpPlugin) Message(bot *Bot, service Service, message Message) {
+ if !service.IsMe(message) {
+ if MatchesCommand(service, "help", message) || MatchesCommand(service, "command", message) {
+ _, parts := ParseCommand(service, message)
- for _, plugin := range bot.Services[service.Name()].Plugins {
- var h []string
- if len(parts) == 0 {
- h = plugin.Help(bot, service, message, false)
- } else if len(parts) == 1 && strings.ToLower(parts[0]) == strings.ToLower(plugin.Name()) {
- h = plugin.Help(bot, service, message, true)
- }
- if h != nil && len(h) > 0 {
- help = append(help, h...)
- }
- }
+ help := []string{}
- if len(parts) == 0 {
- sort.Strings(help)
- help = append([]string{fmt.Sprintf("All commands can be used in private messages without the `%s` prefix.", service.CommandPrefix())}, help...)
- }
+ for _, plugin := range bot.Services[service.Name()].Plugins {
+ var h []string
+ if len(parts) == 0 {
+ h = plugin.Help(bot, service, message, false)
+ } else if len(parts) == 1 && strings.ToLower(parts[0]) == strings.ToLower(plugin.Name()) {
+ h = plugin.Help(bot, service, message, true)
+ }
+ if h != nil && len(h) > 0 {
+ help = append(help, h...)
+ }
+ }
- if len(parts) != 0 && len(help) == 0 {
- help = []string{fmt.Sprintf("Unknown topic: %s", parts[0])}
- }
+ if len(parts) == 0 {
+ sort.Strings(help)
+ help = append([]string{fmt.Sprintf("All commands can be used in private messages without the `%s` prefix.", service.CommandPrefix())}, help...)
+ }
- if service.SupportsMultiline() {
- if service.Name() == DiscordServiceName {
- if err := service.PrivateMessage(message.UserID(), strings.Join(help, "\n")); err != nil {
- log.Println(err)
+ if len(parts) != 0 && len(help) == 0 {
+ help = []string{fmt.Sprintf("Unknown topic: %s", parts[0])}
}
- } else {
- if err := service.SendMessage(message.Channel(), strings.Join(help, "\n")); err != nil {
- log.Println(err)
+
+ if p.Private[message.Channel()] {
+ service.SendMessage(message.Channel(), "Help has been sent via private message.")
+ if service.SupportsMultiline() {
+ service.PrivateMessage(message.UserID(), strings.Join(help, "\n"))
+ } else {
+ for _, h := range help {
+ if err := service.PrivateMessage(message.UserID(), h); err != nil {
+ break
+ }
+ }
+ }
+ } else {
+ if service.SupportsMultiline() {
+ service.SendMessage(message.Channel(), strings.Join(help, "\n"))
+ } else {
+ for _, h := range help {
+ if err := service.SendMessage(message.Channel(), h); err != nil {
+ break
+ }
+ }
+ }
}
- }
- } else {
- for _, h := range help {
- if err := service.SendMessage(message.Channel(), h); err != nil {
- log.Println(err)
+ } else if MatchesCommand(service, "setprivatehelp", message) && service.SupportsPrivateMessages() && !service.IsPrivate(message) {
+ if !service.IsBotOwner(message) && !service.IsModerator(message) {
+ return
}
+
+ p.Private[message.Channel()] = true
+
+ service.PrivateMessage(message.UserID(), fmt.Sprintf("Help text in <#%s> will be sent through private messages.", message.Channel()))
+ } else if MatchesCommand(service, "setpublichelp", message) && service.SupportsPrivateMessages() && !service.IsPrivate(message) {
+ if !service.IsBotOwner(message) && !service.IsModerator(message) {
+ return
+ }
+
+ p.Private[message.Channel()] = false
+
+ service.PrivateMessage(message.UserID(), fmt.Sprintf("Help text in <#%s> will be sent publically.", message.Channel()))
}
}
}
+
+// Load will load plugin state from a byte array.
+func (p *helpPlugin) Load(bot *Bot, service Service, data []byte) error {
+ if data != nil {
+ if err := json.Unmarshal(data, p); err != nil {
+ log.Println("Error loading data", err)
+ }
+ }
+ return nil
+}
+
+// Save will save plugin state to a byte array.
+func (p *helpPlugin) Save() ([]byte, error) {
+ return json.Marshal(p)
+}
+
+// NeHelpPlugin will create a new help plugin.
+func NewHelpPlugin() Plugin {
+ p := &helpPlugin{
+ Private: make(map[string]bool),
+ }
+ return p
+}
View
@@ -50,6 +50,7 @@ type Service interface {
IsBotOwner(message Message) bool
IsPrivate(message Message) bool
IsModerator(message Message) bool
+ SupportsPrivateMessages() bool
SupportsMultiline() bool
CommandPrefix() string
ChannelCount() int
View
@@ -169,6 +169,11 @@ func (i *IRC) PrivateMessage(userID, message string) error {
return i.SendMessage(userID, message)
}
+// SupportsPrivateMessages returns whether the service supports private messages.
+func (i *IRC) SupportsPrivateMessages() bool {
+ return true
+}
+
// SupportsMultiline returns whether the service supports multiline messages.
func (i *IRC) SupportsMultiline() bool {
return false
@@ -120,15 +120,15 @@ func (p *livePlugin) Save() ([]byte, error) {
// Help returns a list of help strings that are printed when the user requests them.
func (p *livePlugin) Help(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message, detailed bool) []string {
c, err := p.discord.Session.State.Channel(message.Channel())
- if err != nil || c.GuildID != livePluginGuildID {
+ if (err != nil || c.GuildID != livePluginGuildID) && !service.IsPrivate(message) {
return nil
}
if detailed {
return []string{
fmt.Sprintf("Announces when you go live in <#%s> as well as an optional channel.", livePluginChannelID),
bruxism.CommandHelp(service, "setyoutubechannel", "<youtube channel id>", "Sets your youtube channel id.")[0],
- bruxism.CommandHelp(service, "setdiscordchannel", "", "Will additionally announce you going live in this channel.")[0],
+ bruxism.CommandHelp(service, "setdiscordchannel", "", fmt.Sprintf("%s will also announce you going live in this channel.", service.UserName()))[0],
bruxism.CommandHelp(service, "unsetdiscordchannel", "", "Disables additional live announcement of channel.")[0],
"Example:",
fmt.Sprintf("`%ssetyoutubechannel UC392dac34_32fafe2deadbeef`", service.CommandPrefix()),
@@ -164,7 +164,7 @@ func (p *livePlugin) Message(bot *bruxism.Bot, service bruxism.Service, message
p.pollChannel(bot, service, lc)
- service.SendMessage(messageChannel, fmt.Sprintf("YouTube Channel ID set. A message will be posted to %s when you go live.", "https://discord.gg/0huaakl2TuIAkv97"))
+ service.SendMessage(messageChannel, fmt.Sprintf("YouTube Channel ID set. A message will be posted to <#%s> when you go live.", livePluginChannelID))
} else {
service.SendMessage(messageChannel, "Sorry, please provide a YouTube Channel ID. eg: setyoutubechannel UC392dac34_32fafe2deadbeef")
}
@@ -64,4 +64,4 @@ func NumberTriviaCommand(bot *bruxism.Bot, service bruxism.Service, message brux
}
// NumberTriviaHelp is the help for the number trivia plugin.
-var NumberTriviaHelp = bruxism.NewCommandHelp("[<number>]", "Returns trivia for a random number or a specified number if provided.")
+var NumberTriviaHelp = bruxism.NewCommandHelp("[number]", "Returns trivia for a random number, or a specified number if provided.")
@@ -46,8 +46,7 @@ func (p *playingPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, mess
return nil
}
- discord := service.(*bruxism.Discord)
- if discord.OwnerUserID != "" && !service.IsBotOwner(message) {
+ if !service.IsBotOwner(message) {
return nil
}
@@ -58,8 +57,7 @@ func (p *playingPlugin) helpFunc(bot *bruxism.Bot, service bruxism.Service, mess
func (p *playingPlugin) messageFunc(bot *bruxism.Bot, service bruxism.Service, message bruxism.Message) {
if !service.IsMe(message) {
if bruxism.MatchesCommand(service, "playing", message) {
- discord := service.(*bruxism.Discord)
- if discord.OwnerUserID != "" && !service.IsBotOwner(message) {
+ if !service.IsBotOwner(message) {
return
}
Oops, something went wrong.

0 comments on commit 46d4fcd

Please sign in to comment.