1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-07 12:38:05 +00:00

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) { func (bot *Bot) Setup(sub *bot.Subcommand) {
// Only allow people in guilds to run guildInfo.
sub.AddMiddleware("GuildInfo", middlewares.GuildOnly(bot.Ctx)) 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". // Add demonstrates the usage of typed arguments. Run it with "~add 1 2".
func (bot *Bot) Add(m *gateway.MessageCreateEvent, a, b int) error { func (bot *Bot) Add(m *gateway.MessageCreateEvent, a, b int) (string, error) {
content := fmt.Sprintf("%d + %d = %d", a, b, a+b) return fmt.Sprintf("%d + %d = %d", a, b, a+b), nil
_, err := bot.Ctx.SendMessage(m.ChannelID, content, nil)
return err
} }
// Ping is a simple ping example, perhaps the most simple you could make it. // Ping is a simple ping example, perhaps the most simple you could make it.
func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error { func (bot *Bot) Ping(m *gateway.MessageCreateEvent) (string, error) {
_, err := bot.Ctx.SendMessage(m.ChannelID, "Pong!", nil) return "Pong!", nil
return err
} }
// Say demonstrates how arguments.Flag could be used without the flag library. // Say demonstrates how arguments.Flag could be used without the flag library.
func (bot *Bot) Say(m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) { func (bot *Bot) Say(m *gateway.MessageCreateEvent, f bot.RawArguments) (string, error) {
args := f.String() if f != "" {
if args == "" { return string(f), nil
// Empty message, ignore
return "", nil
} }
return "", errors.New("Missing content.")
return args, nil
} }
// GuildInfo demonstrates the use of command flags, in this case the GuildOnly // GuildInfo demonstrates the GuildOnly middleware done in (*Bot).Setup().
// flag.
func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) { func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) {
g, err := bot.Ctx.GuildWithCount(m.GuildID) g, err := bot.Ctx.GuildWithCount(m.GuildID)
if err != nil { 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 // Repeat tells the bot to wait for the user's response, then repeat what they
// said. // said.
func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) { func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
_, err := bot.Ctx.SendMessage(m.ChannelID, _, err := bot.Ctx.SendMessage(m.ChannelID, "What do you want me to say?", nil)
"What do you want me to say?", nil)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -80,6 +72,8 @@ func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() 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 { v := bot.Ctx.WaitFor(ctx, func(v interface{}) bool {
// Incoming event is a message create event: // Incoming event is a message create event:
mg, ok := v.(*gateway.MessageCreateEvent) 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 // 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. // 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() fs := arguments.NewFlagSet()
var ( var (

View file

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