Use our own settings file instead of using godotenv

This commit is contained in:
2024-01-21 15:32:26 +01:00
parent 8903709b60
commit f3d11de24e
7 changed files with 81 additions and 26 deletions

View File

@ -1 +0,0 @@
TOKEN=

2
.gitignore vendored
View File

@ -21,4 +21,4 @@ vendor/
go.work
# Discord bot token
.env
settings.json

View File

@ -2,8 +2,18 @@
Shit Discord bot
## Settings file
Create a settings.json file or use the environment variables below
```json
{
"discord_token": "your-bot-token-here"
}
```
## Environment variables
| Env var | Desc | Example |
| ------- | ----------------- | ----------------------------------------------------------- |
| Variable | Description | Example |
| -------- | ----------------- | ----------------------------------------------------------- |
| TOKEN | Discord bot token | MzQ2MDAwODAxNDk5ODk0Nzk1.XkK-7A.w18w6Z99c5DXi8ubbSNbj32lMZo |

5
go.mod
View File

@ -2,10 +2,7 @@ module github.com/TheLovinator1/ANewDawn
go 1.21.6
require (
github.com/bwmarrin/discordgo v0.27.1
github.com/joho/godotenv v1.5.1
)
require github.com/bwmarrin/discordgo v0.27.1
require (
github.com/gorilla/websocket v1.4.2 // indirect

2
go.sum
View File

@ -2,8 +2,6 @@ github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4Ho
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

22
main.go
View File

@ -1,12 +1,12 @@
package main
import (
"fmt"
"log"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
/*
@ -34,25 +34,17 @@ var (
)
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()
config, err := Load()
if err != nil {
log.Fatalf("Cannot load .env file: %v", err)
log.Fatal(err)
}
// Get the bot token from the environment.
token := os.Getenv("TOKEN")
if token == "" {
log.Fatalln("No token provided. Please set the TOKEN environment variable.")
}
// Print the token for debugging purposes.
discordToken := config.DiscordToken
fmt.Println("Discord Token:", discordToken)
// Create a new Discord session using the provided bot token.
session, err := discordgo.New("Bot " + token)
session, err := discordgo.New("Bot " + discordToken)
if err != nil {
log.Fatalf("Cannot create a new Discord session: %v", err)
}

59
settings.go Normal file
View File

@ -0,0 +1,59 @@
package main
import (
"encoding/json"
"fmt"
"os"
)
// Config holds the configuration parameters
type Config struct {
DiscordToken string `json:"discord_token"`
}
// Load reads configuration from settings.json or environment variables
func Load() (*Config, error) {
// Try reading from settings.json file first
config, err := loadFromJSONFile("settings.json")
if err != nil {
// If reading from file fails, try reading from environment variables
config, err = loadFromEnvironment()
if err != nil {
return nil, err
}
}
return config, nil
}
// loadFromJSONFile reads configuration from a JSON file
func loadFromJSONFile(filename string) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open settings file: %v", err)
}
defer file.Close()
config := &Config{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return nil, fmt.Errorf("failed to decode settings file: %v", err)
}
return config, nil
}
// loadFromEnvironment reads configuration from environment variables
func loadFromEnvironment() (*Config, error) {
discordToken := os.Getenv("DISCORD_TOKEN")
if discordToken == "" {
return nil, fmt.Errorf("DISCORD_TOKEN environment variable not set or empty. Also tried reading from settings.json file")
}
config := &Config{
DiscordToken: discordToken,
}
return config, nil
}