From c5d122d2cf4d61db7c10b5ad9ca7841747b35f00 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 8 Jan 2021 20:48:53 -0600 Subject: [PATCH] Add XKCD and FK Signed-off-by: Etzelia --- discord/birb.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ discord/clear.go | 2 +- discord/xkcd.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 4 ++++ 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 discord/birb.go create mode 100644 discord/xkcd.go diff --git a/discord/birb.go b/discord/birb.go new file mode 100644 index 0000000..4f11bb3 --- /dev/null +++ b/discord/birb.go @@ -0,0 +1,46 @@ +package discord + +import ( + "context" + "fmt" + "strconv" + "strings" + + "go.etztech.xyz/falseknees" +) + +func init() { + commands["birb"] = command{ + validate: func(cmd commandInit) bool { + return true + }, + run: func(cmd commandInit) (string, error) { + if !memeRateLimit.Try() { + return "", nil + } + + client := falseknees.New() + + var comic *falseknees.Comic + var err error + args := strings.Fields(cmd.message.Content) + if len(args) < 2 { + comic, err = client.Random(context.Background()) + } else if strings.EqualFold(args[1], "new") { + 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", comic.Num, comic.Title, comic.Img), nil + }, + help: "Get a FalseKnees comic", + } +} diff --git a/discord/clear.go b/discord/clear.go index d549868..5a3f0e9 100644 --- a/discord/clear.go +++ b/discord/clear.go @@ -24,7 +24,7 @@ func init() { if userID != "" { limitArg = 2 if len(args) < 2 { - return "This command takes needs two arguments with a mention", nil + return "This command takes two arguments with a mention", nil } } else if len(args) < 1 { return "This command takes one argument without a mention", nil diff --git a/discord/xkcd.go b/discord/xkcd.go new file mode 100644 index 0000000..9491239 --- /dev/null +++ b/discord/xkcd.go @@ -0,0 +1,44 @@ +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", + } +} diff --git a/go.mod b/go.mod index 8ac31bc..162309c 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,12 @@ require ( github.com/gorilla/websocket v1.4.2 // indirect github.com/pelletier/go-toml v1.8.1 go.etcd.io/bbolt v1.3.4 + go.etztech.xyz/falseknees v0.0.1 go.etztech.xyz/go-mcm v1.3.1 go.etztech.xyz/inspiro v0.0.0-20200606185551-edfdf9da2359 go.jolheiser.com/beaver v1.0.2 go.jolheiser.com/gojang v0.0.2 + go.jolheiser.com/xkcd v0.0.1 golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 // indirect ) diff --git a/go.sum b/go.sum index 04db0dd..851a02a 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNC github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= +go.etztech.xyz/falseknees v0.0.1 h1:XjlBqMyBUH5b7e/9oedEwZ9pnz3sqMhXQv6vnQocxeM= +go.etztech.xyz/falseknees v0.0.1/go.mod h1:Acn1AwrvAArQEqhMBDlak5BvCZ3jgV8vdL8Pe5ZldRE= go.etztech.xyz/go-mcm v1.3.1 h1:RLdOQrMgw0eP7bsfRRbXLW8c5/RaJ5Mg7i/ESFaUwvY= go.etztech.xyz/go-mcm v1.3.1/go.mod h1:Hz2YULB3sN/aQA8cPSm2d6LM3E3qTMspzfRCIAD/1dc= go.etztech.xyz/inspiro v0.0.0-20200606185551-edfdf9da2359 h1:j/ZeoAj185wHfCSYD52Kt/69i3Bzk1MXN4Qh1yP6+P4= @@ -20,6 +22,8 @@ go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk= go.jolheiser.com/beaver v1.0.2/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= go.jolheiser.com/gojang v0.0.2 h1:CvQETKT9sFfvuDeYVUkiR0Jh7xIs7Cayi0rZuDrXoZg= go.jolheiser.com/gojang v0.0.2/go.mod h1:hUBULFDoampNM97E1IaYUhkLBJ30sb7iGsoFOdDU76I= +go.jolheiser.com/xkcd v0.0.1 h1:pRNY2BXxUS+NMtKlm/ENumOr7g3k4VWY/QoDri9YSNU= +go.jolheiser.com/xkcd v0.0.1/go.mod h1:AzWPrZToCLfpazsZBkeu/nPuIvurMqfCOptiydz7Dvk= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=