Move code into main()
This commit is contained in:
32
main.go
32
main.go
@ -9,9 +9,6 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// A Session represents a connection to the Discord API.
|
||||
var s *discordgo.Session
|
||||
|
||||
/*
|
||||
/ping - The bot responds with "Pong!".
|
||||
*/
|
||||
@ -36,14 +33,13 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
/*
|
||||
init() is automatically called when the package is initialized.
|
||||
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.
|
||||
If it does, it calls the corresponding function from the map with the session and interaction as arguments.
|
||||
*/
|
||||
func init() {
|
||||
// Load .env file.
|
||||
func main() {
|
||||
/*
|
||||
Load the .env file into the environment.
|
||||
|
||||
You can create a .env file with the following contents:
|
||||
TOKEN=your-bot-token-here
|
||||
*/
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot load .env file: %v", err)
|
||||
@ -56,28 +52,26 @@ func init() {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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.
|
||||
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 {
|
||||
log.Printf("Handling '%v' command.", i.ApplicationCommandData().Name)
|
||||
h(s, i)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 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)
|
||||
})
|
||||
|
||||
// Open a websocket connection to Discord and begin listening.
|
||||
err := s.Open()
|
||||
err = session.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot open the session: %v", err)
|
||||
}
|
||||
@ -86,7 +80,7 @@ func main() {
|
||||
log.Println("Adding commands...")
|
||||
registeredCommands := make([]*discordgo.ApplicationCommand, len(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 {
|
||||
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
|
||||
}
|
||||
@ -95,7 +89,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Run s.Close() when the program exits.
|
||||
defer s.Close()
|
||||
defer session.Close()
|
||||
|
||||
// Wait here until CTRL-C or other term signal is received.
|
||||
stop := make(chan os.Signal, 1)
|
||||
|
Reference in New Issue
Block a user