Added a check for the unthinkable scenario of when a Twitter post isn't a video

This commit is contained in:
Logan G 2022-07-01 02:12:20 -06:00
parent c0e74eb633
commit 068ef6a00e
Signed by: logan
GPG key ID: E328528C921E7A7A

25
main.go
View file

@ -10,7 +10,6 @@ import (
"strings" "strings"
"regexp" "regexp"
"context" "context"
"reflect"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl" "github.com/wader/goutubedl"
@ -148,6 +147,8 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Println("URLs detected!") log.Println("URLs detected!")
respond := false
for _, url := range urls { for _, url := range urls {
if output, _ := regexp.MatchString("http.*twitter", url); output { if output, _ := regexp.MatchString("http.*twitter", url); output {
log.Println("Cringe twitter detected.") log.Println("Cringe twitter detected.")
@ -155,10 +156,13 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
goutubedl.Path = "yt-dlp" goutubedl.Path = "yt-dlp"
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{}) result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil { 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 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 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 var choice goutubedl.Format
@ -173,29 +177,23 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
downloadResult, err := result.Download(context.Background(), choice.FormatID) downloadResult, err := result.Download(context.Background(), choice.FormatID)
if err != nil { if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error()) log.Println(err)
//s.ChannelMessageSend(m.ChannelID, err.Error())
continue 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 { message.Files = append(message.Files, &discordgo.File {
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext), 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 ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit
Reader: downloadResult, Reader: downloadResult,
}) })
respond = true
defer downloadResult.Close() defer downloadResult.Close()
} }
} }
if respond {
if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil { if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil {
log.Println(message) log.Println(message)
log.Println(err) log.Println(err)
@ -204,3 +202,4 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
} }
} }
} }
}