Add imgur album commands

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/7/head
Etzelia 2021-05-13 21:54:38 -05:00
parent 16e0383e39
commit 43998246d9
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
9 changed files with 208 additions and 48 deletions

View File

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

View File

@ -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,6 +75,13 @@ 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

View File

@ -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"
)
@ -39,6 +38,7 @@ type command struct {
staffOnly bool
deleteInvocation bool
echo bool
album bool
// TODO Does this really need to exist separately?
validate func(cmd commandInit) bool
run func(cmd commandInit) (string, error)
@ -53,11 +53,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 +83,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 == "" {

View File

@ -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.echo || c.album {
continue
}

View File

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

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,
album: 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"
)
// 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)
}

103
sedbot.toml 100755
View File

@ -0,0 +1,103 @@
# token is the Discord token for the bot
token = "ODMyNDUxMDUyNTY5MTAwMzA4.YHj-dQ.UZEO71yJpGVDUFguTx3vs6rpKu0"
# prefix is the bot command prefix
prefix = "!"
# db_path is the path to the database (default is next to binary)
db_path = "sedbot.db"
# mc_path is the path to the root directory of the minecraft server
mc_path = "/home/minecraft/server/"
# fired_role is to check how many time Carolyn has been fired
fired_role = "0"
# register_role is the role to assign to a user after registering
register_role = "0"
# registered_channel is the channel to message to welcome the newly registered user
registered_channel = "0"
# leave_channel is the channel to post leave messages to
leave_channel = "0"
# staff_roles are for staff commands
staff_roles = []
# meme_rate is the rate limit for memes
meme_rate = "0"
# Imgur Client ID
imgur_client_id = "66153d2fa93df9b"
# ServerAPI options
[serverapi]
# API endpoint
endpoint = ""
# Auth token
token = ""
# Twitter options
[twitter]
# Consumer Key
consumer_key = ""
# Consumer Secret
consumer_secret = ""
# Access Token
access_token = ""
# Access Secret
access_secret = ""
# Server options
[server]
# connection address
address = ""
# connection port
port = 25565
# MCM options
[mcm]
# the token for the MCM API
token = ""
# the base URL to the MCM API
url = ""
# insults
# <args>, your <target> looks like <comparison>, you <adjective> <noun>
[insult]
targets = []
comparisons = []
adjectives = []
nouns = []
# compliments
# <args>, I would <verb> my <noun> just to <minor thing>.
[compliment]
verbs = []
nouns = []
minor_things = []
[[albums]]
name = "jupiter"
aliases = ["jup", "jupjup"]
album_id = "TaJVIdQ"
help = "Images of Jupiter"
# echoes are any basic command -> message
[[echoes]]
name = "discord"
aliases = ["invite", "gib"]
message = "<https://birbmc.com/discord>"
help = "Get the invite link"
# message_roles are for messages that should toggle a role when a user selects it
[[message_roles]]
channel_id = "0"
message_id = "0"
[[message_roles.reactions]]
role_id = "0"
emoji = "👍"
[[message_roles.reactions]]
role_id = "0"
emoji = "👎"