-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhooks.py
106 lines (81 loc) · 3.3 KB
/
webhooks.py
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
#######################################################################################################################
# Webhooks are no longer required. balances are kept in lnpay wallet for the user so no need to maintain in database. #
######################################################################################################################
from flask import Flask, request
from pymongo import MongoClient
import json
import helpers
app = Flask(__name__)
client = MongoClient("mongodb://localhost:27017")
db = client['btc-discord-bot']
@app.route('/', methods=['POST'])
def webhook():
request_json = json.loads(request.data.decode('utf-8').replace("'", '"'))
helpers.log(request)
helpers.log(request_json)
created_at = request_json['created_at']
id = request_json['id']
event = request_json['event']
data = request_json['data']['wtx']
passThru = data['passThru']
if passThru['app'] != 'discord-bot':
return
if data["wtxType"]["name"]=="ln_deposit":
txData = data["lnTx"]
txId = txData["id"]
isSettled = txData["settled"] # 1 for true
numSatsPaid = txData["num_satoshis"]
paymentRequest = txData["payment_request"]
if isSettled == 1:
server_id = str(passThru['server_id'])
server_name = str(passThru['server_name'])
depositor_id = str(passThru['depositor_id'])
depositor_name = str(passThru['depositor_name'])
helpers.log(f"\ndeposit confirmed by {depositor_id}: {numSatsPaid} sats")
check(server_id, server_name, depositor_id, depositor_name)
user = db['users'].find_one({'user_id': depositor_id})
user['balance'] += numSatsPaid
db['users'].update_one({'user_id': depositor_id}, {"$set": user})
'''
elif data["wtxType"]["name"]=="ln_withdrawal":
txData = data["lnTx"]
txId = txData["id"]
isSettled = txData["settled"] # 1 for true
numSatsPaid = txData["num_satoshis"]
paymentRequest = txData["payment_request"]
userName = data['passThru']['username']
if isSettled == 1:
server_id = str(passThru['server_id'])
server_name = str(passThru['server_name'])
withdrawer_id = str(passThru['withdrawer_id'])
withdrawer_name = str(passThru['withdrawer_name'])
print(f"\nwithdrawl confirmed by {withdrawer_id}: {numSatsPaid} sats")
check(server_id, server_name, withdrawer_id, withdrawer_name)
user = db['users'].find_one({'user_id': withdrawer_id})
user['balance'] -= numSatsPaid
db['users'].update_one({'user_id': withdrawer_id}, {"$set": user})
'''
return "success"
def check(server_id, server_name, member_id, member_name):
server_id = str(server_id)
member_id = str(member_id)
# check if server is new
servers_dbcol = db['servers']
if servers_dbcol.find_one({'server_id': server_id}) == None:
# insert server to collection
helpers.log(f"adding new server: {server_id}\n")
# data to be inserted
server = {'server_id': server_id, 'name': server_name}
# adds data to collection
servers_dbcol.insert_one(server)
# check if user is new
users_dbcol = db['users']
if users_dbcol.find_one({'user_id': member_id}) == None:
# insert user to collection
helpers.log(f"adding new user: {member_id}\n")
# data to be inserted
user = {'user_id': member_id, 'user_name': member_name, 'balance': 0, 'servers': [server_id]}
# insert user
users_dbcol.insert_one(user)
if __name__ == '__main__':
app.run(host='localhost', port=42069)