EngiBot/main.go

121 lines
3.8 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/wader/goutubedl"
)
func main() {
// Lazyinator 9001
var (
session *discordgo.Session
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.",
},
{
Name: "version",
Description: "Gets the bot's current version.",
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate) {
"ping": ping,
"sping": sping,
"slowping": slowping,
"version": version,
}
)
// Bot params
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
)
goutubedl.Path = "yt-dlp"
flag.Parse()
session, err = discordgo.New("Bot " + *BotToken)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
// 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)
}
})
// Message to print when bot is ready to go
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)
})
// Register the messageCreate func as a callback for MessageCreate events.
session.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 = session.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
log.Println("Adding commands...")
registeredCommands, err := session.ApplicationCommandBulkOverwrite(session.State.User.ID, *GuildID, commands)
if err != nil {
log.Panic(err)
}
// Just incase
defer session.Close()
// Wait here until CTRL-C or other term signal is received.
log.Println("Bot is running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Printf("Signal received, closing bot...")
// Cleanly close down the Discord session.
for _, v := range registeredCommands {
err := session.ApplicationCommandDelete(session.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
session.Close()
}