forked from deomaius/reddbot-slack
-
Notifications
You must be signed in to change notification settings - Fork 8
/
command.rb
213 lines (173 loc) · 6.68 KB
/
command.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/ruby
require 'bitcoin-client'
require 'net/http'
require 'sinatra'
require 'json'
require 'nokogiri'
require 'open-uri'
Dir['./coin_config/*.rb'].each {|file| require file }
require './bitcoin_client_extensions.rb'
class Command
attr_accessor :result, :action, :user_name, :icon_emoji , :channel
ACTIONS = %w(leaderboard balance chart deposit tip withdraw about price help hi commands stats)
def initialize(slack_params)
@coin_config_module = Kernel.const_get ENV['COIN'].capitalize
text = slack_params['text']
@params = text.split(/\s+/)
raise "NEBLIO IS THE BEST" unless @params.shift == slack_params['trigger_word']
@user_name = slack_params['user_name']
@user_id = slack_params['user_id']
@action = @params.shift
@result = {}
end
def perform
if ACTIONS.include?(@action)
self.send("#{@action}".to_sym)
else
raise @coin_config_module::PERFORM_ERROR
end
end
def client
@client ||= Bitcoin::Client.local
end
def balance
balance = client.getbalance(@user_id)
checkprice = JSON.parse(open("https://api.coinmarketcap.com/v2/ticker/1955/?convert=BTC").read)
priceusd = checkprice["data"]["quotes"]["USD"]["price"]
pricei = priceusd.to_i
x = ((balance*pricei.to_f).round(2)).to_s
@result[:text] = "<@#{@user_id}> #{@coin_config_module::BALANCE_REPLY_PRETEXT} #{balance.round(8)}#{@coin_config_module::CURRENCY_ICON} ≈ $#{x} "
if balance > @coin_config_module::WEALTHY_UPPER_BOUND
@result[:text] += @coin_config_module::WEALTHY_UPPER_BOUND_POSTTEXT
@result[:icon_emoji] = @coin_config_module::WEALTHY_UPPER_BOUND_EMOJI
elsif balance > 0 && balance < @coin_config_module::WEALTHY_UPPER_BOUND
@result[:text] += @coin_config_module::BALANCE_REPLY_POSTTEXT
end
end
def chart
@result[:attachments] = [{
title: "Neblio Price Chart",
title_link: "https://coinmarketcap.com/currencies/neblio/#charts",
color: "#0092ff",
footer: "https://coinmarketcap.com/",
footer_icon: "https://files.coinmarketcap.com/static/img/coins/16x16/neblio.png",
attachment_type: "default",
}]
end
def deposit
@result[:text] = "<@#{@user_id}> #{@coin_config_module::DEPOSIT_PRETEXT} #{user_address(@user_id)} #{@coin_config_module::DEPOSIT_POSTTEXT} :neblio:"
end
def tip
user = @params.shift
raise @coin_config_module::TIP_ERROR_TEXT unless user =~ /<@(U.+)>/
target_user = $1
set_amount
tx = client.sendfrom @user_id, user_address(target_user), @amount
@result[:text] = "#{@coin_config_module::TIP_PRETEXT} <@#{@user_id}> -> <@#{target_user}> #{@amount}#{@coin_config_module::CURRENCY_ICON}"
@result[:attachments] = [{
fallback:"<@#{@user_id}> -> <@#{target_user}> #{@amount}NEBL :neblio:",
color: "#ED1B24",
fields: [{
title: "Tipping initiated of #{@amount} NEBL :neblio:",
value: "http://explorer.nebl.io/tx/#{tx}",
short: false
},{
title: "Tipper",
value: "<@#{@user_id}>",
short: true
},{
title: "Recipient",
value: "<@#{target_user}>",
short: true
}]
}]
end
alias :":neblio:" :tip
def withdraw
address = @params.shift
set_amount
tx = client.sendfrom @user_id, address, @amount
@result[:text] = "#{@coin_config_module::WITHDRAW_TEXT} <@#{@user_id}> -> #{address} #{@amount}#{@coin_config_module::CURRENCY_ICON} "
@result[:text] += " (<#{@coin_config_module::TIP_POSTTEXT1}#{tx}#{@coin_config_module::TIP_POSTTEXT2}>)"
@result[:icon_emoji] = @coin_config_module::WITHDRAW_ICON
end
private
def set_amount
amount = @params.shift
@amount = amount.to_f
randomize_amount if (@amount == "random")
raise @coin_config_module::TOO_POOR_TEXT + @coin_config_module::FEE unless available_balance >= @amount + 0.0001
raise @coin_config_module::NO_PURPOSE_LOWER_BOUND_TEXT if @amount < @coin_config_module::NO_PURPOSE_LOWER_BOUND
end
def randomize_amount
lower = [1, @params.shift.to_f].min
upper = [@params.shift.to_f, available_balance].max
@amount = rand(lower..upper)
@result[:icon_emoji] = @coin_config_module::RANDOMIZED_EMOJI
end
def available_balance
client.getbalance(@user_id)
end
def user_address(user_id)
existing = client.getaddressesbyaccount(user_id)
if (existing.size > 0)
@address = existing.first
else
@address = client.getnewaddress(user_id)
end
end
def price
checkprice = JSON.parse(open("https://api.coinmarketcap.com/v2/ticker/1955/?convert=BTC").read)
priceusd = checkprice["data"]["quotes"]["USD"]["price"]
usdchange = checkprice["data"]["quotes"]["USD"]["percent_change_24h"]
pricebtc = checkprice["data"]["quotes"]["BTC"]["price"]
btcchange = checkprice["data"]["quotes"]["BTC"]["percent_change_24h"]
usdarrow = ""
btcarrow = ""
if usdchange.to_f > 0.00
usdarrow = " :arrow_up_small: "
elsif usdchange.to_f > 10
usdarrow = " :arrow_double_up: "
elsif usdchange.to_f < 0.00
usdarrow = " :arrow_down_small: "
elsif usdchange.to_f < -10
usdarrow = " :arrow_double_down: "
end
if btcchange.to_f > 0.00
btcarrow = " :arrow_up_small: "
elsif btcchange.to_f > 10
btcarrow = " :arrow_double_up: "
elsif btcchange.to_f < 0.00
btcarrow = " :arrow_down_small: "
elsif btcchange.to_f < -10
btcarrow = " :arrow_double_down: "
end
@result[:text] = "#{@coin_config_module::PRICE_PRE}$#{priceusd.round(2)} #{usdarrow}#{usdchange.round(2)}% - #{pricebtc.round(8)}BTC :bitcoin: #{btcarrow}#{btcchange.round(2)}%"
end
def leaderboard
end
def help
@result[:text] = @coin_config_module::HELP
end
def hi
@result[:text] = " #{@coin_config_module::HI} <@#{@user_id}> #{@coin_config_module::GREETING}"
end
def about
@result[:text] = "#{@coin_config_module::ABOUT}: #{@coin_config_module::ABOUT2} "
end
def commands
@result[:text] = "#{ACTIONS.join(', ' )}"
end
def stats
checkprice = JSON.parse(open("https://api.coinmarketcap.com/v2/ticker/1955/?convert=BTC").read)
priceusd = checkprice["data"]["quotes"]["USD"]["price"]
position = checkprice["data"]["rank"]
volume = checkprice["data"]["quotes"]["USD"]["volume_24h"]
marketcap = checkprice["data"]["quotes"]["USD"]["market_cap"]
mc = marketcap.to_s.split('.')
mc[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
vol = volume.to_s.split('.')
vol[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
@result[:text] = "NEBL :neblio: is $#{priceusd.round(2)}, rank #{position} with a Market Cap of $#{mc[0]} and a 24Hr Volume of $#{vol[0]}"
end
end