Skip to content

Commit

Permalink
add reaction adding
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Pavoni committed Apr 13, 2022
1 parent 65844be commit 3765b66
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
21 changes: 15 additions & 6 deletions ttfm/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,25 @@ func (a *Actions) ShowSongStats() {
}
}

func (a *Actions) ReloadFavRooms() {
a.bot.FavRooms.LoadFavRoomsFromDb()
func (a *Actions) ReloadFavRooms() error {
if err := a.bot.FavRooms.LoadFavRoomsFromDb(); err != nil {
return err
}
return nil
}

func (a *Actions) UpdateRoom(e ttapi.RoomInfoRes) {
a.bot.Room.Update(e)
func (a *Actions) UpdateRoom(e ttapi.RoomInfoRes) error {
if err := a.bot.Room.Update(e); err != nil {
return err
}
return nil
}

func (a *Actions) UpdateRoomFromApi() {
a.bot.Room.UpdateDataFromApi()
func (a *Actions) UpdateRoomFromApi() error {
if err := a.bot.Room.UpdateDataFromApi(); err != nil {
return err
}
return nil
}

func (a *Actions) EnforceSongDuration() {
Expand Down
12 changes: 10 additions & 2 deletions ttfm/commands/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func ReactionCommand() *ttfm.Command {
return &ttfm.Command{
AuthorizationRoles: []ttfm.UserRole{ttfm.UserRoleNone},
Help: "React with a gif. Without args shows available reactions",
Help: "React with a gif. Without args shows available reactions. Type `!r add <img url> <reaction name>` to add a reaction",
Handler: reactionCommandHandler,
}
}
Expand All @@ -23,7 +23,15 @@ func reactionCommandHandler(b *ttfm.Bot, cmd *ttfm.CommandInput) *ttfm.CommandOu
return &ttfm.CommandOutput{Msg: msg, User: user, ReplyType: cmd.Source}
}

if reaction := b.Reactions.Get(cmd.Args[0]); reaction != "" {
if len(cmd.Args) == 3 && cmd.Args[0] == "add" {
if err := b.Reactions.Put(cmd.Args[1], cmd.Args[2]); err != nil {
return &ttfm.CommandOutput{User: user, ReplyType: cmd.Source, Err: err}
}
msg := fmt.Sprintf("Added %s to `%s` reaction", cmd.Args[2], cmd.Args[1])
return &ttfm.CommandOutput{Msg: msg, User: user, ReplyType: cmd.Source}
}

if reaction := b.Reactions.Get(cmd.Args[0]); len(cmd.Args) == 1 && reaction != "" {
return &ttfm.CommandOutput{Msg: reaction, User: user, ReplyType: ttfm.MessageTypeRoom}
}

Expand Down
3 changes: 2 additions & 1 deletion ttfm/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ttfm

import (
"github.com/alaingilbert/ttapi"
"github.com/andreapavoni/ttfm_bot/utils"
"github.com/sirupsen/logrus"
)

Expand All @@ -14,7 +15,7 @@ func onReady(b *Bot) {
}

func onRoomChanged(b *Bot, e ttapi.RoomInfoRes) {
b.Actions.UpdateRoom(e)
utils.MaybeLogError("BOT:ROOM:UPDATE", func () error {return b.Actions.UpdateRoom(e)})
b.Actions.AutoBop()
b.Actions.ConsiderStartAutoDj()

Expand Down
5 changes: 2 additions & 3 deletions ttfm/reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (r *Reactions) Put(reactionName, imgUrl string) error {
imgs, ok := r.SmartMap.Get(reactionName)
if !ok {
r.SmartMap.Set(reactionName, []string{imgUrl})
return nil
}

if utils.IndexOf(imgUrl, imgs) >= 0 {
Expand All @@ -46,7 +45,7 @@ func (r *Reactions) Put(reactionName, imgUrl string) error {
}

func (r *Reactions) Get(reactionName string) string {
if imgs, ok := r.SmartMap.Get(reactionName); ok {
if imgs, ok := r.SmartMap.Get(reactionName); ok && len(imgs) > 0 {
i := utils.RandomInteger(0, len(imgs)-1)
return imgs[i]
}
Expand All @@ -63,7 +62,7 @@ func (r *Reactions) Availables() []string {

func (r *Reactions) Save() error {
reactions := []reaction{}
for i := range r.Iter() {
for i := range r.SmartMap.Iter() {
reactions = append(reactions, reaction{Name: i.Key, Imgs: i.Value})
}
return r.brain.Put("reactions", &reactions)
Expand Down
20 changes: 13 additions & 7 deletions ttfm/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ func (r *Room) Update(ri ttapi.RoomInfoRes) error {
r.bot.Users.Update(users)
r.UpdateModerators(ri.Room.Metadata.ModeratorID)
r.UpdateDjs(ri.Room.Metadata.Djs)
r.ResetDj()
if err := r.ResetDj(); err != nil {
return err
}

return nil
}

func (r *Room) ResetDj() {
func (r *Room) ResetDj() error {
d, ok := r.Djs.Get(r.CurrentSong.DjId)
if !ok {
panic("Can't reset current Dj")
return errors.New("Can't reset current Dj")
}
r.CurrentDj = d
return nil
}

func (r *Room) AddDj(id string) {
Expand Down Expand Up @@ -109,12 +112,15 @@ func (r *Room) HasModerator(userId string) bool {
return r.moderators.HasElement(userId)
}

func (r *Room) UpdateDataFromApi() {
if roomInfo, err := r.bot.api.RoomInfo(); err == nil {
r.Update(roomInfo)
func (r *Room) UpdateDataFromApi() error {
if roomInfo, err := r.bot.api.RoomInfo(); err != nil {
return err
} else {
panic(err)
if err := r.Update(roomInfo); err != nil {
return err
}
}
return nil
}

// SongStats
Expand Down
12 changes: 12 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/rand"
"os"
"time"

"github.com/sirupsen/logrus"
)

// ENVs
Expand All @@ -19,6 +21,9 @@ func GetEnvOrPanic(key string) string {

// Rand numbers
func RandomInteger(min, max int) int {
if min == max {
return min
}
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
Expand Down Expand Up @@ -63,3 +68,10 @@ func IndexOf[T comparable](value T, collection []T) int {
}
return -1
}

// Errors
func MaybeLogError(topic string, f func() error) {
if err := f(); err != nil {
logrus.WithFields(logrus.Fields{"err": err.Error()}).Error(topic)
}
}

0 comments on commit 3765b66

Please sign in to comment.