github.com/starshine-sys/bcr@v0.21.0/bot/bot.go (about) 1 // Package bot provides a basic embeddable Bot struct for more easily handling commands 2 package bot 3 4 import ( 5 "context" 6 "sort" 7 8 "github.com/diamondburned/arikawa/v3/discord" 9 "github.com/starshine-sys/bcr" 10 ) 11 12 // Bot is the main bot struct 13 type Bot struct { 14 Router *bcr.Router 15 16 Modules []Module 17 } 18 19 // Module is a single module/category of commands 20 type Module interface { 21 String() string 22 Commands() []*bcr.Command 23 } 24 25 // New creates a new instance of Bot. 26 // The token will be prefixed with `Bot ` automatically. 27 func New(token string) (*Bot, error) { 28 r, err := bcr.NewWithState(token, []discord.UserID{}, []string{}) 29 if err != nil { 30 return nil, err 31 } 32 return NewWithRouter(r), nil 33 } 34 35 // NewWithRouter creates a new bot with the given router 36 func NewWithRouter(r *bcr.Router) *Bot { 37 return &Bot{ 38 Router: r, 39 } 40 } 41 42 // Prefix is a helper function to set the bot's router's prefixes 43 func (bot *Bot) Prefix(prefixes ...string) { 44 bot.Router.Prefixes = prefixes 45 } 46 47 // Owner is a helper function to set the bot's owner(s) 48 func (bot *Bot) Owner(owners ...discord.UserID) { 49 o := make([]string, 0) 50 for _, owner := range owners { 51 o = append(o, owner.String()) 52 } 53 bot.Router.BotOwners = o 54 } 55 56 // Add adds a module to the bot 57 func (bot *Bot) Add(f func(*Bot) (string, []*bcr.Command)) { 58 m, c := f(bot) 59 60 // sort the list of commands 61 sort.Sort(bcr.Commands(c)) 62 63 // add the module 64 bot.Modules = append(bot.Modules, &botModule{ 65 name: m, 66 commands: c, 67 }) 68 } 69 70 // Start wraps around Router.ShardManager.Open() 71 func (bot *Bot) Start(ctx context.Context) error { 72 return bot.Router.ShardManager.Open(ctx) 73 }