From b8a199faba2ae985eae28088f88944d1c4e2c065 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Tue, 6 Sep 2022 17:21:45 -0400 Subject: [PATCH] pagecomment: add /url command * Updated discord library * Added an author embed but it's not useful just yet --- bot/interfaces.go | 6 ++ connectors/discord/discord.go | 23 ++++-- go.mod | 6 +- go.sum | 6 ++ main.go | 2 + plugins/cowboy/cowboy.go | 2 +- plugins/emojy/discordslash.go | 2 +- plugins/newsbid/newsbid.go | 2 +- plugins/pagecomment/pagecomment.go | 123 +++++++++++++++++++++++++++++ 9 files changed, 159 insertions(+), 13 deletions(-) create mode 100644 plugins/pagecomment/pagecomment.go diff --git a/bot/interfaces.go b/bot/interfaces.go index 744827b..fb27f94 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -44,6 +44,12 @@ type EphemeralID string type UnfurlLinks bool +type EmbedAuthor struct { + ID string + Who string + IconURL string +} + type ImageAttachment struct { URL string AltTxt string diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index cddc4b6..d9e7d46 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -103,25 +103,32 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a message = "_" + message + "_" } - var embeds *discordgo.MessageEmbed + embeds := []*discordgo.MessageEmbed{} for _, arg := range args { switch a := arg.(type) { + case bot.EmbedAuthor: + embed := &discordgo.MessageEmbed{} + embed.Author = &discordgo.MessageEmbedAuthor{ + Name: a.Who, + IconURL: a.IconURL, + } + embeds = append(embeds, embed) case bot.ImageAttachment: - //embeds.URL = a.URL - embeds = &discordgo.MessageEmbed{} - embeds.Description = a.AltTxt - embeds.Image = &discordgo.MessageEmbedImage{ + embed := &discordgo.MessageEmbed{} + embed.Description = a.AltTxt + embed.Image = &discordgo.MessageEmbedImage{ URL: a.URL, Width: a.Width, Height: a.Height, } + embeds = append(embeds, embed) } } data := &discordgo.MessageSend{ Content: message, - Embed: embeds, + Embeds: embeds, } log.Debug(). @@ -322,7 +329,9 @@ func (d *Discord) Emojy(name string) string { func (d *Discord) UploadEmojy(emojy, path string) error { guildID := d.config.Get("discord.guildid", "") defaultRoles := d.config.GetArray("discord.emojyRoles", []string{}) - _, err := d.client.GuildEmojiCreate(guildID, emojy, path, defaultRoles) + _, err := d.client.GuildEmojiCreate(guildID, &discordgo.EmojiParams{ + emojy, path, defaultRoles, + }) if err != nil { return err } diff --git a/go.mod b/go.mod index dbe5bf4..ca864b0 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( code.chrissexton.org/cws/getaoc v0.0.0-20191201043947-d5417d4b618d github.com/ChimeraCoder/anaconda v2.0.0+incompatible github.com/PuerkitoBio/goquery v1.8.0 - github.com/bwmarrin/discordgo v0.25.0 + github.com/bwmarrin/discordgo v0.26.1 github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035 @@ -30,7 +30,7 @@ require ( github.com/stretchr/testify v1.8.0 github.com/trubitsyn/go-zero-width v1.0.1 github.com/velour/velour v0.0.0-20160303155839-8e090e68d158 - golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e + golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 gopkg.in/go-playground/webhooks.v5 v5.17.0 ) @@ -83,7 +83,7 @@ require ( golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/sys v0.0.0-20220906165534-d0df966e6959 // indirect golang.org/x/text v0.3.7 // indirect gonum.org/v1/gonum v0.6.0 // indirect google.golang.org/appengine v1.6.5 // indirect diff --git a/go.sum b/go.sum index 464ac90..4f9d73e 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330 h1:ekDALXAVvY/Ub1UtNta github.com/azr/backoff v0.0.0-20160115115103-53511d3c7330/go.mod h1:nH+k0SvAt3HeiYyOlJpLLv1HG1p7KWP7qU9QPp2/pCo= github.com/bwmarrin/discordgo v0.25.0 h1:NXhdfHRNxtwso6FPdzW2i3uBvvU7UIQTghmV2T4nqAs= github.com/bwmarrin/discordgo v0.25.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE= +github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk= github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= @@ -180,6 +182,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= +golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -215,6 +219,8 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220906165534-d0df966e6959 h1:qSa+Hg9oBe6UJXrznE+yYvW51V9UbyIj/nj/KpDigo8= +golang.org/x/sys v0.0.0-20220906165534-d0df966e6959/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/main.go b/main.go index ef7c5b0..240a9db 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ package main import ( "flag" + "github.com/velour/catbase/plugins/pagecomment" "github.com/velour/catbase/plugins/topic" "io" "math/rand" @@ -131,6 +132,7 @@ func main() { b.AddPlugin(admin.New(b)) b.AddPlugin(roles.New(b)) + b.AddPlugin(pagecomment.New(b)) b.AddPlugin(gpt3.New(b)) b.AddPlugin(secrets.New(b)) b.AddPlugin(mayi.New(b)) diff --git a/plugins/cowboy/cowboy.go b/plugins/cowboy/cowboy.go index a112ecb..8648055 100644 --- a/plugins/cowboy/cowboy.go +++ b/plugins/cowboy/cowboy.go @@ -191,7 +191,7 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: msg, - Flags: uint64(discordgo.MessageFlagsEphemeral), + Flags: discordgo.MessageFlagsEphemeral, }, }) } diff --git a/plugins/emojy/discordslash.go b/plugins/emojy/discordslash.go index 630b910..b800275 100644 --- a/plugins/emojy/discordslash.go +++ b/plugins/emojy/discordslash.go @@ -61,7 +61,7 @@ resp: Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ Content: msg, - Flags: uint64(discordgo.MessageFlagsEphemeral), + Flags: discordgo.MessageFlagsEphemeral, }, }) } diff --git a/plugins/newsbid/newsbid.go b/plugins/newsbid/newsbid.go index 354f1a2..280c3b9 100644 --- a/plugins/newsbid/newsbid.go +++ b/plugins/newsbid/newsbid.go @@ -44,7 +44,7 @@ func New(b bot.Bot) *NewsBid { var balanceRegex = regexp.MustCompile(`(?i)^balance$`) var bidsRegex = regexp.MustCompile(`(?i)^bids$`) var scoresRegex = regexp.MustCompile(`(?i)^scores$`) -var bidRegex = regexp.MustCompile(`(?i)^bid (?P\S+) (?P)\S+$`) +var bidRegex = regexp.MustCompile(`(?i)^bid (?P\S+) (?P\S+)\s?(?P.+)?$`) var checkRegex = regexp.MustCompile(`(?i)^check ngate$`) func (p *NewsBid) balanceCmd(r bot.Request) bool { diff --git a/plugins/pagecomment/pagecomment.go b/plugins/pagecomment/pagecomment.go new file mode 100644 index 0000000..4c90874 --- /dev/null +++ b/plugins/pagecomment/pagecomment.go @@ -0,0 +1,123 @@ +package pagecomment + +import ( + "fmt" + "github.com/PuerkitoBio/goquery" + "github.com/bwmarrin/discordgo" + "github.com/rs/zerolog/log" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/config" + "github.com/velour/catbase/connectors/discord" + "net/http" + "regexp" + "strings" +) + +type PageComment struct { + b bot.Bot + c *config.Config +} + +func New(b bot.Bot) *PageComment { + p := &PageComment{ + b: b, + c: b.Config(), + } + p.register() + return p +} + +func (p *PageComment) register() { + p.b.RegisterTable(p, bot.HandlerTable{ + { + Kind: bot.Startup, IsCmd: false, + Regex: regexp.MustCompile(`.*`), + Handler: func(r bot.Request) bool { + switch conn := r.Conn.(type) { + case *discord.Discord: + p.registerCmds(conn) + } + return false + }, + }, + {Kind: bot.Message, IsCmd: true, + Regex: regexp.MustCompile(`(?i)^url (?P\S+) (?P.+)`), + HelpText: "Comment on a URL", Handler: p.handleURLReq}, + }) +} + +func (p *PageComment) handleURLReq(r bot.Request) bool { + fullText := r.Msg.Body + fullComment := fullText[strings.Index(fullText, r.Values["comment"]):] + u := r.Values["url"] + if strings.HasPrefix(u, "<") && strings.HasSuffix(u, ">") { + u = u[1 : len(u)-1] + } + msg := p.handleURL(u, fullComment, r.Msg.User.Name) + p.b.Send(r.Conn, bot.Delete, r.Msg.Channel, r.Msg.ID) + p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) + return true +} + +func (p *PageComment) handleURLCmd(conn bot.Connector) func(*discordgo.Session, *discordgo.InteractionCreate) { + return func(s *discordgo.Session, i *discordgo.InteractionCreate) { + u := i.ApplicationCommandData().Options[0].StringValue() + cmt := i.ApplicationCommandData().Options[1].StringValue() + who := i.Member.User.Username + profile, err := conn.Profile(i.Member.User.ID) + if err == nil { + who = profile.Name + } + msg := p.handleURL(u, cmt, who) + err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: msg, + }, + }) + if err != nil { + log.Error().Err(err).Msg("") + return + } + } +} + +func (p *PageComment) handleURL(u, cmt, who string) string { + req, err := http.Get(u) + if err != nil { + return "Couldn't get that URL" + } + doc, err := goquery.NewDocumentFromReader(req.Body) + if err != nil { + return "Couldn't parse that URL" + } + wait := make(chan string, 1) + doc.Find("title").First().Each(func(i int, s *goquery.Selection) { + wait <- fmt.Sprintf("> %s\n%s: %s\n(<%s>)", s.Text(), who, cmt, u) + }) + return <-wait +} + +func (p *PageComment) registerCmds(d *discord.Discord) { + cmd := discordgo.ApplicationCommand{ + Name: "url", + Description: "comment on a URL with its title", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "url", + Description: "What URL would you like", + Required: true, + }, + { + Type: discordgo.ApplicationCommandOptionString, + Name: "comment", + Description: "Your comment", + Required: true, + }, + }, + } + if err := d.RegisterSlashCmd(cmd, p.handleURLCmd(d)); err != nil { + log.Error().Err(err).Msg("could not register emojy command") + } +}