2022-07-17 23:45:11 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"context"
|
2022-07-18 03:05:40 -04:00
|
|
|
"io"
|
2022-07-18 01:25:02 -04:00
|
|
|
"bytes"
|
2022-07-17 23:45:11 -04:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/wader/goutubedl"
|
|
|
|
"mvdan.cc/xurls/v2" // Peak lazy
|
|
|
|
)
|
|
|
|
|
2024-10-09 21:38:44 -04:00
|
|
|
const MaxSize = 10*1024*1024
|
2023-05-05 18:50:05 -04:00
|
|
|
|
2022-07-18 01:25:02 -04:00
|
|
|
// This function will be called every time a new message is created on any channel that the authenticated bot has access to.
|
2022-07-17 23:45:11 -04:00
|
|
|
func messageCreate(session *discordgo.Session, message *discordgo.MessageCreate) {
|
|
|
|
// Ignore all messages created by the bot itself
|
|
|
|
if message.Author.ID == session.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//log.Printf("[%s] %s#%s: %s", message.ID, message.Author.Username, message.Author.Discriminator, message.Content)
|
|
|
|
|
|
|
|
rxStrict := xurls.Strict()
|
|
|
|
urls := rxStrict.FindAllString(message.Content, -1)
|
|
|
|
if len(urls) > 0 {
|
|
|
|
response := discordgo.MessageSend {
|
2022-07-22 03:07:47 -04:00
|
|
|
Content: "Woah there partner! I detect some services that aren't very privacy friendly. Let me help you with that!",
|
2022-07-17 23:45:11 -04:00
|
|
|
Reference: message.Reference(),
|
|
|
|
Files: []*discordgo.File{},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Message %s has URLs!", message.ID)
|
|
|
|
|
2023-03-15 23:34:03 -04:00
|
|
|
respond := false
|
|
|
|
|
2022-07-17 23:45:11 -04:00
|
|
|
for _, url := range urls {
|
2024-06-16 17:39:25 -04:00
|
|
|
if output, _ := regexp.MatchString("(http.*twitter.com/.*/status)|(http.*t.co/.*)|(http.*x.com/.*/status)", url); output {
|
2022-07-17 23:45:11 -04:00
|
|
|
log.Println("Cringe twitter post detected.")
|
|
|
|
|
|
|
|
if shortned, _ := regexp.MatchString("http.*t.co/.*", url); shortned {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
log.Println("Short URL detected. Getting long version.")
|
|
|
|
url, err = getRedirectURL(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 17:40:35 -04:00
|
|
|
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("(http.*twitter.com)|(http.*x.com)").ReplaceAllString(url, "https://nitter.poast.org"))
|
2022-07-17 23:45:11 -04:00
|
|
|
|
|
|
|
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
|
|
|
if err != nil {
|
2023-03-15 23:34:03 -04:00
|
|
|
// If it's complaining due to a lack of videos, don't care.
|
2023-03-15 23:38:12 -04:00
|
|
|
if noVideo, _ := regexp.MatchString(".*No video could be found in this tweet*", err.Error()); noVideo {
|
2023-03-15 23:34:03 -04:00
|
|
|
respond = true
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-07-17 23:45:11 -04:00
|
|
|
continue
|
|
|
|
} else {
|
2023-05-05 18:50:05 -04:00
|
|
|
choice, err := getLargestFormat(result, MaxSize)
|
2022-07-17 23:45:11 -04:00
|
|
|
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024)
|
|
|
|
if err == nil {
|
2022-07-20 19:45:35 -04:00
|
|
|
session.ChannelTyping(message.ChannelID)
|
2022-07-17 23:45:11 -04:00
|
|
|
downloadResult, err := result.Download(context.Background(), choice.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, choice.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)
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 23:34:03 -04:00
|
|
|
|
|
|
|
respond = true
|
2023-11-15 21:22:27 -05:00
|
|
|
} else if output, _ := regexp.MatchString("(http.*youtube.com/watch.*?v=.*)|(http.*youtube.com/shorts/.*)|(http.*youtube.com/v/.*)|(http.*youtu.be/.*)", url); output {
|
2022-07-17 23:45:11 -04:00
|
|
|
log.Println("YouTube detected.")
|
|
|
|
|
2023-11-15 21:22:27 -05:00
|
|
|
if shortned, _ := regexp.MatchString("(http.*youtu\\.be/.*)|(http.*youtube.com/v/.*)", url); shortned {
|
2022-07-17 23:45:11 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
log.Println("Short URL detected. Getting long version.")
|
|
|
|
url, err = getRedirectURL(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 03:52:31 -04:00
|
|
|
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*youtube\\.com").ReplaceAllString(url, "https://piped.video"))
|
2022-07-17 23:45:11 -04:00
|
|
|
|
|
|
|
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
2022-07-20 17:13:54 -04:00
|
|
|
} else if result.Info.AgeLimit < 18 && ! result.Info.IsLive {
|
2022-07-18 01:25:02 -04:00
|
|
|
/*
|
2023-05-05 18:50:05 -04:00
|
|
|
videoChoice, audioChoice, err := getLargestDashFormat(result, MaxSize)
|
2022-07-17 23:45:11 -04:00
|
|
|
log.Printf("Choice: %s+%s | Size: %fM\n", videoChoice.FormatID, audioChoice.FormatID, (videoChoice.Filesize+audioChoice.Filesize)/1024/1024)
|
|
|
|
if err == nil {
|
|
|
|
downloadResult, err := result.Download(context.Background(), fmt.Sprintf("%s+%s", videoChoice.FormatID, audioChoice.FormatID))
|
2022-07-18 01:25:02 -04:00
|
|
|
*/
|
2023-05-05 18:50:05 -04:00
|
|
|
choice, err := getLargestYTFormat(result, MaxSize)
|
2022-07-18 01:25:02 -04:00
|
|
|
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024)
|
|
|
|
if err == nil {
|
2022-07-20 19:45:35 -04:00
|
|
|
session.ChannelTyping(message.ChannelID)
|
2022-07-18 01:25:02 -04:00
|
|
|
downloadResult, err := result.Download(context.Background(), choice.FormatID)
|
|
|
|
|
2022-07-17 23:45:11 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
//session.ChannelMessageSend(message.ChannelID, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Files = append(response.Files, &discordgo.File {
|
2022-07-18 01:25:02 -04:00
|
|
|
//Name: fmt.Sprintf("%s.%s", result.Info.ID, videoChoice.Ext),
|
|
|
|
Name: fmt.Sprintf("%s.%s", result.Info.ID, choice.Ext),
|
2022-07-17 23:45:11 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 23:34:03 -04:00
|
|
|
|
|
|
|
respond = true
|
2022-07-18 01:25:14 -04:00
|
|
|
} else if output, _ := regexp.MatchString("(http.*instagram\\.com/p/.*)|(http.*instagram\\.com/tv/.*)|(http.*instagram\\.com/reel/.*)", url); output {
|
|
|
|
log.Println("Instagram detected.")
|
|
|
|
|
2022-07-20 17:06:07 -04:00
|
|
|
response.Content = response.Content + fmt.Sprintf("\n%s", regexp.MustCompile("http.*instagram\\.com").ReplaceAllString(url, "https://bibliogram.art"))
|
2022-07-18 01:25:14 -04:00
|
|
|
|
|
|
|
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
} else {
|
2022-07-20 19:45:35 -04:00
|
|
|
session.ChannelTyping(message.ChannelID)
|
2022-07-20 20:03:15 -04:00
|
|
|
downloadResult, err := result.Download(context.Background(), "best") // Insta only has one possible format to download :(
|
2022-07-18 01:25:14 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-18 03:05:40 -04:00
|
|
|
// 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
|
2023-05-05 18:50:05 -04:00
|
|
|
var buf = make([]byte, MaxSize + 1)
|
2022-07-18 03:05:40 -04:00
|
|
|
size, err := io.ReadFull(downloadResult, buf)
|
|
|
|
|
|
|
|
// This section is probably really bad, don't copy it. Actually, don't even look at it.
|
2023-05-05 18:50:05 -04:00
|
|
|
if size <= MaxSize && err.Error() == "unexpected EOF" {
|
2022-07-18 03:05:40 -04:00
|
|
|
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
|
2022-07-18 01:25:14 -04:00
|
|
|
|
|
|
|
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
|
2022-07-18 03:05:40 -04:00
|
|
|
Reader: bytes.NewReader(newBuf),
|
2022-07-18 01:25:14 -04:00
|
|
|
})
|
|
|
|
}
|
2022-07-18 03:05:40 -04:00
|
|
|
|
|
|
|
downloadResult.Close()
|
2022-07-18 01:25:14 -04:00
|
|
|
}
|
2023-03-15 23:34:03 -04:00
|
|
|
|
|
|
|
respond = true
|
2022-07-20 20:03:34 -04:00
|
|
|
} else if output, _ := regexp.MatchString("(http.*tiktok\\.com/\\@.*)", url); output {
|
|
|
|
log.Println("TikTok detected.")
|
|
|
|
|
|
|
|
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*tiktok\\.com").ReplaceAllString(url, "https://proxitok.pussthecat.org"))
|
|
|
|
|
|
|
|
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
} else {
|
2023-05-05 18:50:05 -04:00
|
|
|
choice, err := getLargestFormat(result, MaxSize)
|
2022-07-20 20:03:34 -04:00
|
|
|
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.Filesize/1024/1024)
|
|
|
|
if err == nil {
|
|
|
|
session.ChannelTyping(message.ChannelID)
|
|
|
|
downloadResult, err := result.Download(context.Background(), choice.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, choice.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)
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 23:34:03 -04:00
|
|
|
|
|
|
|
respond = true
|
2022-07-22 17:07:30 -04:00
|
|
|
} else if output, _ := regexp.MatchString("(http.*reddit\\.com/r/\\.*)", url); output {
|
|
|
|
log.Println("Reddit detected.")
|
|
|
|
|
2023-06-13 19:32:39 -04:00
|
|
|
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*reddit\\.com").ReplaceAllString(url, "https://libreddit.privacydev.net"))
|
2022-07-22 17:07:30 -04:00
|
|
|
|
|
|
|
// 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.
|
2023-03-15 23:34:03 -04:00
|
|
|
if noVideo, _ := regexp.MatchString(".*No media found.*", err.Error()); noVideo {
|
|
|
|
respond = true
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:07:30 -04:00
|
|
|
continue
|
|
|
|
} else {
|
2023-05-05 18:50:05 -04:00
|
|
|
videoChoice, audioChoice, err := getLargestDashFormat(result, MaxSize)
|
2022-07-22 17:07:30 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2023-03-15 23:34:03 -04:00
|
|
|
|
|
|
|
respond = true
|
2023-05-05 18:47:32 -04:00
|
|
|
} else if output, _ := regexp.MatchString("(http.*clips\\.twitch\\.tv/\\.*)", url); output {
|
|
|
|
//TODO: Finish
|
|
|
|
//Twitch doesn't report filesize (makes too much sense I guess).
|
|
|
|
log.Println("Twitch clip detected.")
|
2022-07-17 23:45:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:34:03 -04:00
|
|
|
if respond {
|
|
|
|
if result, err := session.ChannelMessageSendComplex(message.ChannelID, &response); err != nil {
|
|
|
|
log.Println(result)
|
|
|
|
log.Println(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Successfully responded to %s", message.ID)
|
|
|
|
}
|
2022-07-17 23:45:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|