github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/commands/addemoji.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/keybase/client/go/chat/globals"
     8  	"github.com/keybase/client/go/protocol/chat1"
     9  	"github.com/keybase/client/go/protocol/gregor1"
    10  )
    11  
    12  type AddEmoji struct {
    13  	*baseCommand
    14  }
    15  
    16  func NewAddEmoji(g *globals.Context) *AddEmoji {
    17  	return &AddEmoji{
    18  		baseCommand: newBaseCommand(g, "addemoji", "<alias> <filename or url>", "Add a custom emoji", false),
    19  	}
    20  }
    21  
    22  func (h *AddEmoji) Execute(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
    23  	tlfName, text string, replyTo *chat1.MessageID) (err error) {
    24  	defer h.Trace(ctx, &err, "AddEmoji")()
    25  	if !h.Match(ctx, text) {
    26  		return ErrInvalidCommand
    27  	}
    28  	defer func() {
    29  		if err != nil {
    30  			err := h.getChatUI().ChatCommandStatus(ctx, convID, fmt.Sprintf("Failed to add emoji %v", err),
    31  				chat1.UICommandStatusDisplayTyp_ERROR, nil)
    32  			if err != nil {
    33  				h.Debug(ctx, "Execute: error with command status: %+v", err)
    34  			}
    35  		} else {
    36  			err := h.getChatUI().ChatCommandStatus(ctx, convID, "Emoji added successfully!",
    37  				chat1.UICommandStatusDisplayTyp_STATUS, nil)
    38  			if err != nil {
    39  				h.Debug(ctx, "Execute: error with command status: %+v", err)
    40  			}
    41  		}
    42  	}()
    43  	toks, err := h.tokenize(text, 3)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	_, err = h.G().EmojiSource.Add(ctx, uid, convID, toks[1], toks[2], false)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	_, err = h.G().ChatHelper.SendTextByIDNonblock(ctx, convID, tlfName, text, nil, replyTo)
    52  	return err
    53  }