github.com/hungdoo/bot@v0.0.0-20240325145135-dd1f386f7b81/src/packages/command/bybitido/command.go (about)

     1  package bybitido
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/hungdoo/bot/src/common"
     9  	"github.com/hungdoo/bot/src/packages/bybitapi"
    10  	command "github.com/hungdoo/bot/src/packages/command/common"
    11  	"github.com/hungdoo/bot/src/packages/utils"
    12  )
    13  
    14  type IdoCommand struct {
    15  	command.Command
    16  	Id string `bson:"_id,unique"`
    17  }
    18  
    19  func (c IdoCommand) MarshalJSON() ([]byte, error) {
    20  	return json.Marshal(&struct {
    21  		Name     string `json:"name"`
    22  		Type     string `json:"type"`
    23  		IdleTime string `json:"idletime"`
    24  		Command  string `json:"command"`
    25  	}{
    26  		Name:     c.Name,
    27  		Type:     c.Type.String(),
    28  		IdleTime: c.IdleTime.String(),
    29  		Command:  fmt.Sprintf("add bybit %s", c.Name),
    30  	})
    31  }
    32  
    33  func (c *IdoCommand) Validate(data []string) error {
    34  	return nil
    35  }
    36  
    37  func (c *IdoCommand) SetData(newValue []string) (err error) {
    38  	return nil
    39  }
    40  
    41  func (c *IdoCommand) Execute(mustReport bool, subcommand string) (string, *common.ErrorWithSeverity) {
    42  	switch subcommand {
    43  	case "latest":
    44  		latest, err := bybitapi.GetLatestProject(bybitapi.IDO_URL)
    45  		if err != nil {
    46  			return "", common.NewErrorWithSeverity(common.Error, err.Error())
    47  		}
    48  		return fmt.Sprintf("Latest:\n%s", utils.PrettyPrint(latest)), nil
    49  	case "all":
    50  		a, err := bybitapi.FetchProjects(bybitapi.IDO_URL)
    51  		if err != nil {
    52  			return "", common.NewErrorWithSeverity(common.Error, err.Error())
    53  		}
    54  		return fmt.Sprintf("All:\n%s", utils.PrettyPrint(a)), nil
    55  	default:
    56  		p, err := bybitapi.GetUpcomingProjects(bybitapi.IDO_URL, time.Now())
    57  		if err != nil {
    58  			return "", common.NewErrorWithSeverity(common.Error, err.Error())
    59  		}
    60  		// Process the results
    61  		if len(p) != 0 {
    62  			return fmt.Sprintf("Upcoming:\n%s", utils.PrettyPrint(p)), nil
    63  		}
    64  		return "", nil
    65  	}
    66  }