Top users
This commit is contained in:
		| @ -1,6 +1,7 @@ | ||||
| package main | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| @ -87,3 +88,24 @@ func handleKarma(u *nechotron.Update) error { | ||||
| 		fmt.Sprintf("*%s \\(%d\\)* только что %s карму *%s \\(%d\\)*", mentionFrom, totalFromKarma, changeText, mentionTo, totalToKarma)) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| var topText = ` | ||||
| *Наш ТОП\-10 пользователей:* | ||||
|  | ||||
| %s | ||||
| ` | ||||
|  | ||||
| func handleTop(u *nechotron.Update, _ string) error { | ||||
| 	store := tongo.NewStore[RatedUser](db) | ||||
| 	users, err := store.GetMany(u.Ctx) | ||||
| 	if err != nil { | ||||
| 		return errors.Join(errors.New("can't get top10 users"), err) | ||||
| 	} | ||||
| 	text := "" | ||||
| 	for n, u := range users { | ||||
| 		text += fmt.Sprintf("%d \\- %s \\(%d\\)\n", n, nechotron.EscapeMd2(u.Name), u.TotalKarma) | ||||
| 	} | ||||
| 	text = fmt.Sprintf(topText, text) | ||||
| 	u.AnswerMarkdown(text) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @ -17,7 +17,8 @@ var MainState = neco.State{ | ||||
|  | ||||
| 		mainCommands := neco.NewDispatcher(). | ||||
| 			HandleCommand(commandMe, handleMe). | ||||
| 			HandleCommand(commandHelp, handleHelp) | ||||
| 			HandleCommand(commandHelp, handleHelp). | ||||
| 			HandleCommand(commandTop, handleTop) | ||||
|  | ||||
| 		replyDispatcher := neco.NewDispatcher(). | ||||
| 			HandleFilter(karmaTriggers, handleKarma) | ||||
|  | ||||
							
								
								
									
										9
									
								
								types.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								types.go
									
									
									
									
									
								
							| @ -52,6 +52,15 @@ var userIndex = mongo.IndexModel{ | ||||
|  | ||||
| func (User) Coll() string { return "users" } | ||||
|  | ||||
| type RatedUser struct { | ||||
| 	tongo.Item `bson:",inline"` | ||||
| 	Name       string | ||||
| 	ID         int64 | ||||
| 	TotalKarma int64 `bson:"totalKarma"` | ||||
| } | ||||
|  | ||||
| func (RatedUser) Coll() string { return "top10" } | ||||
|  | ||||
| type Chat struct { | ||||
| 	tongo.Item `bson:",inline"` | ||||
| 	ID         int64 | ||||
|  | ||||
		Reference in New Issue
	
	Block a user