2020-01-19 06:06:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-23 03:52:07 +00:00
|
|
|
"context"
|
2020-01-19 06:06:00 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-01-23 03:52:07 +00:00
|
|
|
"time"
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/bot"
|
|
|
|
"github.com/diamondburned/arikawa/bot/extras/arguments"
|
2020-05-14 21:20:23 +00:00
|
|
|
"github.com/diamondburned/arikawa/bot/extras/middlewares"
|
2020-01-19 06:06:00 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bot struct {
|
|
|
|
// Context must not be embedded.
|
|
|
|
Ctx *bot.Context
|
|
|
|
}
|
|
|
|
|
2020-05-14 21:20:23 +00:00
|
|
|
func (bot *Bot) Setup(sub *bot.Subcommand) {
|
2020-05-15 01:01:48 +00:00
|
|
|
// Only allow people in guilds to run guildInfo.
|
2020-05-14 21:20:23 +00:00
|
|
|
sub.AddMiddleware("GuildInfo", middlewares.GuildOnly(bot.Ctx))
|
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// Help prints the default help message.
|
2020-07-29 23:29:01 +00:00
|
|
|
func (bot *Bot) Help(*gateway.MessageCreateEvent) (string, error) {
|
2020-01-26 09:06:54 +00:00
|
|
|
return bot.Ctx.Help(), nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// Add demonstrates the usage of typed arguments. Run it with "~add 1 2".
|
2020-07-29 23:29:01 +00:00
|
|
|
func (bot *Bot) Add(_ *gateway.MessageCreateEvent, a, b int) (string, error) {
|
2020-05-15 01:01:48 +00:00
|
|
|
return fmt.Sprintf("%d + %d = %d", a, b, a+b), nil
|
2020-01-21 04:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// Ping is a simple ping example, perhaps the most simple you could make it.
|
2020-07-29 23:29:01 +00:00
|
|
|
func (bot *Bot) Ping(*gateway.MessageCreateEvent) (string, error) {
|
2020-05-15 01:01:48 +00:00
|
|
|
return "Pong!", nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// Say demonstrates how arguments.Flag could be used without the flag library.
|
2020-07-29 23:29:01 +00:00
|
|
|
func (bot *Bot) Say(_ *gateway.MessageCreateEvent, f bot.RawArguments) (string, error) {
|
2020-05-15 01:01:48 +00:00
|
|
|
if f != "" {
|
|
|
|
return string(f), nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-05-16 21:14:49 +00:00
|
|
|
return "", errors.New("missing content")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:01:48 +00:00
|
|
|
// GuildInfo demonstrates the GuildOnly middleware done in (*Bot).Setup().
|
2020-05-14 21:20:23 +00:00
|
|
|
func (bot *Bot) GuildInfo(m *gateway.MessageCreateEvent) (string, error) {
|
|
|
|
g, err := bot.Ctx.GuildWithCount(m.GuildID)
|
2020-01-24 03:17:03 +00:00
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return "", fmt.Errorf("failed to get guild: %v", err)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 09:06:54 +00:00
|
|
|
return fmt.Sprintf(
|
2020-01-24 03:17:03 +00:00
|
|
|
"Your guild is %s, and its maximum members is %d",
|
2020-05-14 21:20:23 +00:00
|
|
|
g.Name, g.ApproximateMembers,
|
2020-01-26 09:06:54 +00:00
|
|
|
), nil
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 03:52:07 +00:00
|
|
|
// Repeat tells the bot to wait for the user's response, then repeat what they
|
|
|
|
// said.
|
2020-01-26 09:06:54 +00:00
|
|
|
func (bot *Bot) Repeat(m *gateway.MessageCreateEvent) (string, error) {
|
2020-05-15 01:01:48 +00:00
|
|
|
_, err := bot.Ctx.SendMessage(m.ChannelID, "What do you want me to say?", nil)
|
2020-01-23 03:52:07 +00:00
|
|
|
if err != nil {
|
2020-01-26 09:06:54 +00:00
|
|
|
return "", err
|
2020-01-23 03:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-05-15 01:01:48 +00:00
|
|
|
// This might miss events that are sent immediately after. To make sure all
|
|
|
|
// events are caught, ChanFor should be used.
|
2020-01-23 03:52:07 +00:00
|
|
|
v := bot.Ctx.WaitFor(ctx, func(v interface{}) bool {
|
|
|
|
// Incoming event is a message create event:
|
|
|
|
mg, ok := v.(*gateway.MessageCreateEvent)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message is from the same author:
|
|
|
|
return mg.Author.ID == m.Author.ID
|
|
|
|
})
|
|
|
|
|
|
|
|
if v == nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return "", errors.New("timed out waiting for response")
|
2020-01-23 03:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev := v.(*gateway.MessageCreateEvent)
|
2020-01-26 09:06:54 +00:00
|
|
|
return ev.Content, nil
|
2020-01-23 03:52:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
// 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.
|
2020-07-29 23:29:01 +00:00
|
|
|
func (bot *Bot) Embed(_ *gateway.MessageCreateEvent, f arguments.Flag) (*discord.Embed, error) {
|
2020-01-19 06:06:00 +00:00
|
|
|
fs := arguments.NewFlagSet()
|
|
|
|
|
|
|
|
var (
|
|
|
|
title = fs.String("title", "", "Title")
|
|
|
|
author = fs.String("author", "", "Author")
|
|
|
|
footer = fs.String("footer", "", "Footer")
|
|
|
|
color = fs.String("color", "#FFFFFF", "Color in hex format #hhhhhh")
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := f.With(fs.FlagSet); err != nil {
|
2020-01-26 09:06:54 +00:00
|
|
|
return nil, err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fs.Args()) < 1 {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, fmt.Errorf("usage: embed [flags] content...\n" + fs.Usage())
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the color string is valid.
|
|
|
|
if !strings.HasPrefix(*color, "#") || len(*color) != 7 {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.New("invalid color, format must be #hhhhhh")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the color into decimal numbers.
|
|
|
|
colorHex, err := strconv.ParseInt((*color)[1:], 16, 64)
|
|
|
|
if err != nil {
|
2020-01-26 09:06:54 +00:00
|
|
|
return nil, err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new embed
|
|
|
|
embed := discord.Embed{
|
|
|
|
Title: *title,
|
|
|
|
Description: strings.Join(fs.Args(), " "),
|
|
|
|
Color: discord.Color(colorHex),
|
|
|
|
}
|
|
|
|
|
|
|
|
if *author != "" {
|
|
|
|
embed.Author = &discord.EmbedAuthor{
|
|
|
|
Name: *author,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *footer != "" {
|
|
|
|
embed.Footer = &discord.EmbedFooter{
|
|
|
|
Text: *footer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 09:06:54 +00:00
|
|
|
return &embed, err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|