diff --git a/config/config.go b/config/config.go index 5d58dd1..ce83a62 100644 --- a/config/config.go +++ b/config/config.go @@ -20,8 +20,9 @@ type Config struct { Address string `toml:"address"` Port int `toml:"port"` } `toml:"server"` - DBPath string `toml:"db_path"` - MCPath string `toml:"mc_path"` + DBPath string `toml:"db_path"` + MCPath string `toml:"mc_path"` + ImgurClientID string `toml:"imgur_client_id"` StaffRoles []string `toml:"staff_roles"` Echoes []Echo `toml:"echoes"` diff --git a/discord/discord.go b/discord/discord.go index e42bbb2..07c413e 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -7,6 +7,7 @@ import ( "go.etztech.xyz/sedbot/config" "go.etztech.xyz/sedbot/database" + "go.etztech.xyz/sedbot/imgur" "github.com/bwmarrin/discordgo" "go.jolheiser.com/beaver" @@ -39,6 +40,12 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error) return nil, err } + // Init Jupiter images + rand.Seed(time.Now().UnixNano()) + if err := imgur.Init(cfg.ImgurClientID); err != nil { + return nil, err + } + for _, messageRole := range cfg.MessageRoles { if messageRoleMap[messageRole.MessageID] == nil { messageRoleMap[messageRole.MessageID] = make(map[string]string) diff --git a/discord/jupiter.go b/discord/jupiter.go new file mode 100644 index 0000000..46e7ebb --- /dev/null +++ b/discord/jupiter.go @@ -0,0 +1,23 @@ +package discord + +import "go.etztech.xyz/sedbot/imgur" + +func init() { + cmd := command{ + validate: func(cmd commandInit) bool { + return true + }, + run: func(cmd commandInit) (string, error) { + if !memeRateLimit.Try() { + return "", nil + } + img := imgur.Images[rand.Intn(len(imgur.Images))-1] + return img.Link, nil + }, + help: "Get a Jupiter image", + } + + commands["jupiter"] = cmd + commands["jup"] = cmd + commands["jupjup"] = cmd +} diff --git a/imgur/imgur.go b/imgur/imgur.go new file mode 100644 index 0000000..b7b6378 --- /dev/null +++ b/imgur/imgur.go @@ -0,0 +1,55 @@ +package imgur + +import ( + "encoding/json" + "fmt" + "net/http" +) + +// TODO Make a client for this in a separate module + +var Images []*Image + +type Response struct { + Images []*Image `json:"data"` + Success bool `json:"success"` + Status int `json:"status"` +} + +type Image struct { + ID string `json:"id"` + Title string `json:"title"` + Description string `json:"description"` + Datetime int `json:"datetime"` + Type string `json:"type"` + Animated bool `json:"animated"` + Width int `json:"width"` + Height int `json:"height"` + Size int `json:"size"` + Views int `json:"views"` + Bandwidth int `json:"bandwidth"` + Deletehash string `json:"deletehash"` + Section string `json:"section"` + Link string `json:"link"` +} + +func Init(clientID string) error { + + req, err := http.NewRequest(http.MethodGet, "https://api.imgur.com/3/album/TaJVIdQ/images", nil) + if err != nil { + return err + } + req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID)) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + var imgurResp Response + if err := json.NewDecoder(resp.Body).Decode(&imgurResp); err != nil { + return err + } + Images = imgurResp.Images + return resp.Body.Close() +} diff --git a/sedbot.example.toml b/sedbot.example.toml index 242bd19..f6d4d2b 100644 --- a/sedbot.example.toml +++ b/sedbot.example.toml @@ -28,6 +28,9 @@ staff_roles = [] # meme_rate is the rate limit for memes meme_rate = "0" +# Imgur Client ID +imgur_client_id = "" + # Server options [server] # connection address