mirror of
https://github.com/the-programmers-hangout/swiftspiracy-bot.git
synced 2026-09-04 01:06:01 +02:00
move configurable times to env file
This commit is contained in:
+53
-12
@@ -8,6 +8,7 @@ import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -31,19 +32,55 @@ var (
|
||||
conspiracyIndex int
|
||||
)
|
||||
|
||||
const (
|
||||
// TODO: update value after debugging
|
||||
SendMessageIntervalMin = 5
|
||||
// TODO: update value after debugging
|
||||
SendMessageIntervalMax = 10
|
||||
// TODO: update value after debugging
|
||||
SendMessageUnit = time.Second
|
||||
// TODO: update value after debugging
|
||||
DeleteConspiracyDelay = 3 * time.Second
|
||||
// TODO: update value after debugging
|
||||
ConspiracyProbability = 0.4
|
||||
var (
|
||||
SendMessageIntervalMin int
|
||||
SendMessageIntervalMax int
|
||||
SendMessageUnit time.Duration
|
||||
DeleteConspiracyDelay time.Duration
|
||||
ConspiracyProbability float64
|
||||
)
|
||||
|
||||
func loadEnvConfig() error {
|
||||
var err error
|
||||
|
||||
// Message intervals
|
||||
SendMessageIntervalMin, err = strconv.Atoi(os.Getenv("SEND_MESSAGE_INTERVAL_MIN"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("SEND_MESSAGE_INTERVAL_MIN: %w", err)
|
||||
}
|
||||
SendMessageIntervalMax, err = strconv.Atoi(os.Getenv("SEND_MESSAGE_INTERVAL_MAX"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("SEND_MESSAGE_INTERVAL_MAX: %w", err)
|
||||
}
|
||||
|
||||
// Duration unit
|
||||
unit := os.Getenv("SEND_MESSAGE_UNIT")
|
||||
switch unit {
|
||||
case "second", "seconds":
|
||||
SendMessageUnit = time.Second
|
||||
case "minute", "minutes":
|
||||
SendMessageUnit = time.Minute
|
||||
case "millisecond", "milliseconds":
|
||||
SendMessageUnit = time.Millisecond
|
||||
default:
|
||||
return fmt.Errorf("unsupported SEND_MESSAGE_UNIT: %s", unit)
|
||||
}
|
||||
|
||||
// Conspiracy delete delay
|
||||
DeleteConspiracyDelay, err = time.ParseDuration(os.Getenv("DELETE_CONSPIRACY_DELAY"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("DELETE_CONSPIRACY_DELAY: %w", err)
|
||||
}
|
||||
|
||||
// Conspiracy probability
|
||||
ConspiracyProbability, err = strconv.ParseFloat(os.Getenv("CONSPIRACY_PROBABILITY"), 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("CONSPIRACY_PROBABILITY: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Load environment variables
|
||||
if err := godotenv.Load(); err != nil {
|
||||
@@ -56,6 +93,10 @@ func main() {
|
||||
log.Fatal("[x] Missing DISCORD_BOT_TOKEN or DISCORD_CHANNEL_ID in .env")
|
||||
}
|
||||
|
||||
if err := loadEnvConfig(); err != nil {
|
||||
log.Fatalf("[x] Error loading env config: %v", err)
|
||||
}
|
||||
|
||||
// Load messages at build time
|
||||
if err := loadMessages(); err != nil {
|
||||
log.Fatalf("[x] Error loading messages: %v", err)
|
||||
@@ -127,7 +168,7 @@ func startScheduler(s *discordgo.Session, channelID string) {
|
||||
praiseIndex++
|
||||
|
||||
// Chance to send a conspiracy theory
|
||||
if rand.Float32() < ConspiracyProbability {
|
||||
if rand.Float64() < ConspiracyProbability {
|
||||
discordMessage := sendMessage(conspiracies[conspiracyIndex%len(conspiracies)], s, channelID)
|
||||
conspiracyIndex++
|
||||
|
||||
|
||||
Reference in New Issue
Block a user