This commit is contained in:
Logan G 2022-07-01 00:30:35 -06:00
parent ffe4aa6af5
commit 05b1adfcf9
Signed by: logan
GPG key ID: E328528C921E7A7A

138
main.go
View file

@ -1,16 +1,16 @@
package main package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"strings" "strings"
"regexp" "regexp"
"context" "context"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl" "github.com/wader/goutubedl"
) )
@ -18,29 +18,29 @@ import (
// Variables used for command line parameters // Variables used for command line parameters
var ( var (
commands = []*discordgo.ApplicationCommand { commands = []*discordgo.ApplicationCommand {
{ {
Name: "ping", Name: "ping",
Description: "Hopefully replies with pong or else I'll be sad.", Description: "Hopefully replies with pong or else I'll be sad.",
}, },
} }
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) { commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { "ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: "Hey there! Congratulations, you just executed your first slash command", Content: "Hey there! Congratulations, you just executed your first slash command",
}, },
}) })
}, },
} }
) )
*/ */
// Bot params // Bot params
var ( var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally") GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token") BotToken = flag.String("token", "", "Bot access token")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not") RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
) )
// Lazyinator 9001 // Lazyinator 9001
@ -50,86 +50,86 @@ var (
) )
func init() { func init() {
flag.Parse() flag.Parse()
s, err = discordgo.New("Bot " + *BotToken) s, err = discordgo.New("Bot " + *BotToken)
if err != nil { if err != nil {
fmt.Println("error creating Discord session,", err) fmt.Println("error creating Discord session,", err)
return return
} }
/* /*
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i) h(s, i)
} }
}) })
*/ */
} }
func main() { func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { 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) 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. // Register the messageCreate func as a callback for MessageCreate events.
s.AddHandler(messageCreate) s.AddHandler(messageCreate)
// In this example, we only care about receiving message events. // In this example, we only care about receiving message events.
s.Identify.Intents = discordgo.IntentsGuildMessages s.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening. // Open a websocket connection to Discord and begin listening.
err = s.Open() err = s.Open()
if err != nil { if err != nil {
fmt.Println("error opening connection,", err) fmt.Println("error opening connection,", err)
return return
} }
/* /*
fmt.Println("Adding commands...") fmt.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands { for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v) cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil { if err != nil {
//log.Panicf("Cannot create '%v' command: %v", v.Name, err) //log.Panicf("Cannot create '%v' command: %v", v.Name, err)
fmt.Println("Bad") fmt.Println("Bad")
panic(1) panic(1)
} }
registeredCommands[i] = cmd registeredCommands[i] = cmd
} }
*/ */
// Just incase // Just incase
defer s.Close() defer s.Close()
// Wait here until CTRL-C or other term signal is received. // Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.") fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc <-sc
// Cleanly close down the Discord session. // Cleanly close down the Discord session.
s.Close() s.Close()
} }
// This function will be called (due to AddHandler above) every time a new // 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. // message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself // Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice. // This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
return return
} }
/* /*
// If the message is "ping" reply with "Pong!" // If the message is "ping" reply with "Pong!"
if m.Content == "ping" { if m.Content == "ping" {
s.ChannelMessageSend(m.ChannelID, "Pong!") s.ChannelMessageSend(m.ChannelID, "Pong!")
} }
// If the message is "pong" reply with "Ping!" // If the message is "pong" reply with "Ping!"
if m.Content == "pong" { if m.Content == "pong" {
s.ChannelMessageSend(m.ChannelID, "Ping!") s.ChannelMessageSend(m.ChannelID, "Ping!")
} }
*/ */
if len(m.Embeds) > 0 { if len(m.Embeds) > 0 {