55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
var token string
|
|
|
|
func init() {
|
|
// load env variables
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file.")
|
|
}
|
|
|
|
token = os.Getenv("TOKEN")
|
|
}
|
|
|
|
func main() {
|
|
// Create a new Discord session using the provided bot token.
|
|
dg, err := discordgo.New("Bot " + token)
|
|
if err != nil {
|
|
fmt.Println("Error creating Discord session,", err)
|
|
return
|
|
}
|
|
|
|
// Register the messageCreate func as a callback for MessageCreate events.
|
|
dg.AddHandler(messageCreate)
|
|
|
|
// In this example, we only care about receiving message events.
|
|
dg.Identify.Intents = discordgo.IntentsGuildMessages
|
|
|
|
// Open a websocket connection to Discord and begin listening.
|
|
err = dg.Open()
|
|
if err != nil {
|
|
fmt.Println("Error opening connection,", err)
|
|
return
|
|
}
|
|
|
|
// Wait here until CTRL-C or other term signal is received.
|
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
|
sc := make(chan os.Signal, 1)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
<-sc
|
|
|
|
// Cleanly close down the Discord session.
|
|
dg.Close()
|
|
}
|