From cec5ba20663e4c0027080881f56037de92a4a96f Mon Sep 17 00:00:00 2001 From: Anony <73958752+anonyindian@users.noreply.github.com> Date: Thu, 11 Aug 2022 10:19:06 +0000 Subject: [PATCH] fix some bugs and pre-commit for updater --- bot/client.go | 2 +- changelog.json | 2 +- main.go | 2 ++ modules/update.go | 40 +++++++++++++++++++++++ utils/update.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 modules/update.go create mode 100644 utils/update.go diff --git a/bot/client.go b/bot/client.go index 9897681..7eb614e 100644 --- a/bot/client.go +++ b/bot/client.go @@ -69,5 +69,5 @@ func StartClient(l *logger.Logger, b *gotgbot.Bot) { }, }, }) - log.Println("STARTED") + log.ChangeLevel(logger.LevelMain).Println("STARTED") } diff --git a/changelog.json b/changelog.json index 899c33a..e0596b1 100644 --- a/changelog.json +++ b/changelog.json @@ -1,4 +1,4 @@ { "Version": "0.0.0", - "Changes": [] + "Changes": [""] } \ No newline at end of file diff --git a/main.go b/main.go index 0cf9c30..49ffd5c 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ func main() { // Clean Console os.Stderr.Write([]byte("\n")) } + if *delay != 0 { l.Println("Delaying start for", *delay, "seconds") time.Sleep(time.Second * time.Duration(*delay)) @@ -47,6 +48,7 @@ func main() { if config.DEBUG { l.ChangeMinimumLevel(logger.LevelDebug) } + utils.InitUpdate(l) config.Load(l) handlers.DefaultPrefix = []rune{'.', '$'} db.Load(l) diff --git a/modules/update.go b/modules/update.go new file mode 100644 index 0000000..756d594 --- /dev/null +++ b/modules/update.go @@ -0,0 +1,40 @@ +package modules + +import ( + "fmt" + + "github.com/anonyindian/gotgproto/dispatcher" + "github.com/anonyindian/gotgproto/dispatcher/handlers" + "github.com/anonyindian/gotgproto/ext" + "github.com/anonyindian/gotgproto/parsemode/entityhelper" + "github.com/gigauserbot/giga/utils" + "github.com/gotd/td/tg" +) + +func (m *module) LoadUpdate(dp *dispatcher.CustomDispatcher) { + dp.AddHandler(handlers.NewCommand("changelog", changeLog)) +} + +func changeLog(ctx *ext.Context, u *ext.Update) error { + chat := u.EffectiveChat() + newUpdate, changed := utils.CheckChanges() + if !changed { + ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{ + Message: "You're currently running the latest version.", + ID: u.EffectiveMessage.ID, + }) + return dispatcher.EndGroups + } + text := entityhelper.Combine("New GIGA Update", entityhelper.BoldEntity, entityhelper.UnderlineEntity) + text.Bold("\nVersion: ").Code(newUpdate.Version) + text.Bold("\nChange-log:") + for _, change := range newUpdate.Changes { + text.Plain(fmt.Sprintf("\n • %s", change)) + } + ctx.EditMessage(chat.GetID(), &tg.MessagesEditMessageRequest{ + Message: text.String, + Entities: text.Entities, + ID: u.EffectiveMessage.ID, + }) + return dispatcher.EndGroups +} diff --git a/utils/update.go b/utils/update.go new file mode 100644 index 0000000..fd8d502 --- /dev/null +++ b/utils/update.go @@ -0,0 +1,82 @@ +package utils + +import ( + "encoding/json" + "errors" + "io/ioutil" + "strconv" + "strings" + + "github.com/anonyindian/logger" +) + +var CurrentUpdate = &Update{} +var currentVersion *version + +type Update struct { + Version string + Changes []string +} + +func InitUpdate(l *logger.Logger) { + b, err := ioutil.ReadFile("changelog.json") + if err != nil { + l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to open changelog.json: %s\n", err.Error()) + return + } + err = json.Unmarshal(b, CurrentUpdate) + if err != nil { + l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to parse changelog.json: %s\n", err.Error()) + return + } + currentVersion, err = parseVersion(CurrentUpdate.Version) + if err != nil { + l.ChangeLevel(logger.LevelCritical).Printlnf("Failed to parse current version: %s\n", err.Error()) + return + } +} + +func CheckChanges() (*Update, bool) { + var u Update + origin := "https://raw.githubusercontent.com/GigaUserbot/GIGA/dev/changelog.json" + json.Unmarshal([]byte(origin), &u) + return &u, CompareVersion(u.Version) +} + +// CompareVersion returns true if input version is greater than current one. +func CompareVersion(version string) bool { + parsedVersion, err := parseVersion(version) + if err != nil { + return false + } + return currentVersion.compare(parsedVersion) +} + +type version struct { + minor int + major int + patch int +} + +func parseVersion(s string) (*version, error) { + v := version{} + for index, velem := range strings.Split(s, ".") { + vint, err := strconv.Atoi(velem) + if err != nil { + return nil, errors.New("failed to parse version") + } + switch index { + case 0: + v.major = vint + case 1: + v.minor = vint + case 2: + v.patch = vint + } + } + return &v, nil +} + +func (v *version) compare(v1 *version) bool { + return (v1.major > v.major || v1.minor > v.minor || v1.patch > v.patch) +}