url: add configurable useragent

This commit is contained in:
Chris Sexton 2022-09-29 11:58:06 -04:00
parent dfa6302757
commit 7ca4a30c14
1 changed files with 6 additions and 4 deletions

View File

@ -53,7 +53,8 @@ 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) ua := p.c.Get("url.useragent", "catbase/1.0")
msg := handleURL(u, fullComment, r.Msg.User.Name, ua)
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,7 +64,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()
msg := handleURL(u, cmt, "") ua := p.c.Get("url.useragent", "catbase/1.0")
msg := handleURL(u, cmt, "", ua)
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
@ -77,7 +79,7 @@ func (p *PageComment) handleURLCmd(conn bot.Connector) func(*discordgo.Session,
} }
} }
func handleURL(u, cmt, who string) string { func handleURL(u, cmt, who, ua string) string {
if who != "" { if who != "" {
who = who + ": " who = who + ": "
} }
@ -86,7 +88,7 @@ func handleURL(u, cmt, who string) string {
if err != nil { if err != nil {
return "Couldn't parse that URL" return "Couldn't parse that URL"
} }
req.Header.Set("User-Agent", "catbase/1.0") req.Header.Set("User-Agent", ua)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil || resp.StatusCode > 299 { if err != nil || resp.StatusCode > 299 {
log.Error().Err(err).Int("status", resp.StatusCode).Msgf("error with request") log.Error().Err(err).Int("status", resp.StatusCode).Msgf("error with request")