-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
127 lines (101 loc) · 3.98 KB
/
main.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
import logging
from aiogram import Bot, Dispatcher, executor, types
from config import TOKEN, OWNER
from functions import edit_message
from models import BannedUsers, session
logging.basicConfig(level=logging.INFO)
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
"""
Greeting message for users and for
the owner!
:param message:
:return:
"""
if message.from_user.id != OWNER:
return await message.answer("Welcome to the support bot! You can post questions/suggestions here.")
else:
return await message.answer("Welcome boss. Enjoy using the bot!")
@dp.message_handler(commands=['mute'])
async def mute(message: types.Message):
"""
This feature mutes these users by
blacklisting a database that the bot
owner will flag.
:param message:
:return:
"""
if message.from_user.id == OWNER:
# get replied message
replied_message = message.reply_to_message.text
# get chat id
chat_id = int(replied_message[replied_message.index('[') + 1:replied_message.index(']')])
# get name of user
name_of_user = replied_message.split()[4]
# check user
banned_user = session.query(BannedUsers).filter(BannedUsers.telegram_id == chat_id).count()
if not banned_user:
chat_id = BannedUsers(telegram_id=chat_id)
session.add(chat_id)
session.commit()
return await message.answer(f"{name_of_user} has been blacklisted")
return await message.answer(f"{name_of_user} has already been banned")
return await message.answer('You have no right to this!')
@dp.message_handler(commands=['unmute'])
async def unmute(message: types.Message):
"""
This function unmutes these users by
removing the users from the blacklisted
database that the bot owner will mark.
:param message:
:return:
"""
if message.from_user.id == OWNER:
# get replied message
replied_message = message.reply_to_message.text
# get id who sent replied message
chat_id = int(replied_message[replied_message.index('[') + 1:replied_message.index(']')])
# get name of user
name_of_user = replied_message.split()[4]
# check user id
chat_id = session.query(BannedUsers).filter(BannedUsers.telegram_id == chat_id)
if chat_id.count():
# if user id in black list
chat_id.delete()
session.commit()
return await message.answer(f"{name_of_user} removed from blacklist")
# if user id is not in black list
return await message.answer(f'{name_of_user} is not in blacklist')
return await message.answer("You have no right to this!")
@dp.message_handler()
async def question_users(message: types.Message):
"""
This function forwards messages between
regular users and the bot owner.
:param message:
:return:
"""
# if user is owner
if message.from_user.id == OWNER:
# if message is not replied
if not message.reply_to_message:
return await message.answer("Your message must be a reply to a forwarded message!")
# if message is replied
# get replied message
replied_message = message.reply_to_message.text
# get id who sent replied message
chat_id = replied_message[replied_message.index('[') + 1:replied_message.index(']')]
return await bot.send_message(chat_id, message.text)
# if user isn't owner
user_id = message.from_user.id
# if user is not in black list
if not session.query(BannedUsers).filter(BannedUsers.telegram_id == user_id).count():
# edit message
edited_message = edit_message(message)
return await bot.send_message(OWNER, edited_message)
# if user is in black list
return await message.answer("You are blacklisted by this bot")
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)