Skip to content

Commit

Permalink
add price arrows (#184)
Browse files Browse the repository at this point in the history
* feat: add floor color argument

This commit adds the `color` argument to the floor ticker.

* feat: change floor activity to watching

This commit uses the new UpdateWatchStatus of
bwmarrin/discordgo#1291 to set the activity to
watching instead of streaming.

* feat: add custom activity argument to floor ticker

This commit adds the `activity` argument to the floor ticker.

* feat: add floor colors argument

This commit adds the `colors` argument to the floor ticker.

* feat: feat: add floor ticker decorator argument

This commit adds the `decorator` argument to the floor ticker.

* feat: add ability to disable price arrows

This commit makes sure people can disable the price arrows by setting
the `decorator` argument to `" "`.

* build: remove dev dependency

* refactor: update code comments

* refactor: update code comments

* feat: make floor bot currency customizable

* fix: add_floor_colors merge conflict

* fix: fix decorator not applied bug

This commit makes sure the decorator is applied.

* Update marketcap.go

Co-authored-by: Riley Snyder <[email protected]>

* Update ticker.go

Co-authored-by: Riley Snyder <[email protected]>

* Update token.go

Co-authored-by: Riley Snyder <[email protected]>

---------

Co-authored-by: Riley Snyder <[email protected]>
  • Loading branch information
rickstaa and rssnyder authored Feb 13, 2023
1 parent 55e5c8c commit 04546a5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ Tracks stock or crypto prices. Uses Yahoo for stock or CoinGecko for crypto.
"ticker": "pfg", # string: symbol for the stock from yahoo finance
"name": "2) PFG", # string/OPTIONAL: overwrites display name of bot
"color": true, # bool/OPTIONAL: requires nickname
"decorator": "@", # string/OPTIONAL: what to show instead of arrows
"decorator": "@", # string/OPTIONAL: what to show instead of arrows. Set to " " to disable arrows.
"currency": "aud", # string/OPTIONAL: alternative curreny
"activity": "Hello;Its;Me", # string/OPTIONAL: list of strings to show in activity section
"nickname": true, # bool/OPTIONAL: display information in nickname vs activity
Expand Down Expand Up @@ -412,7 +412,7 @@ Tracks the marketcap of a coin. Uses CoinGecko for data.
"name": "bitcoin", # string: name of the crypto from coingecko
"ticker": "1) BTC", # string/OPTIONAL: overwrites display name of bot
"color": true, # bool/OPTIONAL: requires nickname
"decorator": "@", # string/OPTIONAL: what to show instead of arrows
"decorator": "@", # string/OPTIONAL: what to show instead of arrows. Set to " " to disable arrows.
"currency": "aud", # string/OPTIONAL: alternative curreny
"currency_symbol": "AUD", # string/OPTIONAL: alternative curreny symbol
"activity": "Hello;Its;Me", # string/OPTIONAL: list of strings to show in activity section
Expand Down Expand Up @@ -528,7 +528,7 @@ Track the price of a token on Ethereum, Binance, or Polygon chains. Uses 1inch b
"currency": "0x00000", # string/OPTIONAL: contract address of token to price against, default is USDC
"nickname": true, # bool/OPTIONAL: display information in nickname vs activity
"color": true, # bool/OPTIONAL: requires nickname
"decorator": "@", # string/OPTIONAL: what to show instead of arrows
"decorator": "@", # string/OPTIONAL: what to show instead of arrows. Set to " " to disable arrows.
"activity": "Hello;Its;Me", # string/OPTIONAL: list of strings to show in activity section
"source": "pancakeswap", # string/OPTIONAL: if the token is a BSC token, you can set pancakeswap here to use it vs 1inch; you can also set dexlab for solana tokens
"frequency": 10, # int/OPTIONAL: seconds between refresh
Expand All @@ -551,6 +551,7 @@ Track the floor price of an NFT collection on OpenSea, Solanart, Solsea or Magic
"marketplace": "opensea", # string: one of: opensea, solanart, solsea or magiceden
"name": "ethereum", # string: collection name/id from source
"color": true, # bool/OPTIONAL: requires nickname
"decorator": "@", # string/OPTIONAL: what to show instead of arrows. Set to " " to disable arrows.
"currency": "MATIC", # string/OPTIONAL: alternative curreny
"nickname": true, # bool/OPTIONAL: display information in nickname vs activity
"activity": "Hello;Its;Me", # string/OPTIONAL: list of strings to show in activity section
Expand Down
31 changes: 24 additions & 7 deletions floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Floor struct {
Activity string `json:"activity"`
Frequency int `json:"frequency"`
Color bool `json:"color"`
Decorator string `json:"decorator"`
Currency string `json:"currency"`
ClientID string `json:"client_id"`
Token string `json:"discord_bot_token"`
Expand Down Expand Up @@ -76,6 +77,14 @@ func (f *Floor) watchFloorPrice() {
f.Frequency = 900
}

// Set arrows if no custom decorator
var arrows bool
if f.Decorator == "" {
arrows = true
} else if f.Decorator == " " { // Set to space to disable
f.Decorator = ""
}

// Grab custom activity messages
var custom_activity []string
itr := 0
Expand Down Expand Up @@ -116,20 +125,28 @@ func (f *Floor) watchFloorPrice() {
f.Currency = currency
}

// Convert price to string format.
if f.Currency == "ETH" {
priceString = fmt.Sprintf("Ξ%s", strconv.FormatFloat(price, 'f', -1, 64))
} else {
priceString = fmt.Sprintf("%s %s", strconv.FormatFloat(price, 'f', -1, 64), f.Currency)
}

// calculate if price has moved up or down
if price > oldPrice {
increase = true
} else if price < oldPrice {
increase = false
}

// Add arrows to price if requested
if arrows {
f.Decorator = "⬊"
if increase {
f.Decorator = "⬈"
}
}

// Convert price to string format.
if f.Currency == "ETH" {
priceString = fmt.Sprintf("%s Ξ%s", f.Decorator, strconv.FormatFloat(price, 'f', -1, 64))
} else {
priceString = fmt.Sprintf("%s %s %s", f.Decorator, strconv.FormatFloat(price, 'f', -1, 64), f.Currency)
}

// change nickname
if f.Nickname {

Expand Down
8 changes: 4 additions & 4 deletions floor_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (m *Manager) ImportFloor() {
defer m.Unlock()

// query
rows, err := m.DB.Query("SELECT id, clientID, token, nickname, activity, color, currency, marketplace, name, frequency FROM floors")
rows, err := m.DB.Query("SELECT id, clientID, token, nickname, activity, color, decorator, currency, marketplace, name, frequency FROM floors")
if err != nil {
logger.Warningf("Unable to query tokens in db: %s", err)
return
Expand All @@ -27,7 +27,7 @@ func (m *Manager) ImportFloor() {
var importedFloor Floor
var importedID int

err = rows.Scan(&importedID, &importedFloor.ClientID, &importedFloor.Token, &importedFloor.Nickname, &importedFloor.Activity, &importedFloor.Color, &importedFloor.Currency, &importedFloor.Marketplace, &importedFloor.Name, &importedFloor.Frequency)
err = rows.Scan(&importedID, &importedFloor.ClientID, &importedFloor.Token, &importedFloor.Nickname, &importedFloor.Activity, &importedFloor.Color, &importedFloor.Decorator, &importedFloor.Currency, &importedFloor.Marketplace, &importedFloor.Name, &importedFloor.Frequency)
if err != nil {
logger.Errorf("Unable to load token from db: %s", err)
continue
Expand Down Expand Up @@ -166,13 +166,13 @@ func (m *Manager) WatchFloor(floor *Floor) {
func (m *Manager) StoreFloor(floor *Floor) {

// store new entry in db
stmt, err := m.DB.Prepare("INSERT INTO floors(clientId, token, nickname, activity, color, currency, marketplace, name, frequency) values(?,?,?,?,?,?,?,?,?)")
stmt, err := m.DB.Prepare("INSERT INTO floors(clientId, token, nickname, activity, color, decorator, currency, marketplace, name, frequency) values(?,?,?,?,?,?,?,?,?,?)")
if err != nil {
logger.Warningf("Unable to store floor in db %s: %s", floor.label(), err)
return
}

res, err := stmt.Exec(floor.ClientID, floor.Token, floor.Nickname, floor.Activity, floor.Color, floor.Currency, floor.Marketplace, floor.Name, floor.Frequency)
res, err := stmt.Exec(floor.ClientID, floor.Token, floor.Nickname, floor.Activity, floor.Color, floor.Decorator, floor.Currency, floor.Marketplace, floor.Name, floor.Frequency)
if err != nil {
logger.Warningf("Unable to store floor in db %s: %s", floor.label(), err)
return
Expand Down
13 changes: 13 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,19 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add floor decorator
_, err = db.Exec("alter table floors add column decorator default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: decorator (1)")
} else if err.Error() == "SQL logic error: duplicate column name: decorator (1)" {
logger.Debug("New column already exists in floors: decorator (1)")
} else if err != nil {
logger.Errorln(err)
logger.Warning("Will not be storing state.")
var dbNull *sql.DB
return dbNull
}

// v3.11.0 - add floor currency
_, err = db.Exec("alter table floors add column currency default \"\";")
if err == nil {
Expand Down
2 changes: 2 additions & 0 deletions marketcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (m *MarketCap) watchMarketCap() {
var arrows bool
if m.Decorator == "" {
arrows = true
} else if m.Decorator == " " {
m.Decorator = ""
}

// Grab custom activity messages
Expand Down
2 changes: 2 additions & 0 deletions ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func (s *Ticker) watchStockPrice() {
var arrows bool
if s.Decorator == "" {
arrows = true
} else if s.Decorator == " " {
s.Decorator = ""
}

// Grab custom activity messages
Expand Down
2 changes: 2 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (t *Token) watchTokenPrice() {
var arrows bool
if t.Decorator == "" {
arrows = true
} else if t.Decorator == " " {
t.Decorator = ""
}

// Grab custom activity messages
Expand Down

0 comments on commit 04546a5

Please sign in to comment.