-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdictionary.py
187 lines (156 loc) · 5.92 KB
/
dictionary.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
#Python file for maintaining the overall users
#and
#Python File for maintaining the blocked users
import telebot
import config
import dbhelper
import os
bot = telebot.TeleBot(config.token)
def create_list(file,list_name=None):
'Returns a new created file as list.'
if list_name == None:
list_name = list()
with open(file, 'a') as f:
f.write(str(list_name))
print(file, 'created!')
return list_name
def load_list(file):
'Returns a loaded file as a list.'
temp_list = list()
try:
with open(file, 'r', encoding='utf8') as f:
temp_list = eval(f.read())
return temp_list
except FileNotFoundError:
return create_list(file)
def save_list(file, list_name):
'Saves a list into a file.'
try:
with open(file, 'w', encoding='utf8') as f:
f.write(str(list_name))
except FileNotFoundError:
create_list(file, list_name)
def add_new_user(file, list_name, person):
'Adds a new user to the memory and saves it into a file.'
if person not in list_name:
list_name.append(person)
save_list(file, list_name)
#After successful process sends "user is blocked" message to the admin
bot.send_message(config.my_id,"User is blocked!")
else:
#If the user id already exists in the list it prompts with "User is blocked already!"
bot.send_message(config.my_id,"User is blocked already!")
def remove_user_list(file, list_name, person):
'Removes the user from the memory and saves it into a file.'
try:
list_name.remove(person)
save_list(file, list_name)
#After successful process sends "user is unblocked!" message to the admin
bot.send_message(config.my_id,"User is unblocked!")
except ValueError:
#If the user id is not present in the list it prompts with "Oops! User is not blocked."
bot.send_message(config.my_id,"Oops! User is not blocked.")
return
def create_dict(file, dict_name=None):
'Returns a new created file as dictionary.'
if dict_name == None:
dict_name = dict()
with open(file, 'a') as f:
f.write(str(dict_name))
print(file, 'created!')
return dict_name
def load_dict(file):
'Returns a loaded file as a dictionary.'
temp_dict = dict()
try:
with open(file, 'r', encoding='utf8') as f:
temp_dict = eval(f.read())
return temp_dict
except FileNotFoundError:
return create_dict(file)
def save_dict(file, dict_name):
'Saves a dictionary into a file.'
try:
with open(file, 'w', encoding='utf8') as f:
f.write(str(dict_name))
except FileNotFoundError:
create_dict(file, dict_name)
def add_key_dict(file, dict_name, key, val):
'Adds a key and a value to the memory and saves it into a file.'
dict_name[key] = val
save_dict(file, dict_name)
def remove_key_dict(file, dict_name, key):
'Removes a key from the memory and saves it into a file.'
try:
del dict_name[key]
save_dict(file, dict_name)
except KeyError:
print('Key:', key, 'not found!')
return
#adds the username/nickname of the user in the block list
def add_blocklist(file, person):
user = [line.rstrip('\n') for line in open(file,'rt')]
if str(person).lower() not in user:
user.append(str(person).lower())
with open(file, 'a') as f:
f.write(str(person).lower()+"\n")
#rempves the username/nickname of the user from the block list
def remove_from_blocklist(file, person):
f = open(file,"r")
lines = f.readlines()
f.close()
f = open(file,"w")
for line in lines:
if line!=person.lower()+"\n":
f.write(line)
f.close()
if os.stat(config.storage_namelist).st_size == 0: #checks if the file is empty or not. If empty--> create a new dict() else--> load it
user_name = dict()
else:
user_name = load_dict(config.storage_namelist)
#step handler for adding the nickname as the key for the dictionary as well as storing them in namelist file
def process_name_step(message, dict_name, file, val, firstname):
try:
key = message.text
lower_key = key.lower()
add_key_dict(file, dict_name, lower_key, val)
bot.reply_to(message,"Thanks! " +"\n"+ "*Remember to block this user with the name:* "+"`'"+key+"'`"+" *only!*",parse_mode="Markdown" )
add_key_dict(config.storage_namelist, user_name, lower_key, firstname)
symfile = open(config.storage_fnamelist,'a')
for i in range(len(user_name)):
temp = list(user_name.popitem())
str1 = ': '.join(temp)
symfile.writelines(str1+'\n')
except Exception as e:
bot.reply_to(config.my_id, 'oooops')
#for setting the status of the admin
def set_status(file, status):
with open(file,"w") as text_file:
text_file.write(str(status).lower())
#for checking the status of the admin
def check_status(file):
with open(config.storage_availability) as f:
z = f.read()
if z == "unavailable":
return "false"
print (z)
else:
return "true"
print (z)
#for adding the message.chat.id's of the user in the file
def add_avaiblist(file, person):
user = [line.rstrip('\n') for line in open(file,'rt')]
if str(person) not in user:
user.append(str(person))
with open(file, 'a') as f:
f.write(str(person)+"\n")
#for setting the unavailable message for the admin
def unvb_msg(message, file=None):
try:
if file != None:
unvbmsg = message.text
with open(file,"w") as msg_file:
msg_file.write(str(unvbmsg))
bot.reply_to(message,"Thanks! " +"\n"+ "*The Message has been set successfully* ",parse_mode="Markdown" )
except Exception as e:
bot.reply_to(config.my_id, 'oooops! something went wrong')