Skip to content

Commit

Permalink
Add cat avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi committed Sep 6, 2024
1 parent be6b495 commit a8d30b2
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/connector/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var AllCommands = []commands.CommandHandler{
MessagesCommand,
FileCommand,
CatCommand,
CatAvatarCommand,
}

var DummyHelpsection = commands.HelpSection{
Expand Down Expand Up @@ -325,3 +326,57 @@ var CatCommand = &commands.FullHandler{
Section: DummyHelpsection,
},
}

var CatAvatarCommand = &commands.FullHandler{
Func: func(e *commands.Event) {
e.Log.Debug().Msg("Searching for cat")

catDesc, err := searchCat(e.Ctx)
if err != nil {
e.Reply(err.Error())
return
}

mediaMime, mediaData, err := getCat(e.Ctx, catDesc.URL)
if err != nil {
e.Reply(err.Error())
return
}

mediaName := catDesc.ID
if strings.Contains(mediaMime, "png") {
mediaName = mediaName + ".png"
} else if strings.Contains(mediaMime, "jp") {
mediaName = mediaName + ".jpg"
} else if strings.Contains(mediaMime, "gif") {
mediaName = mediaName + ".gif"
} else if !strings.HasPrefix(mediaMime, "image/") {
e.Reply("Failed to get a cat: %s", mediaMime)
return
}

e.Log.Debug().Msg("Uploading cat")
url, _, err := e.Bot.UploadMedia(e.Ctx, e.RoomID, mediaData, mediaName, mediaMime)
if err != nil {
e.Reply(err.Error())
return
}

e.Log.Debug().Msg("Sending cat state")
content := event.Content{
Parsed: &event.RoomAvatarEventContent{
URL: url,
},
}
_, err = e.Bot.SendState(e.Ctx, e.RoomID, event.StateRoomAvatar, "", &content, time.Now())
if err != nil {
e.Reply(err.Error())
return
}
},
Name: "cat-avatar",
Help: commands.HelpMeta{
Description: "Make this room look like a cat",
Section: DummyHelpsection,
},
}

0 comments on commit a8d30b2

Please sign in to comment.