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"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -31,19 +32,55 @@ var (
|
|||||||
conspiracyIndex int
|
conspiracyIndex int
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
// TODO: update value after debugging
|
SendMessageIntervalMin int
|
||||||
SendMessageIntervalMin = 5
|
SendMessageIntervalMax int
|
||||||
// TODO: update value after debugging
|
SendMessageUnit time.Duration
|
||||||
SendMessageIntervalMax = 10
|
DeleteConspiracyDelay time.Duration
|
||||||
// TODO: update value after debugging
|
ConspiracyProbability float64
|
||||||
SendMessageUnit = time.Second
|
|
||||||
// TODO: update value after debugging
|
|
||||||
DeleteConspiracyDelay = 3 * time.Second
|
|
||||||
// TODO: update value after debugging
|
|
||||||
ConspiracyProbability = 0.4
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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() {
|
func main() {
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
if err := godotenv.Load(); err != nil {
|
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")
|
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
|
// Load messages at build time
|
||||||
if err := loadMessages(); err != nil {
|
if err := loadMessages(); err != nil {
|
||||||
log.Fatalf("[x] Error loading messages: %v", err)
|
log.Fatalf("[x] Error loading messages: %v", err)
|
||||||
@@ -127,7 +168,7 @@ func startScheduler(s *discordgo.Session, channelID string) {
|
|||||||
praiseIndex++
|
praiseIndex++
|
||||||
|
|
||||||
// Chance to send a conspiracy theory
|
// Chance to send a conspiracy theory
|
||||||
if rand.Float32() < ConspiracyProbability {
|
if rand.Float64() < ConspiracyProbability {
|
||||||
discordMessage := sendMessage(conspiracies[conspiracyIndex%len(conspiracies)], s, channelID)
|
discordMessage := sendMessage(conspiracies[conspiracyIndex%len(conspiracies)], s, channelID)
|
||||||
conspiracyIndex++
|
conspiracyIndex++
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user