EngiBot/messageCreate.go

283 lines
13 KiB
Go

package main
import (
"fmt"
"log"
"regexp"
"context"
"io"
"bytes"
"github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl"
"mvdan.cc/xurls/v2" // Peak lazy
)
const MaxSize = 10*1024*1024
// This function will be called every time a new message is created on any channel that the authenticated bot has access to.
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 {
Content: "Woah there partner! I detect some services that aren't very privacy friendly. Let me help you with that!",
Reference: message.Reference(),
Files: []*discordgo.File{},
}
log.Printf("Message %s has URLs!", message.ID)
respond := false
for _, url := range urls {
if output, _ := regexp.MatchString("(http.*twitter.com/.*/status)|(http.*t.co/.*)|(http.*x.com/.*/status)", url); output {
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
}
}
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("(http.*twitter.com)|(http.*x.com)").ReplaceAllString(url, "https://nitter.poast.org"))
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.
if noVideo, _ := regexp.MatchString(".*No video could be found in this tweet*", err.Error()); noVideo {
respond = true
} else {
log.Println(err)
}
continue
} else {
choice, err := getLargestFormat(result, MaxSize)
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/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)
}
}
respond = true
} else if output, _ := regexp.MatchString("(http.*youtube.com/watch.*?v=.*)|(http.*youtube.com/shorts/.*)|(http.*youtube.com/v/.*)|(http.*youtu.be/.*)", url); output {
log.Println("YouTube detected.")
if shortned, _ := regexp.MatchString("(http.*youtu\\.be/.*)|(http.*youtube.com/v/.*)", url); shortned {
var err error
log.Println("Short URL detected. Getting long version.")
url, err = getRedirectURL(url)
if err != nil {
log.Println(err)
continue
}
}
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*youtube\\.com").ReplaceAllString(url, "https://piped.video"))
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
log.Println(err)
continue
} else if result.Info.AgeLimit < 18 && ! result.Info.IsLive {
/*
videoChoice, audioChoice, err := getLargestDashFormat(result, MaxSize)
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))
*/
choice, err := getLargestYTFormat(result, MaxSize)
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/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, videoChoice.Ext),
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)
}
}
respond = true
} else if output, _ := regexp.MatchString("(http.*instagram\\.com/p/.*)|(http.*instagram\\.com/tv/.*)|(http.*instagram\\.com/reel/.*)", url); output {
log.Println("Instagram detected.")
response.Content = response.Content + fmt.Sprintf("\n%s", regexp.MustCompile("http.*instagram\\.com").ReplaceAllString(url, "https://bibliogram.art"))
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
log.Println(err)
continue
} else {
session.ChannelTyping(message.ChannelID)
downloadResult, err := result.Download(context.Background(), "best") // Insta only has one possible format to download :(
if err != nil {
log.Println(err)
continue
}
// 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, MaxSize + 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 <= MaxSize && 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
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: bytes.NewReader(newBuf),
})
}
downloadResult.Close()
}
respond = true
} 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 {
choice, err := getLargestFormat(result, MaxSize)
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)
}
}
respond = true
} else if output, _ := regexp.MatchString("(http.*reddit\\.com/r/\\.*)", url); output {
log.Println("Reddit detected.")
response.Content = response.Content + fmt.Sprintf("\n<%s>", regexp.MustCompile("http.*reddit\\.com").ReplaceAllString(url, "https://libreddit.privacydev.net"))
// 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.
if noVideo, _ := regexp.MatchString(".*No media found.*", err.Error()); noVideo {
respond = true
} else {
log.Println(err)
}
continue
} else {
videoChoice, audioChoice, err := getLargestDashFormat(result, MaxSize)
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)
}
}
*/
respond = true
} 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.")
}
}
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)
}
}
}
}