101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
func main() {
|
|
|
|
out_dir := os.Getenv("OUT_PATH")
|
|
bot_token := os.Getenv("DISCORD_TOKEN")
|
|
|
|
if out_dir == "" {
|
|
panic("No output dir specified")
|
|
}
|
|
|
|
s, err := discordgo.New("Bot " + bot_token)
|
|
if err != nil {
|
|
log.Fatalf("Invalid bot parameters: %v", err)
|
|
}
|
|
|
|
err = s.Open()
|
|
if err != nil {
|
|
log.Fatalf("Error opening connection: %v", err)
|
|
}
|
|
|
|
var defaultMemberPermissions int64 = discordgo.PermissionAllText
|
|
var interactionPrivateChannel = discordgo.InteractionContextPrivateChannel
|
|
|
|
var commands = []*discordgo.ApplicationCommand{
|
|
{
|
|
Name: "download",
|
|
Description: "Download video and save it to 'youtube-vids",
|
|
DefaultMemberPermissions: &defaultMemberPermissions,
|
|
Contexts: &[]discordgo.InteractionContextType{interactionPrivateChannel},
|
|
Options: []*discordgo.ApplicationCommandOption{
|
|
{
|
|
Name: "url",
|
|
Description: "URL",
|
|
Type: discordgo.ApplicationCommandOptionString,
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
|
|
"download": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
options := i.ApplicationCommandData().Options
|
|
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
|
|
for _, opt := range options {
|
|
optionMap[opt.Name] = opt
|
|
}
|
|
response := ""
|
|
if option, ok := optionMap["url"]; ok {
|
|
response = "It works! Your URL is: " + option.StringValue()
|
|
} else {
|
|
response = "It works!"
|
|
}
|
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
Data: &discordgo.InteractionResponseData{
|
|
Content: response,
|
|
Flags: discordgo.MessageFlagsEphemeral,
|
|
},
|
|
})
|
|
},
|
|
}
|
|
|
|
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; 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()
|
|
|
|
//var url string = "https://www.youtube.com/watch?v=WpBWSFF03eI"
|
|
|
|
//downloadVideo(out_dir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true})
|
|
}
|