From cc3ceb66685180e46e9eea476a767a1d5c8d8f00 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 14 May 2021 03:10:02 +0000 Subject: [PATCH] Make imgur album commands configurable (#7) Signed-off-by: Etzelia Rename echo/album field to child Signed-off-by: Etzelia Mark clear as staffOnly Signed-off-by: Etzelia Add imgur album commands Signed-off-by: Etzelia Reviewed-on: https://git.birbmc.com/Etzelia/canopeas/pulls/7 Co-Authored-By: Etzelia Co-Committed-By: Etzelia --- config/canopeas.example.toml | 8 +++- config/config.go | 18 ++++++--- discord/clear.go | 3 +- discord/discord.go | 13 +++---- discord/echo.go | 2 +- discord/help.go | 2 +- discord/history.go | 4 +- discord/imgur.go | 71 ++++++++++++++++++++++++++++++++++++ discord/jupiter.go | 21 ----------- imgur/imgur.go | 19 +++------- 10 files changed, 109 insertions(+), 52 deletions(-) create mode 100644 discord/imgur.go delete mode 100644 discord/jupiter.go diff --git a/config/canopeas.example.toml b/config/canopeas.example.toml index cda4eb2..f01f229 100644 --- a/config/canopeas.example.toml +++ b/config/canopeas.example.toml @@ -5,7 +5,7 @@ token = "" prefix = "!" # 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 = "/home/minecraft/server/" @@ -78,6 +78,12 @@ verbs = [] nouns = [] minor_things = [] +[[albums]] +name = "jupiter" +aliases = ["jup", "jupjup"] +album_id = "" +help = "Images of Jupiter" + # echoes are any basic command -> message [[echoes]] name = "discord" diff --git a/config/config.go b/config/config.go index 2e7e8d1..73101a0 100644 --- a/config/config.go +++ b/config/config.go @@ -38,6 +38,7 @@ type Config struct { StaffRoles []string `toml:"staff_roles"` Echoes []Echo `toml:"echoes"` + Albums []Album `toml:"albums"` MessageRoles []MessageRole `toml:"message_roles"` RegisterRole string `toml:"register_role"` RegisteredChannel string `toml:"registered_channel"` @@ -58,13 +59,13 @@ type Config struct { } type MessageRole struct { - ChannelID string `toml:"channel_id"` - MessageID string `toml:"message_id"` + ChannelID string `toml:"channel_id"` + MessageID string `toml:"message_id"` Reactions []MessageReaction `toml:"reactions"` } type MessageReaction struct { - Emoji string `toml:"emoji"` - RoleID string `toml:"role_id"` + Emoji string `toml:"emoji"` + RoleID string `toml:"role_id"` } type Echo struct { @@ -74,11 +75,18 @@ type Echo struct { 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) { var err error var configContent []byte if len(configPath) == 0 { - configPath = "sedbot.toml" + configPath = "canopeas.toml" } configContent, err = ioutil.ReadFile(configPath) diff --git a/discord/clear.go b/discord/clear.go index f75c676..3a86822 100644 --- a/discord/clear.go +++ b/discord/clear.go @@ -9,7 +9,8 @@ const clearMax = 20 func init() { commands = append(commands, &command{ - name: "clear", + name: "clear", + staffOnly: true, validate: func(cmd commandInit) bool { return isStaff(cmd.message.Member.Roles, cmd.config.StaffRoles) }, diff --git a/discord/discord.go b/discord/discord.go index 423087f..93c5f89 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -7,12 +7,11 @@ import ( "git.birbmc.com/canopeas/config" "git.birbmc.com/canopeas/database" - "git.birbmc.com/canopeas/imgur" + "git.birbmc.com/Etzelia/go-serverapi" "github.com/bwmarrin/discordgo" "github.com/dghubble/go-twitter/twitter" "github.com/dghubble/oauth1" - "git.birbmc.com/Etzelia/go-serverapi" "go.jolheiser.com/beaver" ) @@ -38,7 +37,7 @@ type commandInit struct { type command struct { staffOnly bool deleteInvocation bool - echo bool + child bool // TODO Does this really need to exist separately? validate func(cmd commandInit) bool run func(cmd commandInit) (string, error) @@ -53,11 +52,8 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error) return nil, err } - // Init Jupiter images + // Init rand rand.Seed(time.Now().UnixNano()) - if err := imgur.Init(cfg.ImgurClientID); err != nil { - return nil, err - } // Init ServerAPI 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 + if err := Album(cfg); err != nil { + return nil, err + } Echo(cfg) for _, c := range commands { if c.name == "" { diff --git a/discord/echo.go b/discord/echo.go index b69bd85..7c230d5 100644 --- a/discord/echo.go +++ b/discord/echo.go @@ -14,7 +14,7 @@ func Echo(cfg *config.Config) { commands = append(commands, &command{ name: e.Name, aliases: e.Aliases, - echo: true, + child: true, validate: func(cmd commandInit) bool { return true }, diff --git a/discord/help.go b/discord/help.go index 2587ef6..9b88c32 100644 --- a/discord/help.go +++ b/discord/help.go @@ -65,7 +65,7 @@ func allHelp(cmd commandInit) *discordgo.MessageEmbed { embed.Description = "Commands with an asterisk (*) are staff-only" } for _, c := range commands { - if c.echo { + if c.child { continue } diff --git a/discord/history.go b/discord/history.go index a79a032..fe697c1 100644 --- a/discord/history.go +++ b/discord/history.go @@ -5,10 +5,10 @@ import ( "strings" "time" - "github.com/bwmarrin/discordgo" - "go.jolheiser.com/beaver" "gitea.com/jolheiser/gojang" "gitea.com/jolheiser/gojang/rate" + "github.com/bwmarrin/discordgo" + "go.jolheiser.com/beaver" ) func init() { diff --git a/discord/imgur.go b/discord/imgur.go new file mode 100644 index 0000000..8c441f5 --- /dev/null +++ b/discord/imgur.go @@ -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 +} diff --git a/discord/jupiter.go b/discord/jupiter.go deleted file mode 100644 index a045721..0000000 --- a/discord/jupiter.go +++ /dev/null @@ -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", - }) -} diff --git a/imgur/imgur.go b/imgur/imgur.go index b7b6378..0b2e680 100644 --- a/imgur/imgur.go +++ b/imgur/imgur.go @@ -6,10 +6,6 @@ import ( "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"` @@ -33,23 +29,20 @@ type Image struct { 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 { - return err + return nil, err } req.Header.Set("Authorization", fmt.Sprintf("Client-ID %s", clientID)) resp, err := http.DefaultClient.Do(req) if err != nil { - return err + return nil, err } + defer resp.Body.Close() var imgurResp Response - if err := json.NewDecoder(resp.Body).Decode(&imgurResp); err != nil { - return err - } - Images = imgurResp.Images - return resp.Body.Close() + return imgurResp.Images, json.NewDecoder(resp.Body).Decode(&imgurResp) }