-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk.py
197 lines (178 loc) · 5.62 KB
/
tk.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
# Python function
import datetime as dt
from datetime import datetime,tzinfo,timedelta
from datetime import time as stime#specific time
import time
import os
from random import randrange
from string import Template
from functools import wraps
from urllib import request
from telegram import Bot, Chat, Sticker, ReplyKeyboardMarkup
# do once var
do_once_value=True
# Record bot init time
init_time = datetime.now()
################################################
# tool kits #
################################################
# decorator
def do_once(function):
"""decorator for function do once"""
global do_once_value
if do_once_value:
do_once_value=False
return function
def c_tz(datetime,tz):
t=datetime+timedelta(hours=tz)#轉換時區 tz為加減小時
def is_admin(bot,update):
"""Dectect user if admin, return boolen value"""
is_admin=False
if update.message.chat.type=='private':
return is_admin
else:
adminlist=update.message.chat.get_administrators()
for i in adminlist:
if update.message.from_user.id==i.user.id:
is_admin=True
return is_admin
def bot_is_admin(bot,update):
"""Dectect bot if admin, return boolen value"""
bot_auth=False
if update.message.chat.type=='private':
return bot_auth
else:
adminlist=update.message.chat.get_administrators()
me=bot.get_me()
for b in adminlist:
if me.id==b.user.id:
bot_auth=True
return bot_auth
def room_member_num(bot,update):
# count member number
total_count=bot.get_chat_members_count(update.message.chat.id)
bot_count=2
human_count=total_count-bot_count
return human_count
# decorator
def del_cmd(func):
"""This decorator is used to prevent the command input before init"""
@wraps(func)
def wrapped(bot, update, *args, **kwargs):
"""Dectect bot if admin, if True, del cmd"""
if bot_is_admin(bot,update):
try:
bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)
except:
pass
return func(bot, update, *args, **kwargs)
return wrapped
def del_cmd_func(bot, update):
"""Dectect bot if admin, if True, del cmd"""
if bot_is_admin(bot,update):
try:
bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)
except:
pass
def randList(list):
return list[randrange(len(list))]
# decorator
def do_after_root(func):
"""This decorator is used to prevent the command input before init"""
@wraps(func)
def wrapped(bot, update, *args, **kwargs):
if update.message.date <= init_time:
return
return func(bot, update, *args, **kwargs)
return wrapped
# decorator
def admin_cmd(func):
@wraps(func)
def wrapped(bot, update, *args, **kwargs):
if not is_admin(bot,update):
try:
bot.send_message(chat_id=update.message.chat_id, text="Only admin can use this command.")
except:
bot.send_message(chat_id=update.callback_query.message.chat_id, text="Only admin can use this command.")
return
return func(bot, update, *args, **kwargs)
return wrapped
def bool2text(v):
if v:
return '開啟'
else:
return '關閉'
# DB function
def db_switch_one_value(data_tag):
dict={'tag': ''}
dict['tag']=data_tag
data_value = db.misaki_setting.find_one(dict)
if data_value['value']:
result = db.misaki_setting.update_one(
dict,
{'$set': {'value': False}},
upsert=True)
return False
else:
result = db.misaki_setting.update_one(
dict,
{'$set': {'value': True}},
upsert=True)
return True
def utc8now():
return (datetime.now()+timedelta(hours=8)).strftime("%y/%m/%d %H:%M:%S")
def utc8now_datetime():
return datetime.now()+timedelta(hours=8)
def if_int_negative(interval):
if interval<0:
return True
else:
return False
def formula(key,text,if_list=False,parameter_preword='-',sep_word=' '):
"""
To make function has add function.
Ex. If /funcion -h
Help funcion will return true.
Or
Ex. /funcion -f=123
will return '123'
this funcion support many formula
if user input /funcion -f=123 -d -w=abc
formula('f',text) will return '123'
"""
# key is what you want to dectect
key_len=len(key)
key_total=parameter_preword+key
key_position=text.find(key_total)
value=False
if key_position>=0:
start_point=key_position+key_len+1
if start_point>(len(text)-1):
value=True
else:
try:
if text[start_point] == '=':
start_point+=1
return_value=""
while(start_point<len(text)):
if text[start_point]==sep_word:
break
return_value+=text[start_point]
start_point+=1
value=return_value
if if_list:
"""input data like -data=a,b,c"""
value=[]
for i in return_value.split(','):
value.append(i)
else:
value=True
finally:
pass
return value
def url_valid(url):
try:
code = request.urlopen(url).code
except:
return False
return True