Use our own settings file instead of using godotenv
This commit is contained in:
@ -1 +0,0 @@
|
|||||||
TOKEN=
|
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,4 +21,4 @@ vendor/
|
|||||||
go.work
|
go.work
|
||||||
|
|
||||||
# Discord bot token
|
# Discord bot token
|
||||||
.env
|
settings.json
|
||||||
|
14
README.md
14
README.md
@ -2,8 +2,18 @@
|
|||||||
|
|
||||||
Shit Discord bot
|
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
|
## Environment variables
|
||||||
|
|
||||||
| Env var | Desc | Example |
|
| Variable | Description | Example |
|
||||||
| ------- | ----------------- | ----------------------------------------------------------- |
|
| -------- | ----------------- | ----------------------------------------------------------- |
|
||||||
| TOKEN | Discord bot token | MzQ2MDAwODAxNDk5ODk0Nzk1.XkK-7A.w18w6Z99c5DXi8ubbSNbj32lMZo |
|
| TOKEN | Discord bot token | MzQ2MDAwODAxNDk5ODk0Nzk1.XkK-7A.w18w6Z99c5DXi8ubbSNbj32lMZo |
|
||||||
|
5
go.mod
5
go.mod
@ -2,10 +2,7 @@ module github.com/TheLovinator1/ANewDawn
|
|||||||
|
|
||||||
go 1.21.6
|
go 1.21.6
|
||||||
|
|
||||||
require (
|
require github.com/bwmarrin/discordgo v0.27.1
|
||||||
github.com/bwmarrin/discordgo v0.27.1
|
|
||||||
github.com/joho/godotenv v1.5.1
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/websocket v1.4.2 // indirect
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -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/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 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
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 h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
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=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
22
main.go
22
main.go
@ -1,12 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -34,25 +34,17 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
/*
|
config, err := Load()
|
||||||
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()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Cannot load .env file: %v", err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the bot token from the environment.
|
// Print the token for debugging purposes.
|
||||||
token := os.Getenv("TOKEN")
|
discordToken := config.DiscordToken
|
||||||
if token == "" {
|
fmt.Println("Discord Token:", discordToken)
|
||||||
log.Fatalln("No token provided. Please set the TOKEN environment variable.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new Discord session using the provided bot token.
|
// 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 {
|
if err != nil {
|
||||||
log.Fatalf("Cannot create a new Discord session: %v", err)
|
log.Fatalf("Cannot create a new Discord session: %v", err)
|
||||||
}
|
}
|
||||||
|
59
settings.go
Normal file
59
settings.go
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user