forked from KipCrossing/Flux-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordibdd.py
170 lines (154 loc) · 6.74 KB
/
discordibdd.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
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
import os
from blockchain import Blockchain, Block
import discord
import asyncio
import ast
DATA_DIR = 'data'
NEW_DATA = 'new_data.txt'
NOTE_CONVERT = 'CONVERT'
NOTE_LOAD = 'LOAD'
NOTE_TRANSFER = 'TRANSFER'
class BlockchainManager(object):
def __init__(self, client, server_id, blockchain_channel_id):
self.client = client
self.server_id = server_id
self.blockchain_channel_id = blockchain_channel_id
self.blockchain = Blockchain()
async def update_blockchain(self):
print('Update blockchain')
await self.client.wait_until_ready()
server = self.client.get_guild(self.server_id)
blockchain_channel = self.client.get_channel(self.blockchain_channel_id)
print(self.client.is_closed())
while not self.client.is_closed():
if NEW_DATA in os.listdir(DATA_DIR):
f = open(DATA_DIR + '/' + NEW_DATA, 'r')
data = ''
for line in f:
data = line
f.close()
self.blockchain.mine(Block(data))
os.remove(DATA_DIR + '/' + NEW_DATA)
await blockchain_channel.send(self.blockchain.block)
await asyncio.sleep(5)
async def update_block(self, sender, receiver, amount, note):
s_bal = await self.get_balance(sender)
r_bal = await self.get_balance(receiver)
print(s_bal, r_bal, amount)
try:
sender_bal = s_bal - amount
receiver_bal = r_bal + amount
if sender_bal >= 0:
f = open(DATA_DIR + '/' + NEW_DATA, 'a+')
# [sender_user_id, receiver_id, transver_amount, sender_new_bal]
f.write('["{}","{}","{}","{}","{}","{}"],'.format(
sender, receiver, amount, sender_bal, receiver_bal, note))
f.close()
return(True)
else:
return(False)
except Exception as e:
return(False)
async def load_balance(self, receiver_id, amount):
server_bal = await self.get_balance(self.server_id)
f = open(DATA_DIR + '/' + NEW_DATA, 'a+')
# [sender_user_id, receiver_id, transver_amount, sender_new_bal]
f.write('["{}","{}","{}","{}","{}","{}"],'.format(
self.server_id, receiver_id, amount, server_bal, amount, NOTE_LOAD))
f.close()
async def get_balance(self, blockchain_id):
await self.client.wait_until_ready()
server = self.client.get_guild(self.server_id)
blockchain_channel = self.client.get_channel(self.blockchain_channel_id)
messages = []
counter = 0
current_bal = None
async for message in blockchain_channel.history(limit=None, oldest_first=False):
try:
data = message.content.split('\n')[2].replace('Block Data: ', '')[:-1]
data = '[' + data + ']'
data = ast.literal_eval(data)
should_break = False
for t in data:
if t[0] == str(blockchain_id):
# print(t)
# print('Current bal: {}'.format(t[3]))
current_bal = float(t[3])
should_break = True
elif t[1] == str(blockchain_id):
# print('id here')
current_bal = float(t[4])
should_break = True
if should_break:
break
except Exception as e:
print('Bad data: ', e)
current_bal = None
return(current_bal)
class IssueManager(object):
def __init__(self, bm, commands_channel_id):
self.bm = bm
self.client = bm.client
self.server_id = bm.server_id
self.blockchain_channel_id = bm.blockchain_channel_id
self.commands_channel_id = commands_channel_id
self.issue_in_session = False
async def get_issue(self, issue_id):
issue_data = issue_id.split('-')
server = self.client.get_guild(id=int(issue_data[0]))
channel = self.client.get_channel(int(issue_data[1]))
message = await channel.fetch_message(int(issue_data[2]))
return(message.content.replace('!IBDD ', '').replace('"', ''))
async def non_voters_transver(self):
await self.client.wait_until_ready()
server = self.client.get_guild(id=self.server_id)
channel = self.client.get_channel(self.blockchain_channel_id)
counter = 0
current_bal = None
print(self.issue_in_session)
for member in server.members:
print(member.id)
voted = False
async for message in channel.history(limit=None, oldest_first=False):
data = message.content.split('\n')[2].replace('Block Data: ', '')[:-1]
data = '[' + data + ']'
data = ast.literal_eval(data)
for t in data:
if t[0] == str(member.id) and t[1] == self.issue_in_session:
voted = True
if voted:
print(member.id, 'voted')
else:
await self.bm.update_block(member.id, self.issue_in_session, 0, NOTE_CONVERT)
async def get_converts(self):
server = self.client.get_guild(id=self.server_id)
channel = self.client.get_channel(self.blockchain_channel_id)
converts = 0
convert_ids = []
async for message in channel.history(limit=None, oldest_first=False):
data = message.content.split('\n')[2].replace('Block Data: ', '')[:-1]
data = '[' + data + ']'
data = ast.literal_eval(data)
for t in data:
if t[1] == self.issue_in_session and t[5] == NOTE_CONVERT:
converts += 1
convert_ids.append(t[0])
return(converts, convert_ids)
async def count_votes(self):
await self.non_voters_transver()
await asyncio.sleep(10) # change this for end vote check on BC
sum_traded_votes, convert_ids = await self.get_converts()
server_bal = await self.bm.get_balance(self.server_id)
vote_price = round(float(server_bal)/float(sum_traded_votes), 2)
for id in convert_ids:
await self.bm.update_block(self.server_id, id, vote_price, NOTE_TRANSFER)
print(sum_traded_votes, vote_price)
async def issue_timer(self):
server = self.client.get_guild(id=self.server_id)
channel = self.client.get_channel(self.commands_channel_id)
await asyncio.sleep(60*1)
await channel.send("**1 min** remaining...")
await asyncio.sleep(60*1)
await channel.send("*Vote finished*")
await self.count_votes()
self.issue_in_session = False