pagecomment: add /url command

* Updated discord library
* Added an author embed but it's not useful just yet
This commit is contained in:
Chris Sexton 2022-09-06 17:21:45 -04:00
parent 4617dd84fc
commit b8a199faba
9 changed files with 159 additions and 13 deletions

View File

@ -44,6 +44,12 @@ type EphemeralID string
type UnfurlLinks bool type UnfurlLinks bool
type EmbedAuthor struct {
ID string
Who string
IconURL string
}
type ImageAttachment struct { type ImageAttachment struct {
URL string URL string
AltTxt string AltTxt string

View File

@ -103,25 +103,32 @@ func (d *Discord) sendMessage(channel, message string, meMessage bool, args ...a
message = "_" + message + "_" message = "_" + message + "_"
} }
var embeds *discordgo.MessageEmbed embeds := []*discordgo.MessageEmbed{}
for _, arg := range args { for _, arg := range args {
switch a := arg.(type) { 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: case bot.ImageAttachment:
//embeds.URL = a.URL embed := &discordgo.MessageEmbed{}
embeds = &discordgo.MessageEmbed{} embed.Description = a.AltTxt
embeds.Description = a.AltTxt embed.Image = &discordgo.MessageEmbedImage{
embeds.Image = &discordgo.MessageEmbedImage{
URL: a.URL, URL: a.URL,
Width: a.Width, Width: a.Width,
Height: a.Height, Height: a.Height,
} }
embeds = append(embeds, embed)
} }
} }
data := &discordgo.MessageSend{ data := &discordgo.MessageSend{
Content: message, Content: message,
Embed: embeds, Embeds: embeds,
} }
log.Debug(). log.Debug().
@ -322,7 +329,9 @@ func (d *Discord) Emojy(name string) string {
func (d *Discord) UploadEmojy(emojy, path string) error { func (d *Discord) UploadEmojy(emojy, path string) error {
guildID := d.config.Get("discord.guildid", "") guildID := d.config.Get("discord.guildid", "")
defaultRoles := d.config.GetArray("discord.emojyRoles", []string{}) 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 { if err != nil {
return err return err
} }

6
go.mod
View File

@ -6,7 +6,7 @@ require (
code.chrissexton.org/cws/getaoc v0.0.0-20191201043947-d5417d4b618d code.chrissexton.org/cws/getaoc v0.0.0-20191201043947-d5417d4b618d
github.com/ChimeraCoder/anaconda v2.0.0+incompatible github.com/ChimeraCoder/anaconda v2.0.0+incompatible
github.com/PuerkitoBio/goquery v1.8.0 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/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598
github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff github.com/chrissexton/leftpad v0.0.0-20181207133115-1e93189d2fff
github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035 github.com/chrissexton/sentiment v0.0.0-20190927141846-d69c422ba035
@ -30,7 +30,7 @@ require (
github.com/stretchr/testify v1.8.0 github.com/stretchr/testify v1.8.0
github.com/trubitsyn/go-zero-width v1.0.1 github.com/trubitsyn/go-zero-width v1.0.1
github.com/velour/velour v0.0.0-20160303155839-8e090e68d158 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 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/image v0.0.0-20190802002840-cff245a6509b // indirect
golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 // indirect golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // 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 golang.org/x/text v0.3.7 // indirect
gonum.org/v1/gonum v0.6.0 // indirect gonum.org/v1/gonum v0.6.0 // indirect
google.golang.org/appengine v1.6.5 // indirect google.golang.org/appengine v1.6.5 // indirect

6
go.sum
View File

@ -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/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 h1:NXhdfHRNxtwso6FPdzW2i3uBvvU7UIQTghmV2T4nqAs=
github.com/bwmarrin/discordgo v0.25.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= 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 h1:j2XRGH5Y5uWtBYXGwmrjKeM/kfu/jh7ZcnrGvyN5Ttk=
github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598/go.mod h1:sduMkaHcXDIWurl/Bd/z0rNEUHw5tr6LUA9IO8E9o0o= 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= 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-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 h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 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-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-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/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-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 h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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/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.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

View File

@ -4,6 +4,7 @@ package main
import ( import (
"flag" "flag"
"github.com/velour/catbase/plugins/pagecomment"
"github.com/velour/catbase/plugins/topic" "github.com/velour/catbase/plugins/topic"
"io" "io"
"math/rand" "math/rand"
@ -131,6 +132,7 @@ func main() {
b.AddPlugin(admin.New(b)) b.AddPlugin(admin.New(b))
b.AddPlugin(roles.New(b)) b.AddPlugin(roles.New(b))
b.AddPlugin(pagecomment.New(b))
b.AddPlugin(gpt3.New(b)) b.AddPlugin(gpt3.New(b))
b.AddPlugin(secrets.New(b)) b.AddPlugin(secrets.New(b))
b.AddPlugin(mayi.New(b)) b.AddPlugin(mayi.New(b))

View File

@ -191,7 +191,7 @@ func (p *Cowboy) mkOverlayCB(overlay string) func(s *discordgo.Session, i *disco
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: msg, Content: msg,
Flags: uint64(discordgo.MessageFlagsEphemeral), Flags: discordgo.MessageFlagsEphemeral,
}, },
}) })
} }

View File

@ -61,7 +61,7 @@ resp:
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: msg, Content: msg,
Flags: uint64(discordgo.MessageFlagsEphemeral), Flags: discordgo.MessageFlagsEphemeral,
}, },
}) })
} }

View File

@ -44,7 +44,7 @@ func New(b bot.Bot) *NewsBid {
var balanceRegex = regexp.MustCompile(`(?i)^balance$`) var balanceRegex = regexp.MustCompile(`(?i)^balance$`)
var bidsRegex = regexp.MustCompile(`(?i)^bids$`) var bidsRegex = regexp.MustCompile(`(?i)^bids$`)
var scoresRegex = regexp.MustCompile(`(?i)^scores$`) var scoresRegex = regexp.MustCompile(`(?i)^scores$`)
var bidRegex = regexp.MustCompile(`(?i)^bid (?P<amount>\S+) (?P<url>)\S+$`) var bidRegex = regexp.MustCompile(`(?i)^bid (?P<amount>\S+) (?P<url>\S+)\s?(?P<comment>.+)?$`)
var checkRegex = regexp.MustCompile(`(?i)^check ngate$`) var checkRegex = regexp.MustCompile(`(?i)^check ngate$`)
func (p *NewsBid) balanceCmd(r bot.Request) bool { func (p *NewsBid) balanceCmd(r bot.Request) bool {

View File

@ -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<url>\S+) (?P<comment>.+)`),
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")
}
}