2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
2021-06-10 23:48:32 +00:00
|
|
|
"context"
|
2020-11-13 03:02:52 +00:00
|
|
|
"fmt"
|
2020-01-19 06:06:00 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
2020-01-24 03:17:03 +00:00
|
|
|
"sync"
|
2021-06-22 20:42:08 +00:00
|
|
|
"syscall"
|
2020-11-25 20:08:42 +00:00
|
|
|
"time"
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-07-29 23:29:01 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
2021-06-10 23:48:32 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/session"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/session/shard"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
2021-06-10 23:48:32 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/state/store/defaultstore"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/bot/extras/shellwords"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/handler"
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 20:25:42 +00:00
|
|
|
// Prefixer checks a message if it starts with the desired prefix. By default,
|
|
|
|
// NewPrefix() is used.
|
|
|
|
type Prefixer func(*gateway.MessageCreateEvent) (prefix string, ok bool)
|
|
|
|
|
|
|
|
// NewPrefix creates a simple prefix checker using strings. As the default
|
|
|
|
// prefix is "!", the function is called as NewPrefix("!").
|
|
|
|
func NewPrefix(prefixes ...string) Prefixer {
|
|
|
|
return func(msg *gateway.MessageCreateEvent) (string, bool) {
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
if strings.HasPrefix(msg.Content, prefix) {
|
|
|
|
return prefix, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 06:23:52 +00:00
|
|
|
// ArgsParser is the function type for parsing message content into fields,
|
|
|
|
// usually delimited by spaces.
|
|
|
|
type ArgsParser func(content string) ([]string, error)
|
|
|
|
|
|
|
|
// DefaultArgsParser implements a parser similar to that of shell's,
|
|
|
|
// implementing quotes as well as escapes.
|
2021-06-10 23:48:32 +00:00
|
|
|
var DefaultArgsParser = shellwords.Parse
|
|
|
|
|
|
|
|
// NewShardFunc creates a shard constructor that shares the same internal store.
|
|
|
|
// If opts sets its own cabinet, then a new store isn't created.
|
|
|
|
func NewShardFunc(fn func(*state.State) (*Context, error)) shard.NewShardFunc {
|
|
|
|
if fn == nil {
|
|
|
|
panic("bot.NewShardFunc missing fn")
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(m *shard.Manager, id *gateway.Identifier) (shard.Shard, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
sessn := session.NewCustom(*id, api.NewClient(id.Token), handler.New())
|
|
|
|
state := state.NewFromSession(sessn, defaultstore.New())
|
2021-06-10 23:48:32 +00:00
|
|
|
|
|
|
|
bot, err := fn(state)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create bot instance")
|
|
|
|
}
|
|
|
|
|
|
|
|
return bot, nil
|
|
|
|
}
|
2020-05-06 06:23:52 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-01-20 03:46:48 +00:00
|
|
|
// Context is the bot state for commands and subcommands.
|
|
|
|
//
|
2020-01-26 09:06:54 +00:00
|
|
|
// Commands
|
|
|
|
//
|
2020-01-20 03:46:48 +00:00
|
|
|
// A command can be created by making it a method of Commands, or whatever
|
|
|
|
// struct was given to the constructor. This following example creates a command
|
|
|
|
// with a single integer argument (which can be ran with "~example 123"):
|
|
|
|
//
|
2020-01-26 09:06:54 +00:00
|
|
|
// func (c *Commands) Example(
|
|
|
|
// m *gateway.MessageCreateEvent, i int) (string, error) {
|
|
|
|
//
|
|
|
|
// return fmt.Sprintf("You sent: %d", i)
|
2020-01-20 03:46:48 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Commands' exported methods will all be used as commands. Messages are parsed
|
|
|
|
// with its first argument (the command) mapped accordingly to c.MapName, which
|
|
|
|
// capitalizes the first letter automatically to reflect the exported method
|
|
|
|
// name.
|
2020-01-26 09:06:54 +00:00
|
|
|
//
|
|
|
|
// A command can either return either an error, or data and error. The only data
|
|
|
|
// types allowed are string, *discord.Embed, and *api.SendMessageData. Any other
|
|
|
|
// return types will invalidate the method.
|
|
|
|
//
|
|
|
|
// Events
|
|
|
|
//
|
|
|
|
// An event can only have one argument, which is the pointer to the event
|
|
|
|
// struct. It can also only return error.
|
|
|
|
//
|
|
|
|
// func (c *Commands) Example(o *gateway.TypingStartEvent) error {
|
|
|
|
// log.Println("Someone's typing!")
|
|
|
|
// return nil
|
|
|
|
// }
|
2020-01-19 06:06:00 +00:00
|
|
|
type Context struct {
|
|
|
|
*Subcommand
|
|
|
|
*state.State
|
|
|
|
|
|
|
|
// Descriptive (but optional) bot name
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Descriptive help body
|
|
|
|
Description string
|
|
|
|
|
2020-05-06 06:23:52 +00:00
|
|
|
// Called to parse message content, default to DefaultArgsParser().
|
|
|
|
ParseArgs ArgsParser
|
|
|
|
|
2020-04-06 20:25:42 +00:00
|
|
|
// Called to check a message's prefix. The default prefix is "!". Refer to
|
|
|
|
// NewPrefix().
|
|
|
|
HasPrefix Prefixer
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-04-10 04:46:21 +00:00
|
|
|
// AllowBot makes the router also process MessageCreate events from bots.
|
|
|
|
// This is false by default and only applies to MessageCreate.
|
|
|
|
AllowBot bool
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// QuietUnknownCommand, if true, will not make the bot reply with an unknown
|
|
|
|
// command error into the chat. This will apply to all other subcommands.
|
2021-06-06 19:40:24 +00:00
|
|
|
// SilentUnknown controls whether or not an UnknownCommandError should be
|
2020-05-14 03:42:31 +00:00
|
|
|
// returned (instead of a silent error).
|
|
|
|
SilentUnknown struct {
|
|
|
|
// Command when true will silent only unknown commands. Known
|
|
|
|
// subcommands with unknown commands will still error out.
|
|
|
|
Command bool
|
|
|
|
// Subcommand when true will suppress unknown subcommands.
|
|
|
|
Subcommand bool
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
// FormatError formats any errors returned by anything, including the method
|
|
|
|
// commands or the reflect functions. This also includes invalid usage
|
|
|
|
// errors or unknown command errors. Returning an empty string means
|
|
|
|
// ignoring the error.
|
2020-01-26 08:00:52 +00:00
|
|
|
//
|
|
|
|
// By default, this field replaces all @ with @\u200b, which prevents an
|
|
|
|
// @everyone mention.
|
2020-01-19 06:06:00 +00:00
|
|
|
FormatError func(error) string
|
|
|
|
|
|
|
|
// ErrorLogger logs any error that anything makes and the library can't
|
|
|
|
// reply to the client. This includes any event callback errors that aren't
|
|
|
|
// Message Create.
|
|
|
|
ErrorLogger func(error)
|
|
|
|
|
2020-04-09 20:49:12 +00:00
|
|
|
// ReplyError when true replies to the user the error. This only applies to
|
|
|
|
// MessageCreate events.
|
2020-01-19 06:06:00 +00:00
|
|
|
ReplyError bool
|
|
|
|
|
2021-04-08 20:09:10 +00:00
|
|
|
// ErrorReplier is an optional function that allows changing how the error
|
|
|
|
// is replied. It overrides ReplyError and is only used for MessageCreate
|
|
|
|
// events.
|
|
|
|
//
|
|
|
|
// Note that errors that are passed in here will bypas FormatError; in other
|
|
|
|
// words, the implementation might only care about ErrorReplier and leave
|
|
|
|
// FormatError as it is.
|
|
|
|
ErrorReplier func(err error, src *gateway.MessageCreateEvent) api.SendMessageData
|
|
|
|
|
2020-05-15 01:52:15 +00:00
|
|
|
// EditableCommands when true will also listen for MessageUpdateEvent and
|
|
|
|
// treat them as newly created messages. This is convenient if you want
|
|
|
|
// to quickly edit a message and re-execute the command.
|
|
|
|
EditableCommands bool
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// Subcommands contains all the registered subcommands. This is not
|
|
|
|
// exported, as it shouldn't be used directly.
|
|
|
|
subcommands []*Subcommand
|
|
|
|
|
|
|
|
// Quick access map from event types to pointers. This map will never have
|
|
|
|
// MessageCreateEvent's type.
|
|
|
|
typeCache sync.Map // map[reflect.Type][]*CommandContext
|
2021-06-10 23:48:32 +00:00
|
|
|
|
|
|
|
stopFunc func() // unbind function, see Start()
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start quickly starts a bot with the given command. It will prepend "Bot"
|
|
|
|
// into the token automatically. Refer to example/ for usage.
|
2020-07-11 19:50:32 +00:00
|
|
|
func Start(
|
|
|
|
token string, cmd interface{},
|
2020-03-01 02:13:58 +00:00
|
|
|
opts func(*Context) error) (wait func() error, err error) {
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-02-24 05:40:44 +00:00
|
|
|
if token == "" {
|
|
|
|
return nil, errors.New("token is not given")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(token, "Bot ") {
|
|
|
|
token = "Bot " + token
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
newShard := NewShardFunc(func(s *state.State) (*Context, error) {
|
|
|
|
ctx, err := New(s, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// fail api request if they (will) take up more than 5 minutes
|
|
|
|
ctx.Client.Client.Timeout = 5 * time.Minute
|
2020-11-25 20:08:42 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
if opts != nil {
|
|
|
|
if err := opts(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.AddIntents(ctx.DeriveIntents())
|
|
|
|
ctx.AddIntents(gateway.IntentGuilds) // for channel event caching
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
m, err := shard.NewManager(token, newShard)
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
2021-06-10 23:48:32 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create shard manager")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 04:10:56 +00:00
|
|
|
if err := m.Open(context.Background()); err != nil {
|
2021-06-10 23:48:32 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to open")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
return func() error {
|
|
|
|
WaitForInterrupt()
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// Close the shards first.
|
|
|
|
closeErr := m.Close()
|
2020-10-29 05:49:18 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// Remove all handlers to clean up.
|
|
|
|
m.ForEach(func(s shard.Shard) {
|
|
|
|
ctx := s.(*Context)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
stop := ctx.Start()
|
|
|
|
stop()
|
|
|
|
})
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
return closeErr
|
2020-01-19 06:06:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 05:40:44 +00:00
|
|
|
// Run starts the bot, prints a message into the console, and blocks until
|
|
|
|
// SIGINT. "Bot" is prepended into the token automatically, similar to Start.
|
|
|
|
// The function will call os.Exit(1) on an initialization or cleanup error.
|
|
|
|
func Run(token string, cmd interface{}, opts func(*Context) error) {
|
|
|
|
wait, err := Start(token, cmd, opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("failed to start:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Bot is running.")
|
|
|
|
|
|
|
|
if err := wait(); err != nil {
|
|
|
|
log.Fatalln("cleanup error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// WaitForInterrupt blocks until SIGINT.
|
|
|
|
func WaitForInterrupt() {
|
2020-07-29 23:29:01 +00:00
|
|
|
sigs := make(chan os.Signal, 1)
|
2021-06-22 20:42:08 +00:00
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
2020-01-19 06:06:00 +00:00
|
|
|
<-sigs
|
|
|
|
}
|
|
|
|
|
|
|
|
// New makes a new context with a "~" as the prefix. cmds must be a pointer to a
|
|
|
|
// struct with a *Context field. Example:
|
|
|
|
//
|
|
|
|
// type Commands struct {
|
|
|
|
// Ctx *Context
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// cmds := &Commands{}
|
2020-05-14 03:42:31 +00:00
|
|
|
// c, err := bot.New(session, cmds)
|
2020-01-19 06:06:00 +00:00
|
|
|
//
|
|
|
|
// The default prefix is "~", which means commands must start with "~" followed
|
|
|
|
// by the command name in the first argument, else it will be ignored.
|
|
|
|
//
|
|
|
|
// c.Start() should be called afterwards to actually handle incoming events.
|
|
|
|
func New(s *state.State, cmd interface{}) (*Context, error) {
|
|
|
|
c, err := NewSubcommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &Context{
|
|
|
|
Subcommand: c,
|
|
|
|
State: s,
|
2021-06-10 23:48:32 +00:00
|
|
|
ParseArgs: DefaultArgsParser,
|
2020-04-06 20:25:42 +00:00
|
|
|
HasPrefix: NewPrefix("~"),
|
2020-01-19 06:06:00 +00:00
|
|
|
FormatError: func(err error) string {
|
2020-01-26 08:00:52 +00:00
|
|
|
// Escape all pings, including @everyone.
|
|
|
|
return strings.Replace(err.Error(), "@", "@\u200b", -1)
|
2020-01-19 06:06:00 +00:00
|
|
|
},
|
|
|
|
ErrorLogger: func(err error) {
|
|
|
|
log.Println("Bot error:", err)
|
|
|
|
},
|
|
|
|
ReplyError: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.InitCommands(ctx); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to initialize with given cmds")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2020-10-29 05:49:18 +00:00
|
|
|
// AddIntents adds the given Gateway Intent into the Gateway. This is a
|
2020-07-11 19:50:32 +00:00
|
|
|
// convenient function that calls Gateway's AddIntent.
|
2020-10-29 05:49:18 +00:00
|
|
|
func (ctx *Context) AddIntents(i gateway.Intents) {
|
2021-09-28 20:19:04 +00:00
|
|
|
ctx.Session.AddIntents(i)
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 20:25:42 +00:00
|
|
|
// Subcommands returns the slice of subcommands. To add subcommands, use
|
|
|
|
// RegisterSubcommand().
|
2020-01-24 03:17:03 +00:00
|
|
|
func (ctx *Context) Subcommands() []*Subcommand {
|
|
|
|
// Getter is not useless, refer to the struct doc for reason.
|
|
|
|
return ctx.subcommands
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// FindMethod finds a method based on the struct and method name. The queried
|
2020-01-24 03:17:03 +00:00
|
|
|
// names will have their flags stripped.
|
|
|
|
//
|
|
|
|
// // Find a command from the main context:
|
2020-05-10 08:45:00 +00:00
|
|
|
// cmd := ctx.FindMethod("", "Method")
|
2020-01-24 03:17:03 +00:00
|
|
|
// // Find a command from a subcommand:
|
2020-05-10 08:45:00 +00:00
|
|
|
// cmd = ctx.FindMethod("Starboard", "Reset")
|
2020-01-24 03:17:03 +00:00
|
|
|
//
|
2020-07-29 23:29:01 +00:00
|
|
|
func (ctx *Context) FindCommand(structName, methodName string) *MethodContext {
|
|
|
|
if structName == "" {
|
|
|
|
return ctx.Subcommand.FindCommand(methodName)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
for _, sub := range ctx.subcommands {
|
2020-07-29 23:29:01 +00:00
|
|
|
if sub.StructName == structName {
|
|
|
|
return sub.FindCommand(methodName)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustRegisterSubcommand tries to register a subcommand, and will panic if it
|
|
|
|
// fails. This is recommended, as subcommands won't change after initializing
|
|
|
|
// once in runtime, thus fairly harmless after development.
|
2020-11-13 03:02:52 +00:00
|
|
|
//
|
|
|
|
// If no names are given or if the first name is empty, then the subcommand name
|
|
|
|
// will be derived from the struct name. If one name is given, then that name
|
|
|
|
// will override the struct name. Any other name values will be aliases.
|
|
|
|
//
|
|
|
|
// It is recommended to use this method to add subcommand aliases over manually
|
|
|
|
// altering the Aliases slice of each Subcommand, as it does collision checks
|
|
|
|
// against other subcommands as well.
|
|
|
|
func (ctx *Context) MustRegisterSubcommand(cmd interface{}, names ...string) *Subcommand {
|
|
|
|
s, err := ctx.RegisterSubcommand(cmd, names...)
|
2020-01-22 07:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterSubcommand registers and adds cmd to the list of subcommands. It will
|
2020-11-13 03:02:52 +00:00
|
|
|
// also return the resulting Subcommand. Refer to MustRegisterSubcommand for the
|
|
|
|
// names argument.
|
|
|
|
func (ctx *Context) RegisterSubcommand(cmd interface{}, names ...string) (*Subcommand, error) {
|
2020-01-19 06:06:00 +00:00
|
|
|
s, err := NewSubcommand(cmd)
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to add subcommand")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the subcommand's name.
|
|
|
|
s.NeedsName()
|
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
if len(names) > 0 && names[0] != "" {
|
|
|
|
s.Command = names[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(names) > 1 {
|
|
|
|
// Copy the slice for expected behaviors.
|
|
|
|
s.Aliases = append([]string(nil), names[1:]...)
|
2020-05-14 03:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
if err := s.InitCommands(ctx); err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to initialize subcommand")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
// Check if the existing command name already exists. This could really be
|
|
|
|
// optimized, but since it's in a cold path, who cares.
|
|
|
|
var subcommandNames = append([]string{s.Command}, s.Aliases...)
|
|
|
|
|
|
|
|
for _, name := range subcommandNames {
|
|
|
|
for _, sub := range ctx.subcommands {
|
|
|
|
// Check each alias against the subcommand name.
|
|
|
|
if sub.Command == name {
|
|
|
|
return nil, fmt.Errorf("new subcommand has duplicate name: %q", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also check each alias against other subcommands' aliases.
|
|
|
|
for _, subalias := range sub.Aliases {
|
|
|
|
if subalias == name {
|
|
|
|
return nil, fmt.Errorf("new subcommand has duplicate alias: %q", name)
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
ctx.subcommands = append(ctx.subcommands, s)
|
2020-01-19 06:06:00 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
// emptyMentionTypes is used by Start() to not parse any mentions.
|
|
|
|
var emptyMentionTypes = []api.AllowedMentionType{}
|
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
// Start adds itself into the session handlers. If Start is called more than
|
|
|
|
// once, then it does nothing. The caller doesn't have to call Start if they
|
|
|
|
// call Open.
|
|
|
|
//
|
|
|
|
// The returned function is a delete function, which removes itself from the
|
|
|
|
// Session handlers. The delete function is not safe to use concurrently.
|
2020-01-19 06:06:00 +00:00
|
|
|
func (ctx *Context) Start() func() {
|
2021-06-10 23:48:32 +00:00
|
|
|
if ctx.stopFunc == nil {
|
|
|
|
cancel := ctx.State.AddHandler(func(v interface{}) {
|
|
|
|
if err := ctx.callCmd(v); err != nil {
|
|
|
|
ctx.ErrorLogger(errors.Wrap(err, "command error"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.stopFunc = func() {
|
|
|
|
cancel()
|
|
|
|
ctx.stopFunc = nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2021-06-10 23:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.stopFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open starts the bot context and the gateway connection. It automatically
|
|
|
|
// binds the needed handlers.
|
|
|
|
func (ctx *Context) Open(cancelCtx context.Context) error {
|
|
|
|
ctx.Start()
|
|
|
|
return ctx.State.Open(cancelCtx)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call should only be used if you know what you're doing.
|
|
|
|
func (ctx *Context) Call(event interface{}) error {
|
|
|
|
return ctx.callCmd(event)
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// Help generates a full Help message. It serves mainly as a reference for
|
2020-07-14 23:33:21 +00:00
|
|
|
// people to reimplement and change. It doesn't show hidden commands.
|
2020-01-19 06:06:00 +00:00
|
|
|
func (ctx *Context) Help() string {
|
2020-07-14 23:33:21 +00:00
|
|
|
return ctx.HelpGenerate(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HelpGenerate generates a full Help message. It serves mainly as a reference
|
|
|
|
// for people to reimplement and change. If showHidden is true, then hidden
|
|
|
|
// subcommands and commands will be shown.
|
|
|
|
func (ctx *Context) HelpGenerate(showHidden bool) string {
|
2020-05-14 03:42:31 +00:00
|
|
|
// Generate the header.
|
|
|
|
buf := strings.Builder{}
|
|
|
|
buf.WriteString("__Help__")
|
2020-01-24 06:20:02 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// Name an
|
|
|
|
if ctx.Name != "" {
|
|
|
|
buf.WriteString(": " + ctx.Name)
|
|
|
|
}
|
|
|
|
if ctx.Description != "" {
|
|
|
|
buf.WriteString("\n" + IndentLines(ctx.Description))
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// Separators
|
|
|
|
buf.WriteString("\n---\n")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// Generate all commands
|
|
|
|
if help := ctx.Subcommand.Help(); help != "" {
|
|
|
|
buf.WriteString("__Commands__\n")
|
|
|
|
buf.WriteString(IndentLines(help))
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
var subcommands = ctx.Subcommands()
|
|
|
|
var subhelps = make([]string, 0, len(subcommands))
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
for _, sub := range subcommands {
|
2020-07-14 23:33:21 +00:00
|
|
|
if sub.Hidden && !showHidden {
|
2020-05-14 03:42:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-07-14 23:33:21 +00:00
|
|
|
help := sub.HelpShowHidden(showHidden)
|
2020-05-14 03:42:31 +00:00
|
|
|
if help == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-13 03:02:52 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
help = IndentLines(help)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
builder := strings.Builder{}
|
|
|
|
builder.WriteString("**")
|
|
|
|
builder.WriteString(sub.Command)
|
|
|
|
builder.WriteString("**")
|
|
|
|
|
|
|
|
for _, alias := range sub.Aliases {
|
|
|
|
builder.WriteString("|")
|
|
|
|
builder.WriteString("**")
|
|
|
|
builder.WriteString(alias)
|
|
|
|
builder.WriteString("**")
|
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if sub.Description != "" {
|
2020-11-13 03:02:52 +00:00
|
|
|
builder.WriteString(": ")
|
|
|
|
builder.WriteString(sub.Description)
|
2020-05-14 03:42:31 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-11-13 03:02:52 +00:00
|
|
|
builder.WriteByte('\n')
|
|
|
|
builder.WriteString(help)
|
|
|
|
|
|
|
|
subhelps = append(subhelps, builder.String())
|
2020-05-14 03:42:31 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if len(subhelps) > 0 {
|
|
|
|
buf.WriteString("---\n")
|
|
|
|
buf.WriteString("__Subcommands__\n")
|
|
|
|
buf.WriteString(IndentLines(strings.Join(subhelps, "\n")))
|
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
return buf.String()
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// IndentLine prefixes every line from input with a single-level indentation.
|
|
|
|
func IndentLines(input string) string {
|
|
|
|
const indent = " "
|
|
|
|
var lines = strings.Split(input, "\n")
|
|
|
|
for i := range lines {
|
|
|
|
lines[i] = indent + lines[i]
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-10-29 05:49:18 +00:00
|
|
|
|
|
|
|
// DeriveIntents derives all possible gateway intents from this context and all
|
|
|
|
// its subcommands' method handlers and middlewares.
|
|
|
|
func (ctx *Context) DeriveIntents() gateway.Intents {
|
|
|
|
var intents = ctx.Subcommand.DeriveIntents()
|
|
|
|
for _, subcmd := range ctx.subcommands {
|
|
|
|
intents |= subcmd.DeriveIntents()
|
|
|
|
}
|
|
|
|
return intents
|
|
|
|
}
|