Made code 1000% less jank and prone to breaking

This commit is contained in:
Logan G 2022-07-01 01:48:26 -06:00
parent 05b1adfcf9
commit c0e74eb633
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 49 additions and 29 deletions

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.18
require (
github.com/bwmarrin/discordgo v0.25.0
github.com/wader/goutubedl v0.0.0-20220629162042-a29fe0a651a9
mvdan.cc/xurls/v2 v2.4.0
)
require (

10
go.sum
View file

@ -4,6 +4,11 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/wader/goutubedl v0.0.0-20220629162042-a29fe0a651a9 h1:minVdIOluC83sUlMat3QAUwegFHDfLcIPYvjSHLFtsY=
github.com/wader/goutubedl v0.0.0-20220629162042-a29fe0a651a9/go.mod h1:5KXd5tImdbmz4JoVhePtbIokCwAfEhUVVx3WLHmjYuw=
github.com/wader/osleaktest v0.0.0-20191111175233-f643b0fed071 h1:QkrG4Zr5OVFuC9aaMPmFI0ibfhBZlAgtzDYWfu7tqQk=
@ -11,8 +16,13 @@ github.com/wader/osleaktest v0.0.0-20191111175233-f643b0fed071/go.mod h1:XD6emOF
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
mvdan.cc/xurls/v2 v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
mvdan.cc/xurls/v2 v2.4.0/go.mod h1:+GEjq9uNjqs8LQfM9nVnM8rff0OQ5Iash5rzX+N1CSg=

69
main.go
View file

@ -3,15 +3,18 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"strings"
"regexp"
"context"
"reflect"
"github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl"
"mvdan.cc/xurls/v2" // Peak lazy
)
/*
@ -69,7 +72,7 @@ func init() {
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
fmt.Printf("Logged in as: %v#%v\n", s.State.User.Username, s.State.User.Discriminator)
log.Printf("Logged in as: %v#%v\n", s.State.User.Username, s.State.User.Discriminator)
})
// Register the messageCreate func as a callback for MessageCreate events.
@ -103,7 +106,7 @@ func main() {
defer s.Close()
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
log.Println("Bot is starting. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
@ -132,66 +135,72 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
}
*/
if len(m.Embeds) > 0 {
log.Printf("[%s] %s#%s: %s", m.ID, m.Author.Username, m.Author.Discriminator, m.Content)
rxStrict := xurls.Strict()
urls := rxStrict.FindAllString(m.Content, -1)
if len(urls) > 0 {
message := discordgo.MessageSend {
Content: "Woah there partner! I detect some services that don't embed very well in Discord. Let me help you with that!\n",
Reference: m.Reference(),
Files: []*discordgo.File{},
}
fmt.Println("Embeds detected!")
for _, v := range m.Embeds {
url := v.URL
log.Println("URLs detected!")
for _, url := range urls {
if output, _ := regexp.MatchString("http.*twitter", url); output {
fmt.Println("Cringe twitter detected")
log.Println("Cringe twitter detected.")
goutubedl.Path = "yt-dlp"
result, err := goutubedl.New(context.Background(), url, goutubedl.Options{})
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
continue
}
//message.Content = message.Content + strings.Replace(url, "twitter.com", "nitter.net", -1) + "\n"
message.Content = message.Content + "\""+ strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ") + "\"\n" //Learn what sprintf is you stupid whore
message.Content = fmt.Sprintf("%s \"%s\"\n", message.Content, strings.Trim(regexp.MustCompile("https://t.co/.*").ReplaceAllString(result.Info.Description, ""), " ")) // Still ugly
var choice string
var choice goutubedl.Format
for i := len(result.Formats())-1; i >= 0; i-- {
size := result.Formats()[i].FilesizeApprox
if size < 8*1024*1024 {
choice = result.Formats()[i].FormatID
fmt.Printf("Choice: %s | Size: %fM\n", choice, size/1024/1024)
choice = result.Formats()[i]
log.Printf("Choice: %s | Size: %fM\n", choice.FormatID, size/1024/1024)
break
}
}
downloadResult, err := result.Download(context.Background(), choice)
downloadResult, err := result.Download(context.Background(), choice.FormatID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, err.Error())
continue
}
//var data []byte
//downloadResult.Read(*data)
//reader := bytes.NewReader(data)
//fmt.Println(len(data))
//fmt.Println(downloadResult)
fmt.Println(reflect.TypeOf(message.Files))
/*
message.Files = []*discordgo.File{&discordgo.File {
Name: "video.mp4",
ContentType: "text/plain",
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,
}}
*/
message.Files = append(message.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()
}
}
if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil {
fmt.Println(message)
fmt.Println(err)
}
downloadResult.Close()
}
log.Println(message)
log.Println(err)
} else {
log.Printf("Successfully responded to %s", m.ID)
}
}
}