forked from tenforwardconsulting/dogetip-slack
-
Notifications
You must be signed in to change notification settings - Fork 10
/
command.rb
129 lines (108 loc) · 3.91 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
require 'bitcoin-client'
Dir['./coin_config/*.rb'].each {|file| require file }
require './bitcoin_client_extensions.rb'
class Command
attr_accessor :result, :action, :user_name, :icon_emoji
ACTIONS = %w(balance deposit tip withdraw networkinfo commands)
def initialize(slack_params)
@coin_config_module = Kernel.const_get ENV['COIN'].capitalize
text = slack_params['text']
@params = text.split(/\s+/)
raise "WACK" 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)
@result[:text] = "@#{@user_name} #{@coin_config_module::BALANCE_REPLY_PRETEXT} #{balance}#{@coin_config_module::CURRENCY_ICON}"
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 deposit
@result[:text] = "#{@coin_config_module::DEPOSIT_PRETEXT} #{user_address(@user_id)} #{@coin_config_module::DEPOSIT_POSTTEXT}"
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}Ð",
color: "good",
fields: [{
title: "such tipping #{@amount}Ð wow!",
value: "http://dogechain.info/tx/#{tx}",
short: false
},{
title: "generous shibe",
value: "<@#{@user_id}>",
short: true
},{
title: "lucky shibe",
value: "<@#{target_user}>",
short: true
}]
}]
@result[:text] += " (<#{@coin_config_module::TIP_POSTTEXT1}#{tx}#{@coin_config_module::TIP_POSTTEXT2}>)"
end
alias :":dogecoin:" :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
def networkinfo
info = client.getinfo
@result[:text] = info.to_s
@result[:icon_emoji] = @coin_config_module::NETWORKINFO_ICON
end
private
def set_amount
amount = @params.shift
@amount = amount.to_i
randomize_amount if (@amount == "random")
raise @coin_config_module::TOO_POOR_TEXT unless available_balance >= @amount + 1
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_i].min
upper = [@params.shift.to_i, 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 commands
@result[:text] = "#{ACTIONS.join(', ' )}"
end
end