HwidBot/main.go
2024-07-06 16:55:30 +02:00

66 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
)
var client *discordgo.Session
type Config struct {
Token string `toml:"token"`
GuildID string `toml:"guildId"`
}
func loadConfig(filename string) (Config, error) {
var config Config
_, err := toml.DecodeFile(filename, &config)
return config, err
}
func init() {
config, err := loadConfig("config.toml")
if err != nil {
fmt.Println("Error occurred whilst trying to load config:", err)
return
}
client, err = discordgo.New("Bot " + config.Token)
if err != nil {
fmt.Println("Error initializing bot:", err)
return
}
}
func main() {
if client == nil {
fmt.Println("Bot client is not initialized")
return
}
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
fmt.Println("Bot is online")
})
err := client.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
fmt.Println("Gracefully shutting down.")
client.Close()
}