2020-12-01 01:49:18 +00:00
|
|
|
// +build !uintonly
|
|
|
|
|
|
|
|
package testenv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Env struct {
|
|
|
|
BotToken string
|
|
|
|
ChannelID discord.ChannelID
|
|
|
|
VoiceChID discord.ChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-12-12 01:58:52 +00:00
|
|
|
globalEnv Env
|
|
|
|
globalErr error
|
|
|
|
once sync.Once
|
2020-12-01 01:49:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Must(t *testing.T) Env {
|
|
|
|
e, err := GetEnv()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnv() (Env, error) {
|
|
|
|
once.Do(getEnv)
|
2020-12-12 01:58:52 +00:00
|
|
|
return globalEnv, globalErr
|
2020-12-01 01:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getEnv() {
|
|
|
|
var token = os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
2020-12-12 01:58:52 +00:00
|
|
|
globalErr = errors.New("missing $BOT_TOKEN")
|
2020-12-01 01:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var cid = os.Getenv("CHANNEL_ID")
|
|
|
|
if cid == "" {
|
2020-12-12 01:58:52 +00:00
|
|
|
globalErr = errors.New("missing $CHANNEL_ID")
|
2020-12-01 01:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := discord.ParseSnowflake(cid)
|
|
|
|
if err != nil {
|
2020-12-12 01:58:52 +00:00
|
|
|
globalErr = errors.Wrap(err, "invalid $CHANNEL_ID")
|
2020-12-01 01:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sid = os.Getenv("VOICE_ID")
|
|
|
|
if sid == "" {
|
2020-12-12 01:58:52 +00:00
|
|
|
globalErr = errors.New("missing $VOICE_ID")
|
2020-12-01 01:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vid, err := discord.ParseSnowflake(sid)
|
|
|
|
if err != nil {
|
2020-12-12 01:58:52 +00:00
|
|
|
globalErr = errors.Wrap(err, "invalid $VOICE_ID")
|
2020-12-01 01:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:58:52 +00:00
|
|
|
globalEnv = Env{
|
2020-12-01 01:49:18 +00:00
|
|
|
BotToken: token,
|
|
|
|
ChannelID: discord.ChannelID(id),
|
|
|
|
VoiceChID: discord.ChannelID(vid),
|
|
|
|
}
|
|
|
|
}
|