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..a28db47 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,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 diff --git a/discord/discord.go b/discord/discord.go index 423087f..ac73f80 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" ) @@ -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 == "" { diff --git a/discord/help.go b/discord/help.go index 2587ef6..a8fcd53 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.echo || c.album { 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..8449f52 --- /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, + 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 +} 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) } diff --git a/sedbot.toml b/sedbot.toml new file mode 100755 index 0000000..e0d541f --- /dev/null +++ b/sedbot.toml @@ -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 +# , your looks like , you +[insult] +targets = [] +comparisons = [] +adjectives = [] +nouns = [] + +# compliments +# , I would my just to . +[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 = "" +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 = "👎" \ No newline at end of file