Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Sexton a9a4c9274c pagecomment: omit user if using slash 2022-09-07 10:59:14 -04:00
Chris Sexton b670ecc647 pagecomment: add useragent and check for no title 2022-09-07 10:41:35 -04:00
1 changed files with 22 additions and 13 deletions

View File

@ -53,7 +53,7 @@ func (p *PageComment) handleURLReq(r bot.Request) bool {
if strings.HasPrefix(u, "<") && strings.HasSuffix(u, ">") { if strings.HasPrefix(u, "<") && strings.HasSuffix(u, ">") {
u = u[1 : len(u)-1] u = u[1 : len(u)-1]
} }
msg := p.handleURL(u, fullComment, r.Msg.User.Name) msg := 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.Delete, r.Msg.Channel, r.Msg.ID)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg) p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true return true
@ -63,13 +63,8 @@ func (p *PageComment) handleURLCmd(conn bot.Connector) func(*discordgo.Session,
return func(s *discordgo.Session, i *discordgo.InteractionCreate) { return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
u := i.ApplicationCommandData().Options[0].StringValue() u := i.ApplicationCommandData().Options[0].StringValue()
cmt := i.ApplicationCommandData().Options[1].StringValue() cmt := i.ApplicationCommandData().Options[1].StringValue()
who := i.Member.User.Username msg := handleURL(u, cmt, "")
profile, err := conn.Profile(i.Member.User.ID) err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
if err == nil {
who = profile.Name
}
msg := p.handleURL(u, cmt, who)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: msg, Content: msg,
@ -82,18 +77,32 @@ func (p *PageComment) handleURLCmd(conn bot.Connector) func(*discordgo.Session,
} }
} }
func (p *PageComment) handleURL(u, cmt, who string) string { func handleURL(u, cmt, who string) string {
req, err := http.Get(u) if who != "" {
who = who + ": "
}
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil { if err != nil {
return "Couldn't parse that URL"
}
req.Header.Set("User-Agent", "catbase/1.0")
resp, err := client.Do(req)
if err != nil || resp.StatusCode > 299 {
log.Error().Err(err).Int("status", resp.StatusCode).Msgf("error with request")
return "Couldn't get that URL" return "Couldn't get that URL"
} }
doc, err := goquery.NewDocumentFromReader(req.Body) doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil { if err != nil {
return "Couldn't parse that URL" return "Couldn't parse that URL"
} }
wait := make(chan string, 1) wait := make(chan string, 1)
doc.Find("title").First().Each(func(i int, s *goquery.Selection) { sel := doc.Find("title")
wait <- fmt.Sprintf("> %s\n%s: %s\n(<%s>)", s.Text(), who, cmt, u) if sel.Length() == 0 {
return fmt.Sprintf("%s%s\n(<%s>)", who, cmt, u)
}
sel.First().Each(func(i int, s *goquery.Selection) {
wait <- fmt.Sprintf("> %s\n%s%s\n(<%s>)", s.Text(), who, cmt, u)
}) })
return <-wait return <-wait
} }