Jupiter

Signed-off-by: Etzelia <etzelia@hotmail.com>

Co-authored-by: Etzelia <etzelia@hotmail.com>
Reviewed-on: https://git.etztech.xyz/Etzelia/sedbot/pulls/23
Co-Authored-By: Etzelia <etzelia@noreply.example.org>
Co-Committed-By: Etzelia <etzelia@noreply.example.org>
staff
Etzelia 2021-01-17 03:45:06 +01:00
parent bed25234bb
commit d86a1f86b0
5 changed files with 91 additions and 2 deletions

View File

@ -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"`

View File

@ -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)

23
discord/jupiter.go 100644
View File

@ -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
}

55
imgur/imgur.go 100644
View File

@ -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()
}

View File

@ -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