-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
171 lines (157 loc) · 8.23 KB
/
commands.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
import re
from controls import _is_admin
from telegram.error import *
def setctrl(bot,update,args):
print(args[0])
return
if _is_admin(bot,update):
global ban_time
global max_messages
message_id = update.message.message_id
try:
ctrl_option = re.search("\W\d+$", update.message.text)
time = ctrl_option.group(0).strip()
text_ = 'Filter set to *{0}* second(s) between messages'.format(time)
max_time = time #change time
bot.sendMessage(update.message.chat_id, text= text_, parse_mode='MARKDOWN', reply_to_message_id=message_id)
except (AttributeError, IndexError) as e:
bot.sendMessage(update.message.chat_id, text='Usage:\n`/setctrl time`',parse_mode='MARKDOWN', reply_to_message_id=message_id)
def setoption(bot,update,option,choices,bot_db):
if _is_admin(bot,update):
group_id = update.message.chat_id
message_id = update.message.message_id
try:
target = re.search("\/.* (\D+)$", update.message.text).group(1)
print (target)
#choices e' un array
if target in choices:
bot_db.setGroupOption(group_id, option, target)
bot.sendMessage(update.message.chat_id, text=option+" set to *"+target+"*",parse_mode='MARKDOWN', reply_to_message_id=message_id)
else:
raise AttributeError
except (AttributeError, IndexError) as e:
bot.sendMessage(update.message.chat_id, text='Usage:\n`/'+option+' '+' | '.join(choices)+'`',parse_mode='MARKDOWN', reply_to_message_id=message_id)
#WHITELIST COMMAND [SHOW | ADD | REMOVE]
def whitelist(bot,update,args,bot_db):
group_id = update.message.chat_id
if _is_admin(bot,update):
message_body = update.message
message_reply = message_body.reply_to_message
whitelist = bot_db.getGroupWhitelist(group_id)
message_id = message_body.message_id
try:
if args[0] == "show":
whitelist_users = ""
for elements in whitelist:
whitelist_users = whitelist_users+"\n"+elements['username']
if whitelist_users:
bot.sendMessage(group_id,text="Whitelist:*"+whitelist_users+"*" ,parse_mode='MARKDOWN')
else:
bot.sendMessage(group_id,text="Whitelist is empty")
return
user_name = message_reply.from_user.username
user_id = message_reply.from_user.id
_user={'username':user_name,'id':user_id}
if message_reply.new_chat_member is not None:
user_name = message_reply.new_chat_member.username
user_id = message_reply.new_chat_member.id
if re.search("\w*bot$",user_name) != None:
_user={'username':user_name,'id':user_id}
else:
bot.sendMessage(group_id,text="Only bots can be whitelisted in this way")
return
if args[0] == "add":
if _user not in whitelist:
#ADD USER TO WHITELIST
bot_db.addToWhitelist(group_id,_user)
bot.sendMessage(group_id, text="*"+user_name+"* added to whitelist",parse_mode='MARKDOWN', reply_to_message_id=message_id)
else:
bot.sendMessage(group_id, text="*"+user_name+"* already in whitelist",parse_mode='MARKDOWN', reply_to_message_id=message_id)
print(whitelist)
elif args[0] == "remove":
if _user in whitelist:
#REMOVE USER FROM WHITELIST
bot_db.removeFromWhitelist(group_id,_user)
print(whitelist)
bot.sendMessage(group_id, text="*"+user_name+"* removed from whitelist",parse_mode='MARKDOWN', reply_to_message_id=message_id)
else:
bot.sendMessage(group_id, text="User not in whitelist",parse_mode='MARKDOWN', reply_to_message_id=message_id)
else:
raise AttributeError
except (AttributeError, IndexError) as e:
print(e)
bot.sendMessage(group_id, text="Usage:\n `/whitelist add |remove| show`",parse_mode='MARKDOWN', reply_to_message_id=message_id)
def unban(bot,update,bot_db):
try:
group_id = update.message.chat_id
if _is_admin(bot,update):
message_body = update.message
message_reply = message_body.reply_to_message
user_name = message_reply.from_user.username
if user_name == "":
user_name = "N/D"
user_id = message_reply.from_user.id
user = bot_db.getUser(group_id,user_id)
_user={'username':user_name,'id':user_id}
if _user in bot_db.getGroupBanlist(group_id):
bot_db.removeBanned(group_id,_user)
user['counter'] = 0
bot.sendMessage(group_id,text="*"+user_name+"* Unbanned",parse_mode='MARKDOWN')
else:
bot.sendMessage(group_id,text="*"+user_name+"* Not in blacklist",parse_mode='MARKDOWN')
except AttributeError:
bot.sendMessage(group_id,text="Usage:\n `/unban quoting a message from the user you want to ban.`\n\nRemember:\n *you can't unban bots in that way* [Find Out why](https://core.telegram.org/bots/faq#why-doesn-39t-my-bot-see-messages-from-other-bots)",parse_mode='MARKDOWN')
def banlist(bot,update,bot_db):
if _is_admin(bot,update):
group_id = update.message.chat_id
banlist_users = ""
banlist = bot_db.getGroupBanlist(group_id)
print(banlist)
for elements in banlist:
banlist_users = banlist_users+"\n"+elements['username']
if banlist_users:
bot.sendMessage(group_id,text="Banlist:*"+banlist_users+"*" ,parse_mode='MARKDOWN')
else:
bot.sendMessage(group_id,text="Banlist is empty")
def ban(bot,update,bot_db, error):
if _is_admin(bot,update):
try:
group_id = update.message.chat_id
message_body = update.message
message_reply = message_body.reply_to_message
whitelist = bot_db.getGroupWhitelist(group_id)
banlist = bot_db.getGroupBanlist(group_id)
message_id = message_body.message_id
user_name = message_reply.from_user.username
print(user_name)
if user_name == "":
user_name = 'N/D'
user_id = message_reply.from_user.id
_role = bot.getChatMember(update.message.chat_id, message_reply.from_user.id)
if _role.status == "administrator" or _role.status == "creator":
bot.sendMessage(group_id,text="*You can't ban other admins stupid moron*",parse_mode='MARKDOWN')
return
_user={'username':user_name,'id':user_id}
if _user not in banlist:
bot_db.addBanned(group_id,_user)
bot.kickChatMember(group_id, user_id)
bot.sendMessage(group_id,text="*"+user_name+"* banned",parse_mode='MARKDOWN')
if _user in whitelist:
bot_db.removeFromWhitelist(group_id,_user)
bot.sendMessage(group_id,text="*"+user_name+"* removed from whtelist",parse_mode='MARKDOWN')
else:
bot.sendMessage(group_id,text="*"+user_name+"* already in banlist",parse_mode='MARKDOWN')
except AttributeError as e:
bot.sendMessage(group_id,text="Usage:\n `/ban quoting a message from the user you want to ban.`\n\nRemember:\n *you can't ban bots in that way* [Find Out why](https://core.telegram.org/bots/faq#why-doesn-39t-my-bot-see-messages-from-other-bots)",parse_mode='MARKDOWN')
except (TelegramError, BadRequest) as e:
bot.sendMessage(group_id, text="I need Administrator rights")
def delete(bot,update):
if _is_admin(bot,update):
group_id = update.message.chat_id
fwdmessage_id = update.message.reply_to_message.message_id
message_id = update.message.message_id
bot.deleteMessage(group_id,message_id)
bot.deleteMessage(group_id,fwdmessage_id)
#JUST FOR FUN
def pong(bot, update):
bot.sendMessage(update.message.chat_id, text="pong")