Compare commits
22 Commits
ff02993a0a
...
v1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 5465bd4c77 | |||
| bdfd612fa0 | |||
| de6e50179a | |||
|
38691be66b
|
|||
|
46bbecb280
|
|||
| 772993a596 | |||
|
f809b08c37
|
|||
| 4f3b30885e | |||
| 6c0316ac28 | |||
|
52cc00190b
|
|||
|
a7765f90d9
|
|||
| a0e511b4aa | |||
| 7b4d25d484 | |||
|
b392048ca1
|
|||
| b0cb606afe | |||
|
f303eb3eb8
|
|||
| e641e1d2a5 | |||
|
a6dc4e27b9
|
|||
| 509dab5395 | |||
|
6b9934a221
|
|||
| adefe2f177 | |||
|
084b7ed979
|
+3
-3
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
########################################
|
########################################
|
||||||
# Versions
|
# Versions
|
||||||
ARG YT_DLP_VERSION="2026.03.03"
|
ARG YT_DLP_VERSION="2026.03.17"
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# Deno builder stage
|
# Deno builder stage
|
||||||
@@ -38,7 +38,7 @@ RUN case "$TARGETARCH" in \
|
|||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# FFmpeg builder stage
|
# FFmpeg builder stage
|
||||||
FROM debian:13-slim@sha256:1d3c811171a08a5adaa4a163fbafd96b61b87aa871bbc7aa15431ac275d3d430 AS ffmpeg-builder
|
FROM debian:13-slim@sha256:26f98ccd92fd0a44d6928ce8ff8f4921b4d2f535bfa07555ee5d18f61429cf0c AS ffmpeg-builder
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
apt-get install -y --no-install-recommends ffmpeg
|
apt-get install -y --no-install-recommends ffmpeg
|
||||||
COPY --from=yt-dlp-builder /rootfs/bin/yt-dlp /yt-dlp
|
COPY --from=yt-dlp-builder /rootfs/bin/yt-dlp /yt-dlp
|
||||||
@@ -54,7 +54,7 @@ RUN mkdir -p /rootfs/bin && \
|
|||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# App builder stage
|
# App builder stage
|
||||||
FROM golang:1.26.1-trixie@sha256:ab8c4944b04c6f97c2b5bffce471b7f3d55f2228badc55eae6cce87596d5710b AS app-builder
|
FROM golang:1.26.1-trixie@sha256:96b28783b99bcd265fbfe0b36a3ac6462416ce6bf1feac85d4c4ff533cbaa473 AS app-builder
|
||||||
|
|
||||||
COPY app/ /opt/app
|
COPY app/ /opt/app
|
||||||
WORKDIR /opt/app
|
WORKDIR /opt/app
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
+1
-1
@@ -4,7 +4,7 @@ go 1.25.2
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bwmarrin/discordgo v0.29.0
|
github.com/bwmarrin/discordgo v0.29.0
|
||||||
github.com/lrstanley/go-ytdlp v1.3.3
|
github.com/lrstanley/go-ytdlp v1.3.5
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
|
|||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/lrstanley/go-ytdlp v1.3.3 h1:Y9kJcdTwskPWDiwONMIl501Dhi+OrTF7HHY6J6+Lbco=
|
github.com/lrstanley/go-ytdlp v1.3.3 h1:Y9kJcdTwskPWDiwONMIl501Dhi+OrTF7HHY6J6+Lbco=
|
||||||
github.com/lrstanley/go-ytdlp v1.3.3/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
github.com/lrstanley/go-ytdlp v1.3.3/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
||||||
|
github.com/lrstanley/go-ytdlp v1.3.4 h1:x3ppgdeN3FbguT5ifc6ISrgjYN10+dVUAbprA7/dYrk=
|
||||||
|
github.com/lrstanley/go-ytdlp v1.3.4/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
||||||
|
github.com/lrstanley/go-ytdlp v1.3.5 h1:eT+29mK3Lp+XPMQOH25+jVerrrjifYW1o3IkTYJ9SMs=
|
||||||
|
github.com/lrstanley/go-ytdlp v1.3.5/go.mod h1:VgjnTrvkTf+23JuySjyPq1iQ8ijSovBtTPpXH5XrLtI=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
|||||||
+155
-325
@@ -6,8 +6,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
@@ -142,54 +142,10 @@ func main() {
|
|||||||
setInteractionState(i.Interaction.Token, state)
|
setInteractionState(i.Interaction.Token, state)
|
||||||
|
|
||||||
// Build audio format options
|
// Build audio format options
|
||||||
audioMenuOptions := []discordgo.SelectMenuOption{}
|
audioMenuOptions := buildAudioMenuOptions(state.FormatOptions.AudioOptions)
|
||||||
for _, aOpt := range state.FormatOptions.AudioOptions {
|
|
||||||
label := aOpt.Format
|
|
||||||
if aOpt.Language != nil {
|
|
||||||
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
|
||||||
}
|
|
||||||
if aOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discord has a 100 char limit on labels
|
|
||||||
if len(label) > 100 {
|
|
||||||
label = label[:97] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
audioMenuOptions = append(audioMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: aOpt.FormatID,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Discord has a limit of 25 options per select menu
|
|
||||||
if len(audioMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build video format options (to keep them visible but disabled)
|
// Build video format options (to keep them visible but disabled)
|
||||||
videoMenuOptions := []discordgo.SelectMenuOption{}
|
videoMenuOptions := buildVideoMenuOptions(state.FormatOptions.VideoOptions)
|
||||||
for _, vOpt := range state.FormatOptions.VideoOptions {
|
|
||||||
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
|
||||||
if vOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
|
||||||
}
|
|
||||||
label += ")"
|
|
||||||
|
|
||||||
if len(label) > 100 {
|
|
||||||
label = label[:97] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
videoMenuOptions = append(videoMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: vOpt.FormatID,
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(videoMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update components - disable video select, enable audio select
|
// Update components - disable video select, enable audio select
|
||||||
updatedComponents := []discordgo.MessageComponent{
|
updatedComponents := []discordgo.MessageComponent{
|
||||||
@@ -292,14 +248,7 @@ func main() {
|
|||||||
// Respond immediately to prevent timeout
|
// Respond immediately to prevent timeout
|
||||||
response = fmt.Sprintf("%s **Starting download**", loading_emoji)
|
response = fmt.Sprintf("%s **Starting download**", loading_emoji)
|
||||||
|
|
||||||
// Start async download after responding
|
// Clean up state before responding
|
||||||
go func() {
|
|
||||||
// Small delay to ensure response is sent first
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
startAsyncDownload(s, i, state.Requester, state.URL, state.VideoFormatID, state.VideoFormatName, state.AudioFormatID, state.AudioFormatName, out_dir, temp_dir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Clean up state after starting download
|
|
||||||
deleteInteractionState(i.Interaction.Token)
|
deleteInteractionState(i.Interaction.Token)
|
||||||
} else {
|
} else {
|
||||||
response = "I don't see a video here :("
|
response = "I don't see a video here :("
|
||||||
@@ -314,6 +263,9 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: %v", err)
|
log.Printf("Error: %v", err)
|
||||||
}
|
}
|
||||||
|
if state.URL != "" {
|
||||||
|
go startAsyncDownload(s, i, state.Requester, state.URL, state.VideoFormatID, state.VideoFormatName, state.AudioFormatID, state.AudioFormatName, out_dir, temp_dir)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,150 +305,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch formats asynchronously
|
// Fetch formats asynchronously
|
||||||
go func() {
|
go fetchAndShowFormats(s, i, url)
|
||||||
formatOptions, err := GetFormats(url)
|
|
||||||
if err != nil {
|
|
||||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
|
||||||
Content: ptr("❌ Error fetching formats: " + err.Error()),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error updating interaction: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort video formats: highest resolution first, then by bitrate
|
|
||||||
sort.Slice(formatOptions.VideoOptions, func(i, j int) bool {
|
|
||||||
// Compare by height (descending)
|
|
||||||
heightI := 0
|
|
||||||
if formatOptions.VideoOptions[i].Height != nil {
|
|
||||||
heightI = *formatOptions.VideoOptions[i].Height
|
|
||||||
}
|
|
||||||
heightJ := 0
|
|
||||||
if formatOptions.VideoOptions[j].Height != nil {
|
|
||||||
heightJ = *formatOptions.VideoOptions[j].Height
|
|
||||||
}
|
|
||||||
|
|
||||||
if heightI != heightJ {
|
|
||||||
return heightI > heightJ
|
|
||||||
}
|
|
||||||
|
|
||||||
// If heights are equal, compare by TBR (descending)
|
|
||||||
tbrI := 0.0
|
|
||||||
if formatOptions.VideoOptions[i].TBR != nil {
|
|
||||||
tbrI = *formatOptions.VideoOptions[i].TBR
|
|
||||||
}
|
|
||||||
tbrJ := 0.0
|
|
||||||
if formatOptions.VideoOptions[j].TBR != nil {
|
|
||||||
tbrJ = *formatOptions.VideoOptions[j].TBR
|
|
||||||
}
|
|
||||||
|
|
||||||
return tbrI > tbrJ
|
|
||||||
})
|
|
||||||
|
|
||||||
// Sort audio formats: highest bitrate first
|
|
||||||
sort.Slice(formatOptions.AudioOptions, func(i, j int) bool {
|
|
||||||
tbrI := 0.0
|
|
||||||
if formatOptions.AudioOptions[i].TBR != nil {
|
|
||||||
tbrI = *formatOptions.AudioOptions[i].TBR
|
|
||||||
}
|
|
||||||
tbrJ := 0.0
|
|
||||||
if formatOptions.AudioOptions[j].TBR != nil {
|
|
||||||
tbrJ = *formatOptions.AudioOptions[j].TBR
|
|
||||||
}
|
|
||||||
|
|
||||||
return tbrI > tbrJ
|
|
||||||
})
|
|
||||||
|
|
||||||
// Build video format options for Discord select menu
|
|
||||||
videoMenuOptions := []discordgo.SelectMenuOption{}
|
|
||||||
for _, vOpt := range formatOptions.VideoOptions {
|
|
||||||
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
|
||||||
if vOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
|
||||||
}
|
|
||||||
label += ")"
|
|
||||||
|
|
||||||
// Discord has a 100 char limit on labels
|
|
||||||
if len(label) > 100 {
|
|
||||||
label = label[:97] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
videoMenuOptions = append(videoMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: vOpt.FormatID,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Discord has a limit of 25 options per select menu
|
|
||||||
if len(videoMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build audio format options for Discord select menu
|
|
||||||
audioMenuOptions := []discordgo.SelectMenuOption{}
|
|
||||||
for _, aOpt := range formatOptions.AudioOptions {
|
|
||||||
label := aOpt.Format
|
|
||||||
if aOpt.Language != nil {
|
|
||||||
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
|
||||||
}
|
|
||||||
if aOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discord has a 100 char limit on labels
|
|
||||||
if len(label) > 100 {
|
|
||||||
label = label[:97] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
audioMenuOptions = append(audioMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: aOpt.FormatID,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Discord has a limit of 25 options per select menu
|
|
||||||
if len(audioMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store format options in interaction state
|
|
||||||
setInteractionState(i.Interaction.Token, &InteractionState{
|
|
||||||
URL: url,
|
|
||||||
FormatOptions: formatOptions,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Update message with format selection menus
|
|
||||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
|
||||||
Content: ptr("Select a video format:"),
|
|
||||||
Components: &[]discordgo.MessageComponent{
|
|
||||||
discordgo.ActionsRow{
|
|
||||||
Components: []discordgo.MessageComponent{
|
|
||||||
discordgo.SelectMenu{
|
|
||||||
CustomID: "video_select",
|
|
||||||
Placeholder: "Choose a video format...",
|
|
||||||
MaxValues: 1,
|
|
||||||
Options: videoMenuOptions,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
discordgo.ActionsRow{
|
|
||||||
Components: []discordgo.MessageComponent{
|
|
||||||
discordgo.SelectMenu{
|
|
||||||
CustomID: "audio_select",
|
|
||||||
Placeholder: "Choose an audio format...",
|
|
||||||
MaxValues: 1,
|
|
||||||
Disabled: true,
|
|
||||||
Options: audioMenuOptions,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error updating interaction: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
},
|
},
|
||||||
"download video": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
"download video": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
data := i.ApplicationCommandData()
|
data := i.ApplicationCommandData()
|
||||||
@@ -511,10 +320,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
messageContent := targetMsg.Content
|
url := extractURLFromString(targetMsg.Content)
|
||||||
|
|
||||||
var url string
|
|
||||||
url = extractURLFromString(messageContent)
|
|
||||||
if url == "" {
|
if url == "" {
|
||||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
@@ -540,11 +346,127 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch formats asynchronously
|
// Fetch formats asynchronously
|
||||||
go func() {
|
go fetchAndShowFormats(s, i, url)
|
||||||
|
},
|
||||||
|
"version": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "[yt-dlp-bot](https://git.dubyatp.xyz/williamp/yt-dlp-bot) by dubyatp",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
switch i.Type {
|
||||||
|
case discordgo.InteractionApplicationCommand:
|
||||||
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
||||||
|
h(s, i)
|
||||||
|
}
|
||||||
|
case discordgo.InteractionMessageComponent:
|
||||||
|
customID := i.MessageComponentData().CustomID
|
||||||
|
if h, ok := componentHandlers[customID]; ok {
|
||||||
|
h(s, i)
|
||||||
|
} else if strings.HasPrefix(customID, "retry:") {
|
||||||
|
url := strings.TrimPrefix(customID, "retry:")
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseUpdateMessage,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: fmt.Sprintf("%s Fetching available formats...", loading_emoji),
|
||||||
|
Components: []discordgo.MessageComponent{},
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
go fetchAndShowFormats(s, i, url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Println("Initialize loading emoji")
|
||||||
|
initLoadingEmoji(s)
|
||||||
|
|
||||||
|
|
||||||
|
log.Println("Adding commands")
|
||||||
|
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
||||||
|
for i, v := range commands {
|
||||||
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
||||||
|
}
|
||||||
|
registeredCommands[i] = cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Bot is now running. Press CTRL+C to exit")
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
<-sc
|
||||||
|
|
||||||
|
s.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildVideoMenuOptions(videoOptions []VideoOption) []discordgo.SelectMenuOption {
|
||||||
|
opts := make([]discordgo.SelectMenuOption, 0, 25)
|
||||||
|
for _, vOpt := range videoOptions {
|
||||||
|
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
||||||
|
if vOpt.TBR != nil {
|
||||||
|
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
||||||
|
}
|
||||||
|
label += ")"
|
||||||
|
if len(label) > 100 {
|
||||||
|
label = label[:97] + "..."
|
||||||
|
}
|
||||||
|
opts = append(opts, discordgo.SelectMenuOption{
|
||||||
|
Label: label,
|
||||||
|
Value: vOpt.FormatID,
|
||||||
|
})
|
||||||
|
if len(opts) >= 25 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildAudioMenuOptions(audioOptions []AudioOption) []discordgo.SelectMenuOption {
|
||||||
|
opts := make([]discordgo.SelectMenuOption, 0, 25)
|
||||||
|
for _, aOpt := range audioOptions {
|
||||||
|
label := aOpt.Format
|
||||||
|
if aOpt.Language != nil {
|
||||||
|
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
||||||
|
}
|
||||||
|
if aOpt.TBR != nil {
|
||||||
|
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
||||||
|
}
|
||||||
|
if len(label) > 100 {
|
||||||
|
label = label[:97] + "..."
|
||||||
|
}
|
||||||
|
opts = append(opts, discordgo.SelectMenuOption{
|
||||||
|
Label: label,
|
||||||
|
Value: aOpt.FormatID,
|
||||||
|
})
|
||||||
|
if len(opts) >= 25 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchAndShowFormats(s *discordgo.Session, i *discordgo.InteractionCreate, url string) {
|
||||||
formatOptions, err := GetFormats(url)
|
formatOptions, err := GetFormats(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
Content: ptr("❌ Error fetching formats: " + err.Error()),
|
Content: ptr("❌ Error fetching formats: " + err.Error()),
|
||||||
|
Components: &[]discordgo.MessageComponent{
|
||||||
|
&discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Retry",
|
||||||
|
CustomID: fmt.Sprintf("retry:%s", url),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error updating interaction: %v", err)
|
log.Printf("Error updating interaction: %v", err)
|
||||||
@@ -553,107 +475,57 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort video formats: highest resolution first, then by bitrate
|
// Sort video formats: highest resolution first, then by bitrate
|
||||||
sort.Slice(formatOptions.VideoOptions, func(i, j int) bool {
|
sort.Slice(formatOptions.VideoOptions, func(x, y int) bool {
|
||||||
// Compare by height (descending)
|
heightX, heightY := 0, 0
|
||||||
heightI := 0
|
if formatOptions.VideoOptions[x].Height != nil {
|
||||||
if formatOptions.VideoOptions[i].Height != nil {
|
heightX = *formatOptions.VideoOptions[x].Height
|
||||||
heightI = *formatOptions.VideoOptions[i].Height
|
|
||||||
}
|
}
|
||||||
heightJ := 0
|
if formatOptions.VideoOptions[y].Height != nil {
|
||||||
if formatOptions.VideoOptions[j].Height != nil {
|
heightY = *formatOptions.VideoOptions[y].Height
|
||||||
heightJ = *formatOptions.VideoOptions[j].Height
|
|
||||||
}
|
}
|
||||||
|
if heightX != heightY {
|
||||||
if heightI != heightJ {
|
return heightX > heightY
|
||||||
return heightI > heightJ
|
|
||||||
}
|
}
|
||||||
|
tbrX, tbrY := 0.0, 0.0
|
||||||
// If heights are equal, compare by TBR (descending)
|
if formatOptions.VideoOptions[x].TBR != nil {
|
||||||
tbrI := 0.0
|
tbrX = *formatOptions.VideoOptions[x].TBR
|
||||||
if formatOptions.VideoOptions[i].TBR != nil {
|
|
||||||
tbrI = *formatOptions.VideoOptions[i].TBR
|
|
||||||
}
|
}
|
||||||
tbrJ := 0.0
|
if formatOptions.VideoOptions[y].TBR != nil {
|
||||||
if formatOptions.VideoOptions[j].TBR != nil {
|
tbrY = *formatOptions.VideoOptions[y].TBR
|
||||||
tbrJ = *formatOptions.VideoOptions[j].TBR
|
|
||||||
}
|
}
|
||||||
|
return tbrX > tbrY
|
||||||
return tbrI > tbrJ
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Sort audio formats: highest bitrate first
|
// Sort audio formats: highest bitrate first
|
||||||
sort.Slice(formatOptions.AudioOptions, func(i, j int) bool {
|
sort.Slice(formatOptions.AudioOptions, func(x, y int) bool {
|
||||||
tbrI := 0.0
|
tbrX, tbrY := 0.0, 0.0
|
||||||
if formatOptions.AudioOptions[i].TBR != nil {
|
if formatOptions.AudioOptions[x].TBR != nil {
|
||||||
tbrI = *formatOptions.AudioOptions[i].TBR
|
tbrX = *formatOptions.AudioOptions[x].TBR
|
||||||
}
|
}
|
||||||
tbrJ := 0.0
|
if formatOptions.AudioOptions[y].TBR != nil {
|
||||||
if formatOptions.AudioOptions[j].TBR != nil {
|
tbrY = *formatOptions.AudioOptions[y].TBR
|
||||||
tbrJ = *formatOptions.AudioOptions[j].TBR
|
|
||||||
}
|
}
|
||||||
|
return tbrX > tbrY
|
||||||
return tbrI > tbrJ
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Build video format options for Discord select menu
|
videoMenuOptions := buildVideoMenuOptions(formatOptions.VideoOptions)
|
||||||
videoMenuOptions := []discordgo.SelectMenuOption{}
|
audioMenuOptions := buildAudioMenuOptions(formatOptions.AudioOptions)
|
||||||
for _, vOpt := range formatOptions.VideoOptions {
|
|
||||||
label := fmt.Sprintf("%s (%s", vOpt.Resolution, vOpt.Ext)
|
|
||||||
if vOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(", %.0fkbps", *vOpt.TBR)
|
|
||||||
}
|
|
||||||
label += ")"
|
|
||||||
|
|
||||||
// Discord has a 100 char limit on labels
|
if len(videoMenuOptions) == 0 || len(audioMenuOptions) == 0 {
|
||||||
if len(label) > 100 {
|
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
label = label[:97] + "..."
|
Content: ptr("❌ No separate video/audio streams found for this URL. The source may only provide combined formats."),
|
||||||
}
|
|
||||||
|
|
||||||
videoMenuOptions = append(videoMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: vOpt.FormatID,
|
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
// Discord has a limit of 25 options per select menu
|
log.Printf("Error updating interaction: %v", err)
|
||||||
if len(videoMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build audio format options for Discord select menu
|
|
||||||
audioMenuOptions := []discordgo.SelectMenuOption{}
|
|
||||||
for _, aOpt := range formatOptions.AudioOptions {
|
|
||||||
label := aOpt.Format
|
|
||||||
if aOpt.Language != nil {
|
|
||||||
label += fmt.Sprintf(" [%s]", *aOpt.Language)
|
|
||||||
}
|
|
||||||
if aOpt.TBR != nil {
|
|
||||||
label += fmt.Sprintf(" (%.0fkbps)", *aOpt.TBR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discord has a 100 char limit on labels
|
|
||||||
if len(label) > 100 {
|
|
||||||
label = label[:97] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
audioMenuOptions = append(audioMenuOptions, discordgo.SelectMenuOption{
|
|
||||||
Label: label,
|
|
||||||
Value: aOpt.FormatID,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Discord has a limit of 25 options per select menu
|
|
||||||
if len(audioMenuOptions) >= 25 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store format options in interaction state
|
|
||||||
setInteractionState(i.Interaction.Token, &InteractionState{
|
setInteractionState(i.Interaction.Token, &InteractionState{
|
||||||
URL: url,
|
URL: url,
|
||||||
FormatOptions: formatOptions,
|
FormatOptions: formatOptions,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update message with format selection menus
|
|
||||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
Content: ptr("Select a video format:"),
|
Content: ptr("Select a video format:"),
|
||||||
Components: &[]discordgo.MessageComponent{
|
Components: &[]discordgo.MessageComponent{
|
||||||
@@ -683,46 +555,4 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error updating interaction: %v", err)
|
log.Printf("Error updating interaction: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
},
|
|
||||||
"version": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: "[yt-dlp-bot](https://git.dubyatp.xyz/williamp/yt-dlp-bot) by dubyatp",
|
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
switch i.Type {
|
|
||||||
case discordgo.InteractionApplicationCommand:
|
|
||||||
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
|
|
||||||
h(s, i)
|
|
||||||
}
|
|
||||||
case discordgo.InteractionMessageComponent:
|
|
||||||
if h, ok := componentHandlers[i.MessageComponentData().CustomID]; ok {
|
|
||||||
h(s, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
log.Println("Adding commands")
|
|
||||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
|
|
||||||
for i, v := range commands {
|
|
||||||
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
|
||||||
}
|
|
||||||
registeredCommands[i] = cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Bot is now running. Press CTRL+C to exit")
|
|
||||||
sc := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
||||||
<-sc
|
|
||||||
|
|
||||||
s.Close()
|
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-6
@@ -2,17 +2,50 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
_ "embed"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var loading_emoji = os.Getenv("LOADING_EMOJI")
|
var loading_emoji string
|
||||||
|
|
||||||
|
//go:embed assets/loading.webp
|
||||||
|
var rawLoadingEmoji []byte
|
||||||
|
|
||||||
|
var loadingEmojiBase64 = func() string {
|
||||||
|
s := "data:image/webp;base64," + base64.StdEncoding.EncodeToString(rawLoadingEmoji)
|
||||||
|
rawLoadingEmoji = nil
|
||||||
|
return s
|
||||||
|
}()
|
||||||
|
|
||||||
|
func initLoadingEmoji(s *discordgo.Session) {
|
||||||
|
emojis, err := s.ApplicationEmojis(s.State.User.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Panic("Cannot get emojis")
|
||||||
|
}
|
||||||
|
for _, e := range emojis {
|
||||||
|
if e.Name == "loading" {
|
||||||
|
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e, err := s.ApplicationEmojiCreate(s.State.User.ID, &discordgo.EmojiParams{
|
||||||
|
Name: "loading",
|
||||||
|
Image: loadingEmojiBase64,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("Cannot create loading emoji: %s", err)
|
||||||
|
}
|
||||||
|
loading_emoji = fmt.Sprintf("<a:%s:%s>", e.Name, e.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
var urlPattern = regexp.MustCompile(`https?://\S+`)
|
||||||
|
|
||||||
func extractURLFromString(in_url string) string {
|
func extractURLFromString(in_url string) string {
|
||||||
url_pattern := regexp.MustCompile(`https?://\S+`)
|
return string(urlPattern.Find([]byte(in_url)))
|
||||||
var match = url_pattern.Find([]byte(in_url))
|
|
||||||
|
|
||||||
return string(match)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to create string pointer
|
// Helper function to create string pointer
|
||||||
|
|||||||
+3
-1
@@ -19,7 +19,9 @@ func GetFormats(url string) (*FormatOptions, error) {
|
|||||||
SkipDownload().
|
SkipDownload().
|
||||||
DumpJSON()
|
DumpJSON()
|
||||||
|
|
||||||
result, err := dl.Run(context.TODO(), url)
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
result, err := dl.Run(ctx, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user