Actually add db connection
This commit is contained in:
parent
e8a216b9d7
commit
9afda27ab4
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.22.4
|
|||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.4.0
|
github.com/BurntSushi/toml v1.4.0
|
||||||
github.com/bwmarrin/discordgo v0.28.1
|
github.com/bwmarrin/discordgo v0.28.1
|
||||||
|
github.com/lib/pq v1.10.9
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
54
main.go
54
main.go
@ -2,15 +2,23 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *discordgo.Session
|
var (
|
||||||
|
config Config
|
||||||
|
client *discordgo.Session
|
||||||
|
db *sql.DB
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Discord Discord `toml:"discord"`
|
Discord Discord `toml:"discord"`
|
||||||
@ -37,35 +45,63 @@ func loadConfig(filename string) (Config, error) {
|
|||||||
return config, err
|
return config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func connectDb(config Config) {
|
||||||
config, err := loadConfig("config.toml")
|
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 {
|
if err != nil {
|
||||||
fmt.Println("Error occurred whilst trying to load config:", err)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
config, err = loadConfig("config.toml")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error occurred whilst trying to load config:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err = discordgo.New("Bot " + config.Discord.Token)
|
client, err = discordgo.New("Bot " + config.Discord.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error initializing bot:", err)
|
log.Println("Error initializing bot:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connectDb(config)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
fmt.Println("Bot client is not initialized")
|
log.Println("Bot client is not initialized")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
client.AddHandler(func(client *discordgo.Session, r *discordgo.Ready) {
|
||||||
fmt.Println("Bot is online")
|
log.Println("Bot is online")
|
||||||
})
|
})
|
||||||
|
|
||||||
err := client.Open()
|
err := client.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error opening connection:", err)
|
log.Println("Error opening connection:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +109,7 @@ func main() {
|
|||||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||||
<-stop
|
<-stop
|
||||||
|
|
||||||
fmt.Println("Gracefully shutting down.")
|
log.Println("Gracefully shutting down.")
|
||||||
client.Close()
|
client.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user