Move code into main()

This commit is contained in:
2024-01-21 14:30:18 +01:00
parent 5a0375a43d
commit 8903709b60

28
main.go
View File

@ -9,9 +9,6 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
// A Session represents a connection to the Discord API.
var s *discordgo.Session
/* /*
/ping - The bot responds with "Pong!". /ping - The bot responds with "Pong!".
*/ */
@ -36,14 +33,13 @@ var (
} }
) )
func main() {
/* /*
init() is automatically called when the package is initialized. Load the .env file into the environment.
It adds a handler function to the discordgo.Session that is triggered when a slash command is received.
The handler function checks if the interaction's application command name exists in the commandHandlers map. You can create a .env file with the following contents:
If it does, it calls the corresponding function from the map with the session and interaction as arguments. TOKEN=your-bot-token-here
*/ */
func init() {
// Load .env file.
err := godotenv.Load() err := godotenv.Load()
if err != nil { if err != nil {
log.Fatalf("Cannot load .env file: %v", err) log.Fatalf("Cannot load .env file: %v", err)
@ -56,28 +52,26 @@ func init() {
} }
// Create a new Discord session using the provided bot token. // Create a new Discord session using the provided bot token.
s, err = discordgo.New("Bot " + token) session, err := discordgo.New("Bot " + token)
if err != nil { if err != nil {
log.Fatalf("Cannot create a new Discord session: %v", err) log.Fatalf("Cannot create a new Discord session: %v", err)
} }
// Add a handler function to the discordgo.Session that is triggered when a slash command is received. // Add a handler function to the discordgo.Session that is triggered when a slash command is received.
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { session.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
log.Printf("Handling '%v' command.", i.ApplicationCommandData().Name) log.Printf("Handling '%v' command.", i.ApplicationCommandData().Name)
h(s, i) h(s, i)
} }
}) })
}
func main() {
// Print the user we are logging in as. // Print the user we are logging in as.
s.AddHandler(func(s *discordgo.Session, _ *discordgo.Ready) { session.AddHandler(func(s *discordgo.Session, _ *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
}) })
// Open a websocket connection to Discord and begin listening. // Open a websocket connection to Discord and begin listening.
err := s.Open() err = session.Open()
if err != nil { if err != nil {
log.Fatalf("Cannot open the session: %v", err) log.Fatalf("Cannot open the session: %v", err)
} }
@ -86,7 +80,7 @@ func main() {
log.Println("Adding commands...") log.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, "341001473661992962", v) cmd, err := session.ApplicationCommandCreate(session.State.User.ID, "341001473661992962", 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)
} }
@ -95,7 +89,7 @@ func main() {
} }
// Run s.Close() when the program exits. // Run s.Close() when the program exits.
defer s.Close() defer session.Close()
// Wait here until CTRL-C or other term signal is received. // Wait here until CTRL-C or other term signal is received.
stop := make(chan os.Signal, 1) stop := make(chan os.Signal, 1)