45 lines
836 B
Go
45 lines
836 B
Go
|
package discord
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
|
"go.jolheiser.com/xkcd"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
commands["xkcd"] = command{
|
||
|
validate: func(cmd commandInit) bool {
|
||
|
return true
|
||
|
},
|
||
|
run: func(cmd commandInit) (string, error) {
|
||
|
if !memeRateLimit.Try() {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
client := xkcd.New()
|
||
|
|
||
|
var comic *xkcd.Comic
|
||
|
var err error
|
||
|
args := strings.Fields(cmd.message.Content)
|
||
|
if len(args) < 2 {
|
||
|
comic, err = client.Current(context.Background())
|
||
|
} else {
|
||
|
comicNum, err := strconv.Atoi(args[1])
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
comic, err = client.Comic(context.Background(), comicNum)
|
||
|
}
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return fmt.Sprintf("%d: %s\n%s\n%s", comic.Num, comic.SafeTitle, comic.Alt, comic.Img), nil
|
||
|
},
|
||
|
help: "Get an xkcd comic",
|
||
|
}
|
||
|
}
|