diff --git a/messageCreate.go b/messageCreate.go index d5e37f6..dc66cc5 100644 --- a/messageCreate.go +++ b/messageCreate.go @@ -5,6 +5,7 @@ import ( "log" "regexp" "context" + "io" "bytes" "github.com/bwmarrin/discordgo" @@ -153,18 +154,31 @@ func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) continue } - buf := new(bytes.Buffer) - buf.ReadFrom(downloadResult) - downloadResult.Close() + // Test to see if the result is more than 8MB through the tried and true method of downloading it into a 8MB slice and seeing if it errors out + var buf = make([]byte, 8*1024*1024 + 1) + size, err := io.ReadFull(downloadResult, buf) + + // This section is probably really bad, don't copy it. Actually, don't even look at it. + if size <= 8*1024*1024 && err.Error() == "unexpected EOF" { + log.Printf("Video Size: %fM", float64(size)/1024/1024) + + var newBuf = make([]byte, size) + _, err := io.ReadFull(bytes.NewReader(buf), newBuf) + if err != nil { + log.Println(err) + continue + } + buf = nil - if buf.Len() <= 8*1024*1024 { response.Files = append(response.Files, &discordgo.File { Name: fmt.Sprintf("%s.%s", result.Info.ID, result.Formats()[0].Ext), ContentType: "text/plain", // This is of course not true, but Discord doesn't give a shit - Reader: buf, + Reader: bytes.NewReader(newBuf), }) } + + downloadResult.Close() } respond = true