HwidBot/main.go

116 lines
2.0 KiB
Go
Raw Normal View History

2024-07-06 16:18:05 +02:00
package main
import (
"fmt"
2024-07-06 17:16:13 +02:00
"log"
2024-07-06 16:55:30 +02:00
"os"
"os/signal"
"syscall"
2024-07-06 17:16:13 +02:00
"database/sql"
2024-07-06 16:55:30 +02:00
2024-07-06 16:29:25 +02:00
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
2024-07-06 17:16:13 +02:00
_ "github.com/lib/pq"
2024-07-06 16:18:05 +02:00
)
2024-07-06 17:16:13 +02:00
var (
config Config
client *discordgo.Session
db *sql.DB
)
2024-07-06 16:55:30 +02:00
2024-07-06 16:29:25 +02:00
type Config struct {
2024-07-06 16:59:12 +02:00
Discord Discord `toml:"discord"`
Database Database `toml:"database"`
2024-07-06 16:29:25 +02:00
}
2024-07-06 16:59:12 +02:00
type Discord struct {
Token string `toml:"client"`
GuildID string `toml:"guildid"`
}
type Database struct {
Host string `toml:"host"`
Port int `toml:"port"`
Name string `toml:"name"`
Username string `toml:"username"`
Password string `toml:"password"`
}
2024-07-06 16:29:25 +02:00
func loadConfig(filename string) (Config, error) {
var config Config
_, err := toml.DecodeFile(filename, &config)
return config, err
}
2024-07-06 17:16:13 +02:00
func connectDb(config Config) {
var err error
connectionString := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
config.Database.Host,
config.Database.Port,
config.Database.Username,
config.Database.Password,
config.Database.Name,
)
log.Println(connectionString)
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatalf("Error connecting to the database: %v", err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatalf("Error pinging the database: %v", err)
}
}
2024-07-06 16:55:30 +02:00
func init() {
2024-07-06 17:16:13 +02:00
var err error
config, err = loadConfig("config.toml")
2024-07-06 16:29:25 +02:00
if err != nil {
2024-07-06 17:16:13 +02:00
log.Println("Error occurred whilst trying to load config:", err)
2024-07-06 16:29:25 +02:00
return
}
2024-07-06 16:55:30 +02:00
2024-07-06 16:59:12 +02:00
client, err = discordgo.New("Bot " + config.Discord.Token)
2024-07-06 16:29:25 +02:00
if err != nil {
2024-07-06 17:16:13 +02:00
log.Println("Error initializing bot:", err)
2024-07-06 16:29:25 +02:00
return
}
2024-07-06 16:55:30 +02:00
2024-07-06 17:16:13 +02:00
connectDb(config)
2024-07-06 16:55:30 +02:00
2024-07-06 16:18:05 +02:00
}
2024-07-06 16:55:30 +02:00
func main() {
if client == nil {
2024-07-06 17:16:13 +02:00
log.Println("Bot client is not initialized")
2024-07-06 16:55:30 +02:00
return
}
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
2024-07-06 17:16:13 +02:00
log.Println("Bot is online")
2024-07-06 16:55:30 +02:00
})
err := client.Open()
if err != nil {
2024-07-06 17:16:13 +02:00
log.Println("Error opening connection:", err)
2024-07-06 16:55:30 +02:00
return
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
2024-07-06 17:16:13 +02:00
log.Println("Gracefully shutting down.")
2024-07-06 16:55:30 +02:00
client.Close()
}