diff --git a/go.mod b/go.mod index e6f6a3c..1746428 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/Brawl345/gobot -go 1.21 - -toolchain go1.21.5 +go 1.22 require ( github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.27 diff --git a/model/sql/sql.go b/model/sql/sql.go index f602362..6351650 100644 --- a/model/sql/sql.go +++ b/model/sql/sql.go @@ -1,6 +1,7 @@ package sql import ( + "cmp" "database/sql" "embed" "fmt" @@ -20,21 +21,12 @@ var log = logger.New("db") var embeddedMigrations embed.FS func New() (*sqlx.DB, error) { - host := strings.TrimSpace(os.Getenv("MYSQL_HOST")) - if host == "" { - host = "localhost" - } - port := strings.TrimSpace(os.Getenv("MYSQL_PORT")) - if port == "" { - port = "3306" - } + host := cmp.Or(strings.TrimSpace(os.Getenv("MYSQL_HOST")), "localhost") + port := cmp.Or(strings.TrimSpace(os.Getenv("MYSQL_PORT")), "3306") user := strings.TrimSpace(os.Getenv("MYSQL_USER")) password := strings.TrimSpace(os.Getenv("MYSQL_PASSWORD")) dbname := strings.TrimSpace(os.Getenv("MYSQL_DB")) - tls := strings.TrimSpace(os.Getenv("MYSQL_TLS")) - if tls == "" { - tls = "false" - } + tls := cmp.Or(strings.TrimSpace(os.Getenv("MYSQL_TLS")), "false") connectionString := fmt.Sprintf( "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s", diff --git a/plugin/gemini/gemini.go b/plugin/gemini/gemini.go index 8d5f4a4..17268f7 100644 --- a/plugin/gemini/gemini.go +++ b/plugin/gemini/gemini.go @@ -1,6 +1,7 @@ package gemini import ( + "cmp" "encoding/json" "errors" "fmt" @@ -101,10 +102,7 @@ func (p *Plugin) onGemini(b *gotgbot.Bot, c plugin.GobotContext) error { apiUrlGemini = proxyUrlGemini } - systemInstruction := p.credentialService.GetKey("google_gemini_system_instruction") - if systemInstruction == "" { - systemInstruction = DefaultSystemInstruction - } + systemInstruction := cmp.Or(p.credentialService.GetKey("google_gemini_system_instruction"), DefaultSystemInstruction) var contents []Content geminiData, err := p.geminiService.GetHistory(c.EffectiveChat) diff --git a/plugin/getfile/getfile.go b/plugin/getfile/getfile.go index 2b1defe..2c33a50 100644 --- a/plugin/getfile/getfile.go +++ b/plugin/getfile/getfile.go @@ -1,6 +1,7 @@ package getfile import ( + "cmp" "io" "os" "path" @@ -119,10 +120,7 @@ func (p *Plugin) OnMedia(b *gotgbot.Bot, c plugin.GobotContext) error { return nil } - dir := p.credentialService.GetKey("getfile_dir") - if dir == "" { - dir = "Files" - } + dir := cmp.Or(p.credentialService.GetKey("getfile_dir"), "Files") savePath := filepath.Join(dir, subFolder) err = os.MkdirAll(savePath, 0770) diff --git a/utils/tgUtils/utils.go b/utils/tgUtils/utils.go index 77f04e9..90e0d80 100644 --- a/utils/tgUtils/utils.go +++ b/utils/tgUtils/utils.go @@ -1,6 +1,7 @@ package tgUtils import ( + "cmp" "errors" "os" "strconv" @@ -101,15 +102,8 @@ func AddRectionWithFallback(b *gotgbot.Bot, message *gotgbot.Message, emoji stri var telegramErr *gotgbot.TelegramError if err != nil && errors.As(err, &telegramErr) && telegramErr.Description == ErrReactionInvalid { - fallback := opts.Fallback - if fallback == "" { - fallback = emoji - } - - sendMessageOpts := opts.SendMessageOpts - if sendMessageOpts == nil { - sendMessageOpts = utils.DefaultSendOptions() - } + fallback := cmp.Or(opts.Fallback, emoji) + sendMessageOpts := cmp.Or(opts.SendMessageOpts, utils.DefaultSendOptions()) _, err = message.Reply(b, fallback, sendMessageOpts) }