This repository has been archived on 2024-05-11. You can view files and clone it, but cannot push or open issues or pull requests.
Corn-Utility/loaders/json.go

62 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-10-19 18:08:38 -04:00
package loaders
import (
"encoding/json"
"fmt"
"os"
)
type Tokens struct {
DeployCommands bool `json:"deployCommands"`
Bot string `json:"bot"`
HookID string `json:"hookId"`
HookToken string `json:"hookToken"`
BotPublicKey string `json:"botPublicKey"`
}
var (
tokens Tokens
invalidToken = "Token not found; Please setup a \"tokens.json\" file in root directory."
)
func LoadJSON(path string, v interface{}) error {
var file, err = os.Open(path)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&v)
if err != nil {
fmt.Println(err.Error())
}
return err
}
func TokenLoader(token string) string {
err := LoadJSON("tokens.json", &tokens)
if err != nil {
return invalidToken
}
switch token {
case "bot":
return tokens.Bot
case "hookId":
return tokens.HookID
case "hookToken":
return tokens.HookToken
case "botPublicKey":
return tokens.BotPublicKey
}
return invalidToken
}
func IsCmdsDeployable() bool {
err := LoadJSON("tokens.json", &tokens)
if err != nil {
return false
}
return tokens.DeployCommands
}