Make imgur album commands configurable (#7)

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

Rename echo/album field to child

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

Mark clear as staffOnly

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

Add imgur album commands

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

Reviewed-on: https://git.birbmc.com/Etzelia/canopeas/pulls/7
Co-Authored-By: Etzelia <etzelia@hotmail.com>
Co-Committed-By: Etzelia <etzelia@hotmail.com>
discord-sync
Etzelia 2021-05-14 03:10:02 +00:00
parent 16e0383e39
commit cc3ceb6668
10 changed files with 109 additions and 52 deletions

View File

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

View File

@ -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"`
@ -58,13 +59,13 @@ type Config struct {
} }
type MessageRole struct { type MessageRole struct {
ChannelID string `toml:"channel_id"` ChannelID string `toml:"channel_id"`
MessageID string `toml:"message_id"` MessageID string `toml:"message_id"`
Reactions []MessageReaction `toml:"reactions"` Reactions []MessageReaction `toml:"reactions"`
} }
type MessageReaction struct { type MessageReaction struct {
Emoji string `toml:"emoji"` Emoji string `toml:"emoji"`
RoleID string `toml:"role_id"` RoleID string `toml:"role_id"`
} }
type Echo struct { type Echo struct {
@ -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)

View File

@ -9,7 +9,8 @@ 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)
}, },

View File

@ -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 == "" {

View File

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

View File

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

View File

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

71
discord/imgur.go 100644
View File

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

View File

@ -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",
})
}

View File

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