EngiBot/main.go

198 lines
5.8 KiB
Go
Raw Normal View History

2022-07-01 02:28:20 -04:00
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()
}
}
}
}