More cleanup, split messageCreate into it's own file
This commit is contained in:
parent
24622e7b38
commit
7f2c9fa130
2 changed files with 144 additions and 148 deletions
148
main.go
148
main.go
|
@ -7,13 +7,9 @@ import (
|
|||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
//"strings"
|
||||
"regexp"
|
||||
"context"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/wader/goutubedl"
|
||||
"mvdan.cc/xurls/v2" // Peak lazy
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -123,147 +119,3 @@ func main() {
|
|||
}
|
||||
session.Close()
|
||||
}
|
||||
|
||||
// This function will be called (due to AddHandler above) 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
|
||||
// This isn't required in this specific example but it's a good practice.
|
||||
if message.Author.ID == session.State.User.ID {
|
||||
return
|
||||
}
|
||||
/*
|
||||
// If the message is "ping" reply with "Pong!"
|
||||
if message.Content == "ping" {
|
||||
session.ChannelMessageSend(message.ChannelID, "Pong!")
|
||||
}
|
||||
|
||||
// If the message is "pong" reply with "Ping!"
|
||||
if message.Content == "pong" {
|
||||
session.ChannelMessageSend(message.ChannelID, "Ping!")
|
||||
}
|
||||
*/
|
||||
|
||||
//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!\n",
|
||||
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/.*)", 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("<%s>", regexp.MustCompile("http.*twitter.com").ReplaceAllString(url, "https://nitter.pussthecat.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(".*There's no video in this tweet.*", err.Error()); noVideo {
|
||||
respond = true
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
continue
|
||||
} else {
|
||||
choice, err := getLargestFormat(result, 8*1024*1024)
|
||||
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024)
|
||||
if err == nil {
|
||||
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.*youtu.be/.*)", url); output {
|
||||
log.Println("YouTube detected.")
|
||||
|
||||
if shortned, _ := regexp.MatchString("http.*youtu\\.be/.*", 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("<%s>", regexp.MustCompile("http.*youtube\\.com").ReplaceAllString(url, "https://piped.kavin.rocks"))
|
||||
|
||||
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
} else {
|
||||
videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024)
|
||||
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))
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
144
messageCreate.go
Normal file
144
messageCreate.go
Normal file
|
@ -0,0 +1,144 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"context"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/wader/goutubedl"
|
||||
"mvdan.cc/xurls/v2" // Peak lazy
|
||||
)
|
||||
|
||||
// This function will be called (due to AddHandler above) 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!\n",
|
||||
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/.*)", 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("<%s>", regexp.MustCompile("http.*twitter.com").ReplaceAllString(url, "https://nitter.pussthecat.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(".*There's no video in this tweet.*", err.Error()); noVideo {
|
||||
respond = true
|
||||
} else {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
continue
|
||||
} else {
|
||||
choice, err := getLargestFormat(result, 8*1024*1024)
|
||||
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, choice.FilesizeApprox/1024/1024)
|
||||
if err == nil {
|
||||
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.*youtu.be/.*)", url); output {
|
||||
log.Println("YouTube detected.")
|
||||
|
||||
if shortned, _ := regexp.MatchString("http.*youtu\\.be/.*", 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("<%s>", regexp.MustCompile("http.*youtube\\.com").ReplaceAllString(url, "https://piped.kavin.rocks"))
|
||||
|
||||
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
} else {
|
||||
videoChoice, audioChoice, err := getLargestDashFormat(result, 8*1024*1024)
|
||||
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))
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue