Make imgur album commands configurable #7
|
@ -5,7 +5,7 @@ token = ""
|
||||||
prefix = "!"
|
prefix = "!"
|
||||||
|
|
||||||
# db_path is the path to the database (default is next to binary)
|
# db_path is the path to the database (default is next to binary)
|
||||||
db_path = "sedbot.db"
|
db_path = "canopeas.db"
|
||||||
|
|
||||||
# mc_path is the path to the root directory of the minecraft server
|
# mc_path is the path to the root directory of the minecraft server
|
||||||
mc_path = "/home/minecraft/server/"
|
mc_path = "/home/minecraft/server/"
|
||||||
|
@ -78,6 +78,12 @@ verbs = []
|
||||||
nouns = []
|
nouns = []
|
||||||
minor_things = []
|
minor_things = []
|
||||||
|
|
||||||
|
[[albums]]
|
||||||
|
name = "jupiter"
|
||||||
|
aliases = ["jup", "jupjup"]
|
||||||
|
album_id = ""
|
||||||
|
help = "Images of Jupiter"
|
||||||
|
|
||||||
# echoes are any basic command -> message
|
# echoes are any basic command -> message
|
||||||
[[echoes]]
|
[[echoes]]
|
||||||
name = "discord"
|
name = "discord"
|
||||||
|
|
|
@ -38,6 +38,7 @@ type Config struct {
|
||||||
|
|
||||||
StaffRoles []string `toml:"staff_roles"`
|
StaffRoles []string `toml:"staff_roles"`
|
||||||
Echoes []Echo `toml:"echoes"`
|
Echoes []Echo `toml:"echoes"`
|
||||||
|
Albums []Album `toml:"albums"`
|
||||||
MessageRoles []MessageRole `toml:"message_roles"`
|
MessageRoles []MessageRole `toml:"message_roles"`
|
||||||
RegisterRole string `toml:"register_role"`
|
RegisterRole string `toml:"register_role"`
|
||||||
RegisteredChannel string `toml:"registered_channel"`
|
RegisteredChannel string `toml:"registered_channel"`
|
||||||
|
@ -74,11 +75,18 @@ type Echo struct {
|
||||||
Help string `toml:"help"`
|
Help string `toml:"help"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Album struct {
|
||||||
|
Name string `toml:"name"`
|
||||||
|
Aliases []string `toml:"aliases"`
|
||||||
|
AlbumID string `toml:"album_id"`
|
||||||
|
Help string `toml:"help"`
|
||||||
|
}
|
||||||
|
|
||||||
func Load(configPath string) (*Config, error) {
|
func Load(configPath string) (*Config, error) {
|
||||||
var err error
|
var err error
|
||||||
var configContent []byte
|
var configContent []byte
|
||||||
if len(configPath) == 0 {
|
if len(configPath) == 0 {
|
||||||
configPath = "sedbot.toml"
|
configPath = "canopeas.toml"
|
||||||
}
|
}
|
||||||
|
|
||||||
configContent, err = ioutil.ReadFile(configPath)
|
configContent, err = ioutil.ReadFile(configPath)
|
||||||
|
|
|
@ -10,6 +10,7 @@ const clearMax = 20
|
||||||
func init() {
|
func init() {
|
||||||
commands = append(commands, &command{
|
commands = append(commands, &command{
|
||||||
name: "clear",
|
name: "clear",
|
||||||
|
staffOnly: true,
|
||||||
validate: func(cmd commandInit) bool {
|
validate: func(cmd commandInit) bool {
|
||||||
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
|
return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles)
|
||||||
},
|
},
|
||||||
|
|
|
@ -7,12 +7,11 @@ import (
|
||||||
|
|
||||||
"git.birbmc.com/canopeas/config"
|
"git.birbmc.com/canopeas/config"
|
||||||
"git.birbmc.com/canopeas/database"
|
"git.birbmc.com/canopeas/database"
|
||||||
"git.birbmc.com/canopeas/imgur"
|
|
||||||
|
|
||||||
|
"git.birbmc.com/Etzelia/go-serverapi"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/dghubble/go-twitter/twitter"
|
"github.com/dghubble/go-twitter/twitter"
|
||||||
"github.com/dghubble/oauth1"
|
"github.com/dghubble/oauth1"
|
||||||
"git.birbmc.com/Etzelia/go-serverapi"
|
|
||||||
"go.jolheiser.com/beaver"
|
"go.jolheiser.com/beaver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,7 +37,7 @@ type commandInit struct {
|
||||||
type command struct {
|
type command struct {
|
||||||
staffOnly bool
|
staffOnly bool
|
||||||
deleteInvocation bool
|
deleteInvocation bool
|
||||||
echo bool
|
child bool
|
||||||
// TODO Does this really need to exist separately?
|
// TODO Does this really need to exist separately?
|
||||||
validate func(cmd commandInit) bool
|
validate func(cmd commandInit) bool
|
||||||
run func(cmd commandInit) (string, error)
|
run func(cmd commandInit) (string, error)
|
||||||
|
@ -53,11 +52,8 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init Jupiter images
|
// Init rand
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
if err := imgur.Init(cfg.ImgurClientID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init ServerAPI
|
// Init ServerAPI
|
||||||
sapi := serverapi.NewClient(cfg.ServerAPI.Endpoint, serverapi.WithToken(cfg.ServerAPI.Token))
|
sapi := serverapi.NewClient(cfg.ServerAPI.Endpoint, serverapi.WithToken(cfg.ServerAPI.Token))
|
||||||
|
@ -86,6 +82,9 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init commandMap
|
// Init commandMap
|
||||||
|
if err := Album(cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
Echo(cfg)
|
Echo(cfg)
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
if c.name == "" {
|
if c.name == "" {
|
||||||
|
|
|
@ -14,7 +14,7 @@ func Echo(cfg *config.Config) {
|
||||||
commands = append(commands, &command{
|
commands = append(commands, &command{
|
||||||
name: e.Name,
|
name: e.Name,
|
||||||
aliases: e.Aliases,
|
aliases: e.Aliases,
|
||||||
echo: true,
|
child: true,
|
||||||
validate: func(cmd commandInit) bool {
|
validate: func(cmd commandInit) bool {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
|
|
|
@ -65,7 +65,7 @@ func allHelp(cmd commandInit) *discordgo.MessageEmbed {
|
||||||
embed.Description = "Commands with an asterisk (*) are staff-only"
|
embed.Description = "Commands with an asterisk (*) are staff-only"
|
||||||
}
|
}
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
if c.echo {
|
if c.child {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,10 +5,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
"go.jolheiser.com/beaver"
|
|
||||||
"gitea.com/jolheiser/gojang"
|
"gitea.com/jolheiser/gojang"
|
||||||
"gitea.com/jolheiser/gojang/rate"
|
"gitea.com/jolheiser/gojang/rate"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"go.jolheiser.com/beaver"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package discord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.birbmc.com/canopeas/config"
|
||||||
|
"git.birbmc.com/canopeas/imgur"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Album(cfg *config.Config) error {
|
||||||
|
for _, a := range cfg.Albums {
|
||||||
|
images, err := imgur.Get(cfg.ImgurClientID, a.AlbumID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
commands = append(commands, &command{
|
||||||
|
name: a.Name,
|
||||||
|
aliases: a.Aliases,
|
||||||
|
child: true,
|
||||||
|
validate: func(cmd commandInit) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
run: func(cmd commandInit) (string, error) {
|
||||||
|
if !memeRateLimit.Try() {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
img := images[rand.Intn(len(images))-1]
|
||||||
|
return img.Link, nil
|
||||||
|
},
|
||||||
|
help: a.Help,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
commands = append(commands, &command{
|
||||||
|
deleteInvocation: true,
|
||||||
|
name: "albums",
|
||||||
|
validate: func(cmd commandInit) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
run: func(cmd commandInit) (string, error) {
|
||||||
|
embed := &discordgo.MessageEmbed{
|
||||||
|
Title: "Album Commands",
|
||||||
|
Fields: make([]*discordgo.MessageEmbedField, len(cfg.Albums)),
|
||||||
|
}
|
||||||
|
for i, a := range cfg.Albums {
|
||||||
|
name := a.Name
|
||||||
|
if len(a.Aliases) > 0 {
|
||||||
|
name += fmt.Sprintf(" (%s)", strings.Join(a.Aliases, ", "))
|
||||||
|
}
|
||||||
|
embed.Fields[i] = &discordgo.MessageEmbedField{
|
||||||
|
Name: name,
|
||||||
|
Value: a.Help,
|
||||||
|
Inline: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
channel, err := cmd.session.UserChannelCreate(cmd.message.Author.ID)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
sendEmbed(cmd.session, channel.ID, embed)
|
||||||
|
return "", nil
|
||||||
|
},
|
||||||
|
help: "Get all imgur albums",
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,21 +0,0 @@
|
||||||
package discord
|
|
||||||
|
|
||||||
import "git.birbmc.com/canopeas/imgur"
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
commands = append(commands, &command{
|
|
||||||
name: "jupiter",
|
|
||||||
aliases: []string{"jup", "jupjup"},
|
|
||||||
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",
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -6,10 +6,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO Make a client for this in a separate module
|
|
||||||
|
|
||||||
var Images []*Image
|
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Images []*Image `json:"data"`
|
Images []*Image `json:"data"`
|
||||||
Success bool `json:"success"`
|
Success bool `json:"success"`
|
||||||
|
@ -33,23 +29,20 @@ type Image struct {
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(clientID string) error {
|
func Get(clientID, albumID string) ([]*Image, error) {
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, "https://api.imgur.com/3/album/TaJVIdQ/images", nil)
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://api.imgur.com/3/album/%s/images", albumID), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID))
|
req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID))
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var imgurResp Response
|
var imgurResp Response
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&imgurResp); err != nil {
|
return imgurResp.Images, json.NewDecoder(resp.Body).Decode(&imgurResp)
|
||||||
return err
|
|
||||||
}
|
|
||||||
Images = imgurResp.Images
|
|
||||||
return resp.Body.Close()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue