EngiBot/main.go

122 lines
3.8 KiB
Go
Raw Normal View History

2022-07-01 02:28:20 -04:00
package main
import (
2022-07-01 02:30:35 -04:00
"flag"
"fmt"
"log"
2022-07-01 02:30:35 -04:00
"os"
"os/signal"
"syscall"
2022-07-01 02:28:20 -04:00
2022-07-01 02:30:35 -04:00
"github.com/bwmarrin/discordgo"
2022-07-01 02:28:20 -04:00
"github.com/wader/goutubedl"
)
func main() {
2022-07-01 17:06:58 -04:00
// Lazyinator 9001
var (
2022-07-02 02:53:19 -04:00
session *discordgo.Session
2022-07-01 17:06:58 -04:00
err error
)
// Variables used for registering slash commands
var (
commands = []*discordgo.ApplicationCommand {
{
Name: "ping",
Description: "Hopefully replies with pong or else I'll be sad.",
},
{
Name: "sping",
Description: "Hopefully replies with pong silently or else I'll be sad.",
},
{
Name: "slowping",
Description: "Hopefully replies with pong 10 seconds later or else I'll be sad.",
},
2022-07-17 19:43:08 -04:00
{
Name: "version",
Description: "Gets the bot's current version.",
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
2022-07-01 17:06:58 -04:00
"ping": ping,
"sping": sping,
"slowping": slowping,
2022-07-17 19:43:08 -04:00
"version": version,
}
)
2022-07-01 02:28:20 -04:00
// Bot params
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
)
2022-07-01 02:28:20 -04:00
goutubedl.Path = "yt-dlp"
2022-07-01 02:30:35 -04:00
flag.Parse()
2022-07-01 02:28:20 -04:00
2022-07-02 02:53:19 -04:00
session, err = discordgo.New("Bot " + *BotToken)
2022-07-01 02:28:20 -04:00
if err != nil {
2022-07-01 02:30:35 -04:00
fmt.Println("error creating Discord session,", err)
return
}
2022-07-01 02:28:20 -04:00
2022-07-02 02:53:19 -04:00
// This runs slash commands
session.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
if h, ok := commandHandlers[interaction.ApplicationCommandData().Name]; ok {
if interaction.Interaction.Member != nil {
log.Printf("%s#%s invoked command %s", interaction.Member.User.Username, interaction.Member.User.Discriminator, interaction.ApplicationCommandData().Name)
} else if interaction.Interaction.User != nil {
log.Printf("%s#%s invoked command %s", interaction.User.Username, interaction.User.Discriminator, interaction.ApplicationCommandData().Name)
}
h(session, interaction)
2022-07-01 02:30:35 -04:00
}
})
2022-07-01 02:28:20 -04:00
// Message to print when bot is ready to go
2022-07-02 02:53:19 -04:00
session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v\n", session.State.User.Username, session.State.User.Discriminator)
2022-07-01 02:30:35 -04:00
})
2022-07-01 02:28:20 -04:00
2022-07-01 02:30:35 -04:00
// Register the messageCreate func as a callback for MessageCreate events.
2022-07-02 02:53:19 -04:00
session.AddHandler(messageCreate)
2022-07-01 02:28:20 -04:00
2022-07-01 02:30:35 -04:00
// In this example, we only care about receiving message events.
2022-07-01 06:06:03 -04:00
//s.Identify.Intents = discordgo.IntentsGuildMessages
2022-07-01 02:28:20 -04:00
2022-07-01 02:30:35 -04:00
// Open a websocket connection to Discord and begin listening.
2022-07-02 02:53:19 -04:00
err = session.Open()
2022-07-01 02:30:35 -04:00
if err != nil {
fmt.Println("error opening connection,", err)
return
}
2022-07-01 02:28:20 -04:00
2022-07-01 06:06:03 -04:00
log.Println("Adding commands...")
2022-07-02 02:53:19 -04:00
registeredCommands, err := session.ApplicationCommandBulkOverwrite(session.State.User.ID, *GuildID, commands)
2022-07-01 06:06:03 -04:00
if err != nil {
log.Panic(err)
}
2022-07-01 02:28:20 -04:00
// Just incase
2022-07-02 02:53:19 -04:00
defer session.Close()
2022-07-01 02:28:20 -04:00
2022-07-01 02:30:35 -04:00
// Wait here until CTRL-C or other term signal is received.
log.Println("Bot is running. Press CTRL-C to exit.")
2022-07-01 02:30:35 -04:00
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
2022-07-01 02:28:20 -04:00
log.Printf("Signal received, closing bot...")
2022-07-01 02:30:35 -04:00
// Cleanly close down the Discord session.
2022-07-01 06:06:03 -04:00
for _, v := range registeredCommands {
2022-07-02 02:53:19 -04:00
err := session.ApplicationCommandDelete(session.State.User.ID, *GuildID, v.ID)
2022-07-01 06:06:03 -04:00
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
2022-07-02 02:53:19 -04:00
session.Close()
2022-07-01 02:28:20 -04:00
}