Examples: Fixed up advanced_bot to make it up to date

This commit is contained in:
diamondburned 2020-05-14 18:01:48 -07:00
parent 7d683a2ace
commit fe950de9e0
2 changed files with 22 additions and 49 deletions

View File

@ -21,6 +21,7 @@ type Bot struct {
}
func (bot *Bot) Setup(sub *bot.Subcommand) {
// Only allow people in guilds to run guildInfo.
sub.AddMiddleware("GuildInfo", middlewares.GuildOnly(bot.Ctx))
}
@ -30,32 +31,24 @@ func (bot *Bot) Help(m *gateway.MessageCreateEvent) (string, error) {
}
// Add demonstrates the usage of typed arguments. Run it with "~add 1 2".
func (bot *Bot) Add(m *gateway.MessageCreateEvent, a, b int) error {
content := fmt.Sprintf("%d + %d = %d", a, b, a+b)
_, err := bot.Ctx.SendMessage(m.ChannelID, content, nil)
return err
func (bot *Bot) Add(m *gateway.MessageCreateEvent, a, b int) (string, error) {
return fmt.Sprintf("%d + %d = %d", a, b, a+b), nil
}
// Ping is a simple ping example, perhaps the most simple you could make it.
func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error {
_, err := bot.Ctx.SendMessage(m.ChannelID, "Pong!", nil)
return err
func (bot *Bot) Ping(m *gateway.MessageCreateEvent) (string, error) {
return "Pong!", nil
}
// Say demonstrates how arguments.Flag could be used without the flag library.
func (bot *Bot) Say(m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) {
args := f.String()
if args == "" {
// Empty message, ignore
return "", nil
func (bot *Bot) Say(m *gateway.MessageCreateEvent, f bot.RawArguments) (string, error) {
if f != "" {
return string(f), nil
}
return args, nil
return "", errors.New("Missing content.")
}
// GuildInfo demonstrates the use of command flags, in this case the GuildOnly
// flag.
// GuildInfo demonstrates the GuildOnly middleware done in (*Bot).Setup().
func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) {
g, err := bot.Ctx.GuildWithCount(m.GuildID)
if err != nil {
@ -71,8 +64,7 @@ func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) {
// Repeat tells the bot to wait for the user's response, then repeat what they
// said.
func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
_, err := bot.Ctx.SendMessage(m.ChannelID,
"What do you want me to say?", nil)
_, err := bot.Ctx.SendMessage(m.ChannelID, "What do you want me to say?", nil)
if err != nil {
return "", err
}
@ -80,6 +72,8 @@ func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// This might miss events that are sent immediately after. To make sure all
// events are caught, ChanFor should be used.
v := bot.Ctx.WaitFor(ctx, func(v interface{}) bool {
// Incoming event is a message create event:
mg, ok := v.(*gateway.MessageCreateEvent)
@ -101,7 +95,7 @@ func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
// Embed is a simple embed creator. Its purpose is to demonstrate the usage of
// the ParseContent interface, as well as using the stdlib flag package.
func (bot *Bot) Embed(m *gateway.MessageCreateEvent, f *arguments.Flag) (*discord.Embed, error) {
func (bot *Bot) Embed(m *gateway.MessageCreateEvent, f arguments.Flag) (*discord.Embed, error) {
fs := arguments.NewFlagSet()
var (

View File

@ -4,7 +4,6 @@ import (
"bytes"
"flag"
"io/ioutil"
"strings"
)
var FlagName = "command"
@ -30,41 +29,21 @@ func (fs *FlagSet) Usage() string {
return buf.String()
}
type Flag struct {
command string
arguments []string
}
type Flag []string
func (f *Flag) ParseContent(arguments []string) error {
// trim the command out
f.command, f.arguments = arguments[0], arguments[1:]
*f = arguments
return nil
}
func (f *Flag) Usage() string {
return "[flags] arguments..."
func (f Flag) Usage() string {
return "[flags] arguments"
}
func (f *Flag) Command() string {
return f.command
func (f Flag) Args() []string {
return f
}
func (f *Flag) Args() []string {
return f.arguments
}
func (f *Flag) Arg(n int) string {
if n < 0 || n >= len(f.arguments) {
return ""
}
return f.arguments[n]
}
func (f *Flag) String() string {
return strings.Join(f.arguments, " ")
}
func (f *Flag) With(fs *flag.FlagSet) error {
return fs.Parse(f.arguments)
func (f Flag) With(fs *flag.FlagSet) error {
return fs.Parse(f)
}