twitch-user.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "time"
  6. tirc "github.com/gempir/go-twitch-irc/v4"
  7. "github.com/zveinn/twitch-bot/mongowrapper"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. )
  12. type User struct {
  13. Points int `json:"Points" bson:"Points"`
  14. ID string `json:"ID" bson:"ID"`
  15. Name string `json:"Name" bson:"Name"`
  16. DisplayName string `json:"DisplayName" bson:"DisplayName"`
  17. Color string `json:"Color" bson:"Color"`
  18. Badges map[string]int `json:"Badges" bson:"Badges"`
  19. }
  20. type UserMSG struct {
  21. Raw string `bson:"Raw"`
  22. Type tirc.MessageType `bson:"Type"`
  23. RawType string `bson:"RawType"`
  24. Tags map[string]string `bson:"Tags"`
  25. Message string `bson:"Message"`
  26. Channel string `bson:"Channel"`
  27. RoomID string `bson:"RoomID"`
  28. ID string `bson:"ID"`
  29. Time time.Time `bson:"Time"`
  30. Emotes []*tirc.Emote `bson:"Emotes"`
  31. Bits int `bson:"Bits"`
  32. Action bool `bson:"Action"`
  33. FirstMessage bool `bson:"FirstMessage"`
  34. Reply *tirc.Reply `bson:"Reply"`
  35. CustomRewardID string `bson:"CustomRewardID"`
  36. }
  37. func GetTop10() (userList []*User) {
  38. opts := options.Find().SetSort(bson.D{{"Points", -1}}).SetLimit(11)
  39. ctx := context.Background()
  40. cursor, err := mongowrapper.UserCollection.Find(
  41. ctx,
  42. bson.D{},
  43. opts,
  44. )
  45. if err != nil {
  46. log.Println("Unable to decode top10", err)
  47. return
  48. }
  49. userList = make([]*User, 0)
  50. err = cursor.All(ctx, &userList)
  51. if err != nil {
  52. log.Println("Unable to decode top10", err)
  53. return
  54. }
  55. return
  56. }
  57. func IncrementUserPoints(user *User, points int) (err error) {
  58. opts := options.FindOneAndUpdate().SetUpsert(true)
  59. filter := bson.M{"uid": user.ID}
  60. ctx := context.Background()
  61. err = mongowrapper.UserCollection.FindOneAndUpdate(
  62. ctx,
  63. filter,
  64. bson.D{
  65. {"$inc", bson.D{{"Points", points}}},
  66. },
  67. opts,
  68. ).Err()
  69. if err != nil {
  70. log.Println("ERROR INCREMENTING USER STATS", err)
  71. }
  72. return
  73. }
  74. func FindUserMessagesFromMatch(user string, match string) (msgList []*tirc.PrivateMessage, err error) {
  75. opts := options.Find()
  76. filter := bson.D{
  77. {"message", primitive.Regex{Pattern: match, Options: ""}},
  78. {"user.name", user},
  79. }
  80. ctx := context.Background()
  81. cursor, err := mongowrapper.MSGCollection.Find(
  82. ctx,
  83. filter,
  84. opts,
  85. )
  86. if err != nil {
  87. log.Println("Error getting quote", err)
  88. return
  89. }
  90. msgList = make([]*tirc.PrivateMessage, 0)
  91. err = cursor.All(ctx, &msgList)
  92. if err != nil {
  93. log.Println("Error parsing quote:", err)
  94. return
  95. }
  96. return
  97. }
  98. func FindOrUpsertUser(user *tirc.User) (U *User, err error) {
  99. opts := options.FindOneAndUpdate().SetUpsert(true)
  100. filter := bson.M{"uid": user.ID}
  101. ctx := context.Background()
  102. U = new(User)
  103. err = mongowrapper.UserCollection.FindOneAndUpdate(
  104. ctx,
  105. filter,
  106. bson.D{
  107. {"$set", bson.D{{"lastSeen", time.Now().UnixNano()}}},
  108. {"$set", bson.D{{"ID", user.ID}}},
  109. {"$set", bson.D{{"Name", user.Name}}},
  110. {"$set", bson.D{{"DisplayName", user.DisplayName}}},
  111. {"$set", bson.D{{"Color", user.Color}}},
  112. {"$set", bson.D{{"Badges", user.Badges}}},
  113. },
  114. opts,
  115. ).Decode(&U)
  116. if err != nil {
  117. log.Println("ERROR FINDING USER:", err)
  118. return
  119. }
  120. return
  121. }
  122. func SaveMessage(msg *tirc.PrivateMessage) (err error) {
  123. ctx := context.Background()
  124. _, err = mongowrapper.MSGCollection.InsertOne(ctx, msg, options.InsertOne().SetBypassDocumentValidation(true))
  125. if err != nil {
  126. log.Println("ERROR INSERTING MSG")
  127. return
  128. }
  129. return
  130. }