pass download status to discord
This commit is contained in:
73
app/main.go
73
app/main.go
@@ -28,6 +28,7 @@ type DownloadResult struct {
|
||||
|
||||
// startAsyncDownload initiates a download in a goroutine and handles progress updates
|
||||
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, audioFormat, outputDir string) {
|
||||
progressChan := make(chan ProgressUpdate, 1)
|
||||
resultChan := make(chan DownloadResult, 1)
|
||||
|
||||
// Start download in goroutine
|
||||
@@ -47,7 +48,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
|
||||
}()
|
||||
|
||||
// Call downloadVideo (it panics on error instead of returning error)
|
||||
downloadVideo(outputDir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true})
|
||||
downloadVideo(outputDir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true}, progressChan)
|
||||
|
||||
// If we reach here, download was successful
|
||||
resultChan <- DownloadResult{
|
||||
@@ -59,7 +60,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle results asynchronously
|
||||
// Handle progress and results asynchronously
|
||||
go func() {
|
||||
// First update the original ephemeral message with "Processing..."
|
||||
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
@@ -69,31 +70,49 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
|
||||
result := <-resultChan
|
||||
|
||||
if result.Success {
|
||||
// Update ephemeral message with completion status
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("✅ Download completed successfully!\nURL: " + result.URL + "\nAudio: " + result.Format),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
|
||||
// Send non-ephemeral completion message
|
||||
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
|
||||
Content: "📥 Video downloaded: " + result.URL,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error sending public completion message: %v", err)
|
||||
}
|
||||
} else {
|
||||
// Update ephemeral message with error
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("❌ Download failed: " + result.Message + "\nURL: " + result.URL + "\nAudio: " + result.Format),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
for {
|
||||
select {
|
||||
case prog, ok := <-progressChan:
|
||||
if !ok {
|
||||
progressChan = nil
|
||||
continue
|
||||
}
|
||||
// Update message w/ phase and real time progress
|
||||
phaseEmoji := "⏬"
|
||||
if prog.Phase == "post-processing" {
|
||||
phaseEmoji = "⚙️"
|
||||
}
|
||||
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s",
|
||||
phaseEmoji,
|
||||
prog.Phase,
|
||||
prog.Status,
|
||||
prog.Percent,
|
||||
prog.ETA,
|
||||
prog.Filename)
|
||||
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr(content),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating progress: %v", err)
|
||||
}
|
||||
case result := <-resultChan:
|
||||
// Handle completion
|
||||
if result.Success {
|
||||
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
|
||||
Content: "📥 Video downloaded: " + result.URL,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
} else {
|
||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||
Content: ptr("❌ Download failed: " + result.Message),
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error updating interaction: %v", err)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user