alptangall commited on
Commit
d38953d
·
verified ·
1 Parent(s): 4fff1b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -0
app.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import os,sys
3
+ import re,json
4
+ import discord
5
+ from discord.ext import commands, tasks
6
+ from discord import app_commands
7
+ from discord.utils import get
8
+ import random
9
+ import datetime
10
+ import server
11
+ from guild import *
12
+ from acb import *
13
+ import aiohttp
14
+ import threading
15
+ import gradio as gr
16
+
17
+ USERNAME=os.environ.get('username')
18
+ PASSWORD=os.environ.get('password')
19
+
20
+ intents = discord.Intents.default()
21
+ client = discord.Client(intents=intents)
22
+ tree = app_commands.CommandTree(client)
23
+ VIETTELS='032, 033, 034, 035, 036, 037, 038, 039, 096, 097, 098, 086'
24
+ VIETTELS=VIETTELS.split(',')
25
+ VINAPHONES=' 091, 094, 088, 081, 082, 083, 084, 085'
26
+ VINAPHONES=VINAPHONES.split(',')
27
+ VIETNAMOBILE='052, 056, 058, 092'
28
+ VIETNAMOBILE=VIETNAMOBILE.split(',')
29
+ HEADERS = []
30
+ THREADS = []
31
+ USERNAMES = []
32
+ GUILDID = 1236962671824211998
33
+ RESULT=None
34
+
35
+ def correctSingleQuoteJSON(s):
36
+ rstr = ""
37
+ escaped = False
38
+
39
+ for c in s:
40
+
41
+ if c == "'" and not escaped:
42
+ c = '"' # replace single with double quote
43
+
44
+ elif c == "'" and escaped:
45
+ rstr = rstr[:-1] # remove escape character before single quotes
46
+
47
+ elif c == '"':
48
+ c = '\\' + c # escape existing double quotes
49
+
50
+ escaped = (c == "\\") # check for an escape character
51
+ rstr += c # append the correct json
52
+
53
+ return rstr
54
+ INFO=False
55
+ BASE_URL='https://bot-py-8t7nnjtaylrcjeqj8tevuh.streamlit.app/'
56
+ @client.event
57
+ async def on_ready():
58
+ global INFO,RESULT
59
+ try:
60
+ req=requests.get('http://localhost:8888')
61
+ print(req.status_code)
62
+ print('Client closed')
63
+ sys.exit('Exited')
64
+ except Exception as error:
65
+ print(error)
66
+ server.b()
67
+ guild = client.get_guild(GUILDID)
68
+ rs=await login(USERNAME,PASSWORD)
69
+ RESULT=await getBasic(guild)
70
+ if rs:
71
+ INFO=rs
72
+ if not getTransAcb.is_running():
73
+ getTransAcb.start(guild)
74
+
75
+
76
+
77
+ @client.event
78
+ async def on_disconnect():
79
+ global RESULT
80
+ if RESULT:
81
+ acbThread=None
82
+ for thread in RESULT['banksCh'].threads:
83
+ if 'acb' in thread.name:
84
+ acbThread=thread
85
+ if acbThread:
86
+ msgs=[msg async for msg in acbThread.history()]
87
+ if len(msgs)>0:
88
+ old=re.search('.*Sessions are `(.*?)`.*',msgs[0].content).group(1)
89
+ i=int(old)-1 if int(old)>0 else 0
90
+ await msgs[0].edit(content='Sessions are `'+str(i)+'` actived')
91
+
92
+ @tasks.loop(seconds=1)
93
+ async def getTransAcb(guild):
94
+ global INFO
95
+ print('getTransAcb is running')
96
+ if INFO:
97
+ try:
98
+ rs1=await getListAccount(INFO)
99
+ for item in rs1['list']:
100
+ list=await getBalance(INFO,item['accountNumber'])
101
+ basic=await getBasic(guild)
102
+ threads=basic['acbCh'].threads+[thread async for thread in basic['acbCh'].archived_threads()]
103
+ if list:
104
+ data=list['data'][::-1]
105
+ for item in data:
106
+ applied_tags=[]
107
+ if str(item['activeDatetime']) not in str(threads):
108
+ tags=basic['acbCh'].available_tags
109
+ st=''
110
+ if item['type'].lower()=='in':
111
+ for tag in tags:
112
+ if 'in' in tag.name.lower() or 'chuyển đến' in tag.name.lower():
113
+ applied_tags.append(tag)
114
+ else:
115
+ for tag in tags:
116
+ if 'out' in tag.name.lower() or 'chuyển đi' in tag.name.lower():
117
+ applied_tags.append(tag)
118
+ if 'bankName' in item and item['bankName']!='':
119
+ st+='\nĐến ngân hàng: **'+item['bankName']+'**\n'
120
+ if 'receiverAccountNumber' in item and item['receiverAccountNumber']!='':
121
+ st+='\nĐến số tài khoản: **'+item['receiverAccountNumber']+'**'
122
+ if 'receiverName' in item and item['receiverName']!='':
123
+ st+='\nĐến chủ tài khoản: **'+item['receiverName']+'**'
124
+ allowed_mentions = discord.AllowedMentions(everyone = True)
125
+ amount=str(item['amount']).split('.')[0]
126
+ amount=[f'{cur:,}' for cur in [int(amount)]][0]
127
+ time=datetime.datetime.fromtimestamp(item['activeDatetime']/1000)
128
+ day=time.day if time.day>9 else '0'+str(time.day)
129
+ month=time.month if time.month>9 else '0'+str(time.month)
130
+ hour=time.hour if time.hour>9 else '0'+str(time.hour)
131
+ minute=time.minute if time.minute>9 else '0'+str(time.minute)
132
+ second=time.second if time.second>9 else '0'+str(time.second)
133
+ timestr=f'{day}/{month}/{time.year} {hour}:{minute}:{second}'
134
+ thread=await basic['acbCh'].create_thread(name=('+ ' if item['type'].lower()=='in' else '- ')+amount+' '+item['currency']+'/ '+str(item['activeDatetime']),content='\nSố tiền: **'+amount+' '+item['currency']+'**\nNội dung: **'+item['description']+'**\nBiến động trên STK: **'+str(item['account'])+'**\nThời điểm: **'+timestr.split(' ')[1]+'** ngày **'+timestr.split(' ')[0]+'**'+st+'\n@everyone',applied_tags =applied_tags )
135
+ else:
136
+ rs=await login(USERNAME,PASSWORD)
137
+ if rs:
138
+ INFO=rs
139
+ except Exception as error:
140
+ print(error)
141
+ rs=await login(USERNAME,PASSWORD)
142
+ if rs:
143
+ INFO=rs
144
+
145
+
146
+ def run_bot():
147
+ #client.run(DISCORD_TOKEN)
148
+ client.run(os.environ.get('botToken'))
149
+
150
+
151
+ threading.Thread(target=run_bot).start()
152
+ """This allows us to run the Discord bot in a Python thread"""
153
+ with gr.Blocks() as demo:
154
+ gr.Markdown("""
155
+ # Huggingbots Server
156
+ This space hosts the huggingbots discord bot.
157
+ Currently supported models are Falcon and DeepfloydIF
158
+ """)
159
+ demo.queue(concurrency_count=100)
160
+ demo.queue(max_size=100)
161
+ demo.launch()