commit ffe4aa6af54d183f3066ce3a150ab14befe98b7f Author: Logan Gartner Date: Fri Jul 1 00:28:20 2022 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89f9ac0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +out/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e9e8e7c --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2022 Logan Gartner + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ede6f18 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.PHONY: build + +GIT_COMMIT := $(shell git rev-parse HEAD) +BUILD_DATE := $(shell date -u +.%Y%m%d.%H%M%S) +CGO_ENABLED=0 + +default: + CGO_ENABLED=$(CGO_ENABLED) go build -o out/engibot -trimpath -ldflags "-s -w -X main.gitCommit=$(GIT_COMMIT)" main.go + +debug: + go build -o out/ -ldflags "-X main.gitCommit=$(GIT_COMMIT)" main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9a0a15 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# EngiBot +Shitty Discord bot I made in Go because Kerby can't stop posting Twitter links diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3c5c79a --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module EngiBot + +go 1.18 + +require ( + github.com/bwmarrin/discordgo v0.25.0 + github.com/wader/goutubedl v0.0.0-20220629162042-a29fe0a651a9 +) + +require ( + github.com/gorilla/websocket v1.4.2 // indirect + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect + golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..32ed685 --- /dev/null +++ b/go.sum @@ -0,0 +1,18 @@ +github.com/bwmarrin/discordgo v0.25.0 h1:NXhdfHRNxtwso6FPdzW2i3uBvvU7UIQTghmV2T4nqAs= +github.com/bwmarrin/discordgo v0.25.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= +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/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= +github.com/wader/osleaktest v0.0.0-20191111175233-f643b0fed071/go.mod h1:XD6emOFPHVzb0+qQpiNOdPL2XZ0SRUM0N5JHuq6OmXo= +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/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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..37be2c6 --- /dev/null +++ b/main.go @@ -0,0 +1,197 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/signal" + "syscall" + "strings" + "regexp" + "context" + + "github.com/bwmarrin/discordgo" + "github.com/wader/goutubedl" +) + +/* +// Variables used for command line parameters +var ( + commands = []*discordgo.ApplicationCommand { + { + Name: "ping", + Description: "Hopefully replies with pong or else I'll be sad.", + }, + } + commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) { + "ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Hey there! Congratulations, you just executed your first slash command", + }, + }) + }, + } +) +*/ + +// Bot params +var ( + GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally") + BotToken = flag.String("token", "", "Bot access token") + RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") +) + +// Lazyinator 9001 +var ( + s *discordgo.Session + err error +) + +func init() { + flag.Parse() + + s, err = discordgo.New("Bot " + *BotToken) + if err != nil { + fmt.Println("error creating Discord session,", err) + return + } + + /* + s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { + h(s, i) + } + }) + */ +} + +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) + }) + + // Register the messageCreate func as a callback for MessageCreate events. + s.AddHandler(messageCreate) + + // In this example, we only care about receiving message events. + s.Identify.Intents = discordgo.IntentsGuildMessages + + // Open a websocket connection to Discord and begin listening. + err = s.Open() + if err != nil { + fmt.Println("error opening connection,", err) + return + } + + /* + fmt.Println("Adding commands...") + registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) + for i, v := range commands { + cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v) + if err != nil { + //log.Panicf("Cannot create '%v' command: %v", v.Name, err) + fmt.Println("Bad") + panic(1) + } + registeredCommands[i] = cmd + } + */ + + // Just incase + 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.") + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + + // Cleanly close down the Discord session. + s.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(s *discordgo.Session, m *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 m.Author.ID == s.State.User.ID { + return + } + /* + // If the message is "ping" reply with "Pong!" + if m.Content == "ping" { + s.ChannelMessageSend(m.ChannelID, "Pong!") + } + + // If the message is "pong" reply with "Ping!" + if m.Content == "pong" { + s.ChannelMessageSend(m.ChannelID, "Ping!") + } + */ + + if len(m.Embeds) > 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(), + } + + fmt.Println("Embeds detected!") + + for _, v := range m.Embeds { + url := v.URL + + if output, _ := regexp.MatchString("http.*twitter", url); output { + fmt.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()) + } + + //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 + + var choice string + 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) + break + } + } + + downloadResult, err := result.Download(context.Background(), choice) + if err != nil { + s.ChannelMessageSend(m.ChannelID, err.Error()) + } + + //var data []byte + //downloadResult.Read(*data) + //reader := bytes.NewReader(data) + + //fmt.Println(len(data)) + //fmt.Println(downloadResult) + + + message.Files = []*discordgo.File{&discordgo.File { + Name: "video.mp4", + ContentType: "text/plain", + Reader: downloadResult, + }} + + if message, err := s.ChannelMessageSendComplex(m.ChannelID, &message); err != nil { + fmt.Println(message) + fmt.Println(err) + } + + downloadResult.Close() + } + } + } +}