-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.py
240 lines (205 loc) · 8.19 KB
/
app.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
from discord.ext import commands, tasks
import requests
import json
### env ###
TOKEN = ""
query_time = 250
global min_status
min_status = 70
Bot = commands.Bot("$")
file_json = "data.json"
api_data = []
txhash_data = []
txhash_true_data = []
### functions ###
## register ##
def save(user_id, user_name, user_tag, API, channel_id):
if API == None:
API = None
else:
API = API[1:]
try:
file = json.load(open(file_json, "r", encoding="UTF-8"))
except:
file = []
i = 0
for user in file:
if int(user["id"]) == int(user_id):
while True:
try:
jsonFile = open(file_json, "r", encoding="utf-8")
break
except:
pass
data = json.load(jsonFile)
jsonFile.close()
data[i]["api"] = API
data[i]["channel_id"] = channel_id
with open(file_json, mode='w', encoding="utf-8") as f:
f.write(json.dumps(data, indent=2,ensure_ascii=False))
return
i += 1
information = {
'id': user_id,
'username': user_name,
'usertag': user_tag,
'api': API,
'channel_id': channel_id
}
a = []
if not os.path.isfile(file_json):
a.append(information)
with open(file_json, mode='w', encoding="utf-8") as f:
f.write(json.dumps(a, indent=2,ensure_ascii=False))
else:
with open(file_json, encoding="utf-8") as feedsjson:
feeds = json.load(feedsjson)
feeds.append(information)
with open(file_json, mode='w', encoding="utf-8") as f:
f.write(json.dumps(feeds, indent=2,ensure_ascii=False))
if API in api_data:
pass
else:
api_data.append(API)
## query ##
def query(api):
for data in response_json:
if data["voter"] == api:
if data["vote"] == False:
return [False, data]
else:
return [True, data]
return [None, None]
## Bot auth ##
@Bot.event
async def on_ready():
check_control_data.start()
## query loop ##
@tasks.loop(seconds=query_time)
async def check_control_data():
vote_true = 0
vote_false = 0
try:
file = json.load(open(file_json, "r", encoding="UTF-8"))
global response_json
try:
response_json = json.loads(requests.get("https://testnet.api.axelarscan.io/evm-votes").text)["data"]
# response_json = json.load(open("test.json", "r", encoding="UTF-8"))["data"]
except:
response_json = []
for data in response_json:
if data["vote"] == True:
vote_true += 1
elif data["vote"] == False:
vote_false += 1
except:
file = []
message_list = []
message_list_true = []
for user in file:
channel_id = user["channel_id"]
api = user["api"]
if api == None or len(api) < 3:
pass
else:
answer_query = query(api)
answer = answer_query[1]
if answer_query[0] == False:
if answer["txhash"] in txhash_data:
pass
else:
sender_chain = answer["sender_chain"]
txhash = answer["txhash"]
data2 = []
for user2 in file:
if user2["api"] == api:
data2.append(f'<@{user2["id"]}>')
text_id = ""
for user_data_id in data2:
text_id = text_id + " " + user_data_id
message = f"Hey {text_id}, voted ***NO*** for {sender_chain}!\nTx Hash: `{txhash}`"
message_list.append([message, channel_id])
txhash_data.append(txhash)
elif answer_query[0] == True:
if answer["txhash"] in txhash_true_data:
pass
else:
txhash = answer["txhash"]
data3 = []
for user3 in file:
if user3["api"] == api:
data3.append(f'<@{user3["id"]}>')
text_id = ""
for user_data_id in data3:
text_id = text_id + " " + user_data_id
message = f"Voting result is incompatible with the majority. You may need to check. {text_id}"
message_list_true.append([message, channel_id])
txhash_true_data.append(txhash)
if int(min_status * (vote_true + vote_false) / 100) > vote_false:
for message_i in message_list:
await Bot.get_channel(int(message_i[1])).send(message_i[0])
else:
for message_i in message_list_true:
await Bot.get_channel(int(message_i[1])).send(message_i[0])
## register app ##
@Bot.event
async def on_message(message):
msg = await Bot.get_channel(int(message.channel.id)).fetch_message(int(message.id))
msg_list = msg.content.split(" ")
if msg_list[0].lower() == "$ping":
await Bot.get_channel(int(message.channel.id)).send(f"Pong!({Bot.latency * 1000}ms)")
elif msg_list[0].lower() == "$help":
await Bot.get_channel(int(message.channel.id)).send(f"🧾 Set the voter address```bash\n You can register as $VOTERADDRESS or $VOTERADDRESS @username @username ... ```\n🔕 Delete your voter address```bash\n If you want to delete: $delete or $delete @username @username ... ```\n```bash\n For MS: $ping ```\n```bash\n For help: $help```")
elif msg_list[0].lower() == "$min":
try:
new_min_status = int(msg_list[1])
global min_status
min_status = new_min_status
txhash_data.clear()
txhash_true_data.clear()
await Bot.get_channel(int(message.channel.id)).send(f"Successful & Min: {str(min_status)}")
except:
await Bot.get_channel(int(message.channel.id)).send(f"Please enter number.")
elif msg_list[0][0] == "$":
if msg_list[0][:7] == "$delete":
API = None
else:
API = msg_list[0]
save(msg.author.id, msg.author.name, msg.author.discriminator, API, int(message.channel.id))
if len(msg_list) != 1:
a = 0
users = []
for msg_text in msg_list:
if a == 0:
a += 1
else:
if len(msg_text) < 5:
pass
else:
if msg_text[:2] == "<@":
users.append(msg_text.replace("<@","").replace(">","").replace(" ", ""))
else:
await Bot.get_channel(int(message.channel.id)).send(f"Failed {msg_text}")
data3 = []
data3.append(f'<@{msg.author.id}>')
for user in users:
user_data = await Bot.fetch_user(user)
user_id = user
user_name, user_tag = str(user_data).split("#")
save(user_id, user_name, user_tag, API, int(message.channel.id))
data3.append(f'<@{user_id}>')
text_id = ""
for user_data_id in data3:
text_id = text_id + " " + user_data_id
if len(users) > 0:
if API == None:
await Bot.get_channel(int(message.channel.id)).send(f"Hey {text_id}, successfully deleted.")
else:
await Bot.get_channel(int(message.channel.id)).send(f"Hey {text_id}, successfully saved.")
else:
if API == None:
await Bot.get_channel(int(message.channel.id)).send(f"Hey <@{msg.author.id}>, successfully deleted.")
else:
await Bot.get_channel(int(message.channel.id)).send(f"Hey <@{msg.author.id}>, successfully saved.")
Bot.run(TOKEN)