Skip to content

Commit

Permalink
Added slap command
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameInTheDark committed May 15, 2019
1 parent a266abd commit 6dd3ac1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
52 changes: 52 additions & 0 deletions api/fun/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fun

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

type ImageResponse struct {
Error string `json:"error"`
Success bool `json:"success"`
ImageURL string `json:"image"`
}

func GetImageURL(category string) (string, error) {
resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category))
if err != nil && resp.StatusCode == http.StatusOK {
fmt.Printf("Getting image url error: %v", err)
return "", errors.New("getting image url error")
}

var result ImageResponse

err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}

if result.Success {
return fmt.Sprintf("https://botimages.realpha.ru/%v", result.ImageURL), nil
}
return "", errors.New("wrong data")
}

func GetImage(category string) (*bytes.Buffer, error) {
resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category))
if err != nil {
fmt.Printf("Getting image url error: %v", err)
return nil, errors.New("getting image url error")
}

buf := new(bytes.Buffer)

_, err = io.Copy(buf, resp.Body) //png.Encode(buf, resp.Body)
if err != nil {
fmt.Printf("Map image: %v", err.Error())
}
return buf, err
}
9 changes: 9 additions & 0 deletions bot/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,12 @@ func (ctx *Context) ReplyEmbedAttachment(name, content, fileName string, file io
Color(ctx.GetGuild().EmbedColor).
Send(ctx)
}

// ReplyEmbedAttachment reply on message with embed message with attachment
func (ctx *Context) ReplyEmbedAttachmentImageURL(title, imageUrl string) *discordgo.Message {
return NewEmbed(title).
AttachImgURL(imageUrl).
Footer(ctx.Loc("requested_by") + ": " + ctx.User.Username).
Color(ctx.GetGuild().EmbedColor).
Send(ctx)
}
17 changes: 17 additions & 0 deletions cmd/funcommand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"fmt"
"github.com/FlameInTheDark/dtbot/api/fun"
"github.com/FlameInTheDark/dtbot/bot"
)

// SlapCommand returns slap image
func SlapCommand(ctx bot.Context) {
if len(ctx.Args) > 0 {
url, err := fun.GetImageURL("slap")
if err != nil {
ctx.ReplyEmbedAttachmentImageURL(fmt.Sprintf("%v slaping %v", ctx.User.Mention(), ctx.Args[0]), url)
}
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func registerCommands() {
CmdHandler.Register("!twitch", cmd.TwitchCommand)
CmdHandler.Register("!greetings", cmd.GreetingsCommand)
CmdHandler.Register("!alb", cmd.AlbionCommand)
CmdHandler.Register("!slap", cmd.SlapCommand)
}

// MetricsSender sends metrics to InfluxDB and another services
Expand Down

0 comments on commit 6dd3ac1

Please sign in to comment.