From 18a07ec828d51c02a42a2a725c3518b84342bd56 Mon Sep 17 00:00:00 2001 From: Logan Gartner Date: Fri, 22 Jul 2022 15:07:30 -0600 Subject: [PATCH] Added Reddit --- messageCreate.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/messageCreate.go b/messageCreate.go index 66fb8fb..6849059 100644 --- a/messageCreate.go +++ b/messageCreate.go @@ -218,6 +218,49 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) } } + respond = true + } else if output, _ := regexp.MatchString("(http.*reddit\\.com/r/\\.*)", url); output { + log.Println("Reddit detected.") + + response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*reddit\\.com").ReplaceAllString(url, "https://libreddit.spike.codes")) + + // Reddit's native format is 'mpegts', which Discord cannot preview. This code however does produce a usable video with audio. + /* + result, err := goutubedl.New(context.Background(), url, goutubedl.Options{}) + if err != nil { + // If it's complaining due to a lack of videos, don't care. + if noVideo, _ := regexp.MatchString(".*No media found.*", err.Error()); noVideo { + respond = true + } else { + log.Println(err) + } + + continue + } else { + videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024) + log.Printf("Choice: %s+%s | Size: %fM\n", videoChoice.FormatID, audioChoice.FormatID, (videoChoice.FilesizeApprox+audioChoice.FilesizeApprox)/1024/1024) + if err == nil { + session.ChannelTyping(message.ChannelID) + downloadResult, err := result.Download(context.Background(), fmt.Sprintf("%s+%s", videoChoice.FormatID, audioChoice.FormatID)) + if err != nil { + log.Println(err) + //session.ChannelMessageSend(message.ChannelID, err.Error()) + continue + } + + response.Files = append(response.Files, &discordgo.File { + Name: fmt.Sprintf("%s.%s", result.Info.ID, videoChoice.Ext), + ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit + Reader: downloadResult, + }) + + defer downloadResult.Close() + } else { + log.Println(err) + } + } + */ + respond = true } }