-
Notifications
You must be signed in to change notification settings - Fork 38
/
command.rb
121 lines (102 loc) · 3.02 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
require 'bitcoin-client'
require './bitcoin_client_extensions.rb'
class Command
attr_accessor :result, :action, :user_name, :icon_emoji
ACTIONS = %w(balance info deposit tip withdraw networkinfo)
def initialize(slack_params)
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 "such error no command wow"
end
end
def client
@client ||= Bitcoin::Client.local('dogecoin')
end
def balance
balance = client.getbalance(@user_id)
@result[:text] = "@#{@user_name} such balance #{balance}Ð"
if (balance > 0)
@result[:text] += " many coin"
elsif balance > 1000
@result[:text] += " very wealth!"
@result[:icon_emoji] = ":moneybag:"
end
end
def deposit
@result[:text] = "so deposit #{user_address(@user_id)} many address"
end
def tip
user = @params.shift
raise "pls say tip @username amount" unless user =~ /<@(U.+)>/
target_user = $1
set_amount
tx = client.sendfrom @user_id, user_address(target_user), @amount
@result[:text] = "such generous! <@#{@user_id}> => <@#{target_user}> #{@amount}Ð"
@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] += " (<http://dogechain.info/tx/#{tx}|such blockchain>)"
end
alias :":dogecoin:" :tip
def withdraw
address = @params.shift
set_amount
tx = client.sendfrom @user_id, address, @amount
@result[:text] = "such stingy <@#{@user_id}> => #{address} #{@amount}Ð (#{tx})"
@result[:icon_emoji] = ":shit:"
end
def networkinfo
info = client.getinfo
@result[:text] = info.to_s
@result[:icon_emoji] = ":bar_chart:"
end
private
def set_amount
@amount = @params.shift
randomize_amount if (@amount == "random")
@amount = @amount.to_i
raise "so poor not money many sorry" unless available_balance >= @amount + 1
raise "such stupid no purpose" if @amount < 10
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] = ":black_joker:"
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
end