mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-27 17:23:00 +00:00
Examples: Updated advanced_bot to the new API
This commit is contained in:
parent
729979088c
commit
7d683a2ace
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"github.com/diamondburned/arikawa/bot"
|
"github.com/diamondburned/arikawa/bot"
|
||||||
"github.com/diamondburned/arikawa/bot/extras/arguments"
|
"github.com/diamondburned/arikawa/bot/extras/arguments"
|
||||||
|
"github.com/diamondburned/arikawa/bot/extras/middlewares"
|
||||||
"github.com/diamondburned/arikawa/discord"
|
"github.com/diamondburned/arikawa/discord"
|
||||||
"github.com/diamondburned/arikawa/gateway"
|
"github.com/diamondburned/arikawa/gateway"
|
||||||
)
|
)
|
||||||
|
@ -19,6 +20,10 @@ type Bot struct {
|
||||||
Ctx *bot.Context
|
Ctx *bot.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *Bot) Setup(sub *bot.Subcommand) {
|
||||||
|
sub.AddMiddleware("GuildInfo", middlewares.GuildOnly(bot.Ctx))
|
||||||
|
}
|
||||||
|
|
||||||
// Help prints the default help message.
|
// Help prints the default help message.
|
||||||
func (bot *Bot) Help(m *gateway.MessageCreateEvent) (string, error) {
|
func (bot *Bot) Help(m *gateway.MessageCreateEvent) (string, error) {
|
||||||
return bot.Ctx.Help(), nil
|
return bot.Ctx.Help(), nil
|
||||||
|
@ -39,9 +44,7 @@ func (bot *Bot) Ping(m *gateway.MessageCreateEvent) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(
|
func (bot *Bot) Say(m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) {
|
||||||
m *gateway.MessageCreateEvent, f *arguments.Flag) (string, error) {
|
|
||||||
|
|
||||||
args := f.String()
|
args := f.String()
|
||||||
if args == "" {
|
if args == "" {
|
||||||
// Empty message, ignore
|
// Empty message, ignore
|
||||||
|
@ -53,15 +56,15 @@ func (bot *Bot) Say(
|
||||||
|
|
||||||
// GuildInfo demonstrates the use of command flags, in this case the GuildOnly
|
// GuildInfo demonstrates the use of command flags, in this case the GuildOnly
|
||||||
// flag.
|
// flag.
|
||||||
func (bot *Bot) GーGuildInfo(m *gateway.MessageCreateEvent) (string, error) {
|
func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) {
|
||||||
g, err := bot.Ctx.Guild(m.GuildID)
|
g, err := bot.Ctx.GuildWithCount(m.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Failed to get guild: %v", err)
|
return "", fmt.Errorf("Failed to get guild: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Your guild is %s, and its maximum members is %d",
|
"Your guild is %s, and its maximum members is %d",
|
||||||
g.Name, g.MaxMembers,
|
g.Name, g.ApproximateMembers,
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,9 +101,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(
|
func (bot *Bot) Embed(m *gateway.MessageCreateEvent, f *arguments.Flag) (*discord.Embed, error) {
|
||||||
m *gateway.MessageCreateEvent, f *arguments.Flag) (*discord.Embed, error) {
|
|
||||||
|
|
||||||
fs := arguments.NewFlagSet()
|
fs := arguments.NewFlagSet()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/diamondburned/arikawa/bot"
|
"github.com/diamondburned/arikawa/bot"
|
||||||
|
"github.com/diamondburned/arikawa/bot/extras/middlewares"
|
||||||
"github.com/diamondburned/arikawa/gateway"
|
"github.com/diamondburned/arikawa/gateway"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,14 +26,12 @@ func (d *Debug) Setup(sub *bot.Subcommand) {
|
||||||
|
|
||||||
// Manually set the usage for each function.
|
// Manually set the usage for each function.
|
||||||
|
|
||||||
sub.ChangeCommandInfo("GOOS", "",
|
sub.ChangeCommandInfo("GOOS", "GOOS", "Prints the current operating system")
|
||||||
"Prints the current operating system")
|
sub.ChangeCommandInfo("GC", "GC", "Triggers the garbage collecto")
|
||||||
|
sub.ChangeCommandInfo("Goroutines", "", "Prints the current number of Goroutines")
|
||||||
|
|
||||||
sub.ChangeCommandInfo("GC", "",
|
sub.Hide("Die")
|
||||||
"Triggers the garbage collecto")
|
sub.AddMiddleware("Die", middlewares.AdminOnly(d.Context))
|
||||||
|
|
||||||
sub.ChangeCommandInfo("Goroutines", "",
|
|
||||||
"Prints the current number of Goroutines")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~go goroutines
|
// ~go goroutines
|
||||||
|
@ -44,19 +43,19 @@ func (d *Debug) Goroutines(m *gateway.MessageCreateEvent) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~go GOOS
|
// ~go GOOS
|
||||||
func (d *Debug) RーGOOS(m *gateway.MessageCreateEvent) (string, error) {
|
func (d *Debug) GOOS(m *gateway.MessageCreateEvent) (string, error) {
|
||||||
return strings.Title(runtime.GOOS), nil
|
return strings.Title(runtime.GOOS), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~go GC
|
// ~go GC
|
||||||
func (d *Debug) RーGC(m *gateway.MessageCreateEvent) (string, error) {
|
func (d *Debug) GC(m *gateway.MessageCreateEvent) (string, error) {
|
||||||
runtime.GC()
|
runtime.GC()
|
||||||
return "Done.", nil
|
return "Done.", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~go die
|
// ~go die
|
||||||
// This command will be hidden from ~help by default.
|
// This command will be hidden from ~help by default.
|
||||||
func (d *Debug) AーDie(m *gateway.MessageCreateEvent) error {
|
func (d *Debug) Die(m *gateway.MessageCreateEvent) error {
|
||||||
log.Fatalln("User", m.Author.Username, "killed the bot x_x")
|
log.Fatalln("User", m.Author.Username, "killed the bot x_x")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue