Added a check for the unthinkable scenario of when a Twitter post isn't a video
This commit is contained in:
parent
c0e74eb633
commit
068ef6a00e
1 changed files with 17 additions and 18 deletions
25
main.go
25
main.go
|
@ -10,7 +10,6 @@ import (
|
|||
"strings"
|
||||
"regexp"
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/wader/goutubedl"
|
||||
|
@ -148,6 +147,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
|
||||
log.Println("URLs detected!")
|
||||
|
||||
respond := false
|
||||
|
||||
for _, url := range urls {
|
||||
if output, _ := regexp.MatchString("http.*twitter", url); output {
|
||||
log.Println("Cringe twitter detected.")
|
||||
|
@ -155,10 +156,13 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
goutubedl.Path = "yt-dlp"
|
||||
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
||||
if err != nil {
|
||||
s.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
// Probably no videos in it, don't care
|
||||
log.Println(err)
|
||||
//s.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
// Get rid of the stupid short URL that Shitter appends to everything
|
||||
message.Content = fmt.Sprintf("%s \"%s\"\n", message.Content, strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly
|
||||
|
||||
var choice goutubedl.Format
|
||||
|
@ -173,29 +177,23 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
|
||||
downloadResult, err := result.Download(context.Background(), choice.FormatID)
|
||||
if err != nil {
|
||||
s.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
log.Println(err)
|
||||
//s.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(reflect.TypeOf(message.Files))
|
||||
|
||||
/*
|
||||
message.Files = []*discordgo.File{&discordgo.File {
|
||||
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext),
|
||||
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
|
||||
Reader: downloadResult,
|
||||
}}
|
||||
*/
|
||||
|
||||
message.Files = append(message.Files, &discordgo.File {
|
||||
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext),
|
||||
ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
|
||||
Reader: downloadResult,
|
||||
})
|
||||
|
||||
respond = true
|
||||
|
||||
defer downloadResult.Close()
|
||||
}
|
||||
}
|
||||
if respond {
|
||||
if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil {
|
||||
log.Println(message)
|
||||
log.Println(err)
|
||||
|
@ -203,4 +201,5 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
log.Printf("Successfully responded to %s", m.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue