From 036e4dcbadf9a31e384d25cb64dd049ed85b85b7 Mon Sep 17 00:00:00 2001 From: nefrace Date: Tue, 24 Jan 2023 00:37:27 +0300 Subject: [PATCH] Test middleware --- commands.go | 20 +++++++++++--------- main.go | 7 +++++-- middleware.go | 15 +++++++++++++++ types.go | 25 +++++++++++++++++++++---- 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 middleware.go diff --git a/commands.go b/commands.go index f54bc95..3820178 100644 --- a/commands.go +++ b/commands.go @@ -3,26 +3,28 @@ package main import ( "fmt" - "git.nefrace.ru/nefrace/nechotron" + neco "git.nefrace.ru/nefrace/nechotron" ) -var commandMe = nechotron.NewCommand("me", false) +var commandMe = neco.NewCommand("me", "Пишу ваш текст о вас в третьем лице", false) -func handleMe(u *nechotron.UpdateCommand) error { - _, err := u.AnswerMarkdown(fmt.Sprintf("_*%s* %s_", nechotron.EscapeMd2(u.From().FirstName), nechotron.EscapeMd2(u.Param))) +func handleMe(u *neco.UpdateCommand) error { + _, err := u.AnswerMarkdown(fmt.Sprintf("_*%s* %s_", neco.EscapeMd2(u.From().FirstName), neco.EscapeMd2(u.Param))) return err } -var commandHelp = nechotron.NewCommand("help", false) +var commandHelp = neco.NewCommand("help", "Показываю данный текст", false) -func handleHelp(u *nechotron.UpdateCommand) error { +func handleHelp(u *neco.UpdateCommand) error { _, err := u.AnswerPlain("Тут текст помощи") return err } -var commandSay = nechotron.NewCommand("say", true) +var commandSay = neco.NewCommand("say", "Пишу ваш текст от своего имени.", true) -func handleSay(u *nechotron.UpdateCommand) error { - _, err := u.AnswerMarkdown(fmt.Sprintf("*_%s_*", nechotron.EscapeMd2(u.Param))) +func handleSay(u *neco.UpdateCommand) error { + _, err := u.AnswerMarkdown(fmt.Sprintf("*_%s_*", neco.EscapeMd2(u.Param))) return err } + +var commandWarn = neco.NewCommand("warn", "Делаю предупреждение пользователю", true) diff --git a/main.go b/main.go index 353af66..fca9590 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,11 @@ import ( "git.nefrace.ru/nefrace/nechotron" ) +var BuildTime string + func main() { - neko := nechotron.NewTron("1698825178:AAHy4Er7z768CmYy9M5drJOMQO2S-QnwdTI", &MainState) + neco := nechotron.NewTron("1698825178:AAHy4Er7z768CmYy9M5drJOMQO2S-QnwdTI", &MainState) + neco.Use(UserLogger) // neko.ApiServer = "http://192.168.100.15:7373" - log.Fatal(neko.DispatchPoll()) + log.Fatal(neco.DispatchPoll()) } diff --git a/middleware.go b/middleware.go new file mode 100644 index 0000000..7b9382c --- /dev/null +++ b/middleware.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + + "git.nefrace.ru/nefrace/nechotron" +) + +func UserLogger(next nechotron.UpdateHandler) nechotron.UpdateHandler { + return func(u *nechotron.Update) error { + log.Println(u.From().FirstName) + next(u) + return nil + } +} diff --git a/types.go b/types.go index d713598..c2986c6 100644 --- a/types.go +++ b/types.go @@ -1,14 +1,31 @@ package main -import "git.nefrace.ru/nefrace/tongo" +import ( + "time" + + "git.nefrace.ru/nefrace/tongo" +) var _ tongo.Collectable = &User{} type User struct { tongo.Item - Username string - FirstName string - ID int64 + Username string + FirstName string + ID int64 + KarmaOffser int } func (User) Coll() string { return "users" } + +var _ tongo.Collectable = &KarmaShot{} + +type KarmaShot struct { + tongo.Item + From tongo.OID + To tongo.OID + Count int + When time.Time +} + +func (KarmaShot) Coll() string { return "karma" }