Compare commits

...

1 Commits
main ... jup

Author SHA1 Message Date
Etzelia 9fefc8b702
Jupiter
Signed-off-by: Etzelia <etzelia@hotmail.com>
2021-01-16 20:39:02 -06:00
5 changed files with 91 additions and 2 deletions

View File

@ -22,6 +22,7 @@ type Config struct {
} `toml:"server"` } `toml:"server"`
DBPath string `toml:"db_path"` DBPath string `toml:"db_path"`
MCPath string `toml:"mc_path"` MCPath string `toml:"mc_path"`
ImgurClientID string `toml:"imgur_client_id"`
StaffRoles []string `toml:"staff_roles"` StaffRoles []string `toml:"staff_roles"`
Echoes []Echo `toml:"echoes"` Echoes []Echo `toml:"echoes"`

View File

@ -7,6 +7,7 @@ import (
"go.etztech.xyz/sedbot/config" "go.etztech.xyz/sedbot/config"
"go.etztech.xyz/sedbot/database" "go.etztech.xyz/sedbot/database"
"go.etztech.xyz/sedbot/imgur"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"go.jolheiser.com/beaver" "go.jolheiser.com/beaver"
@ -39,6 +40,12 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
return nil, err 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 { for _, messageRole := range cfg.MessageRoles {
if messageRoleMap[messageRole.MessageID] == nil { if messageRoleMap[messageRole.MessageID] == nil {
messageRoleMap[messageRole.MessageID] = make(map[string]string) 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 is the rate limit for memes
meme_rate = "0" meme_rate = "0"
# Imgur Client ID
imgur_client_id = ""
# Server options # Server options
[server] [server]
# connection address # connection address