Skip to content

Commit

Permalink
fix some bugs and pre-commit for updater
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 11, 2022
1 parent c1195be commit cec5ba2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ func StartClient(l *logger.Logger, b *gotgbot.Bot) {
},
},
})
log.Println("STARTED")
log.ChangeLevel(logger.LevelMain).Println("STARTED")
}
2 changes: 1 addition & 1 deletion changelog.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"Version": "0.0.0",
"Changes": []
"Changes": [""]
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ 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))
}
if config.DEBUG {
l.ChangeMinimumLevel(logger.LevelDebug)
}
utils.InitUpdate(l)
config.Load(l)
handlers.DefaultPrefix = []rune{'.', '$'}
db.Load(l)
Expand Down
40 changes: 40 additions & 0 deletions modules/update.go
Original file line number Diff line number Diff line change
@@ -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
}
82 changes: 82 additions & 0 deletions utils/update.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit cec5ba2

Please sign in to comment.