Compare commits

..

No commits in common. "a9a4c9274c20751bfb546bc578fb66364d02b4e0" and "b8a199faba2ae985eae28088f88944d1c4e2c065" have entirely different histories.

1 changed files with 13 additions and 22 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 := handleURL(u, fullComment, r.Msg.User.Name) 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.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,8 +63,13 @@ 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()
msg := handleURL(u, cmt, "") who := i.Member.User.Username
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ 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, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: msg, Content: msg,
@ -77,32 +82,18 @@ func (p *PageComment) handleURLCmd(conn bot.Connector) func(*discordgo.Session,
} }
} }
func handleURL(u, cmt, who string) string { func (p *PageComment) handleURL(u, cmt, who string) string {
if who != "" { req, err := http.Get(u)
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(resp.Body) doc, err := goquery.NewDocumentFromReader(req.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)
sel := doc.Find("title") doc.Find("title").First().Each(func(i int, s *goquery.Selection) {
if sel.Length() == 0 { wait <- fmt.Sprintf("> %s\n%s: %s\n(<%s>)", s.Text(), who, cmt, u)
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
} }