-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
211 lines (147 loc) · 7.82 KB
/
main.py
File metadata and controls
211 lines (147 loc) · 7.82 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from time import time
from balethon import Client
from balethon.conditions import regex, at_state, text, private, author
from balethon.objects import Message, CallbackQuery, User, ReplyKeyboardRemove
from balethon.dispatcher import MonitoringChain
from balethon.states import StateMachine
import config
import texts
import keyboards
from database import Database
from polls import Poll, QuizPoll
from chains import commands_chain, admins_chain, statistics_chain
from chains.commands_chain import help_
bot = Client(config.TOKEN)
incomplete_polls = {}
User.state_machine = StateMachine("user_states.db")
@bot.on_message(private & at_state(None) & regex("ایجاد نظرسنجی"))
async def create_poll(message: Message):
message.author.set_state("POLL_TYPE")
await message.reply(texts.select_poll_type, keyboards.poll_types)
@bot.on_message(private & at_state(None) & regex("نظرسنجی های من"))
async def my_polls(message: Message):
polls = Database.get_polls(message.author.id)
if not polls:
return await message.reply(texts.no_polls)
polls = texts.my_polls + "\n\n" + "\n\n".join(f"💠 * [{poll.type_name} - {poll.mode_name}) {poll.question}](send:/poll {poll.code}) *" for poll in polls)
await message.reply(polls)
@bot.on_message(private & at_state(None) & regex("راهنمایی"))
async def guide(client: Client, message: Message):
await help_(client=client, message=message)
@bot.on_message(private & at_state(None) & regex("پشتیبانی"))
async def support(message: Message):
await message.reply(texts.support)
@bot.on_message(private & at_state(None) & regex("تعرفه تبلیغات"))
async def ads(message: Message):
await message.reply_photo(config.ADS_FILE_ID, caption=texts.ads)
@bot.on_message(private & at_state(None) & regex("پنل ادمین ها") & author(*config.ADMINS))
async def admins_panel(message: Message):
message.author.set_state("ADMINS_PANEL")
await message.reply_document("users.db", texts.admins_panel, keyboards.admins_panel)
@bot.on_message(private & at_state("POLL_TYPE") & regex("نظرسنجی عادی"))
async def default_poll(message: Message):
incomplete_polls[message.author.id] = Poll.create_new("default_poll")
incomplete_polls[message.author.id].creator = message.author.id
message.author.set_state("POLL_MODE")
await message.reply(texts.select_poll_mode, keyboards.poll_modes)
@bot.on_message(private & at_state("POLL_TYPE") & regex("نظرسنجی چند جوابی"))
async def multiple_answers_poll(message: Message):
incomplete_polls[message.author.id] = Poll.create_new("multiple_answers_poll")
incomplete_polls[message.author.id].creator = message.author.id
message.author.set_state("POLL_MODE")
await message.reply(texts.select_poll_mode, keyboards.poll_modes)
@bot.on_message(private & at_state("POLL_TYPE") & regex("نظرسنجی آزمون"))
async def quiz_poll(message: Message):
incomplete_polls[message.author.id] = Poll.create_new("quiz_poll")
incomplete_polls[message.author.id].creator = message.author.id
message.author.set_state("POLL_MODE")
await message.reply(texts.select_poll_mode, keyboards.poll_modes)
@bot.on_message(private & at_state("POLL_MODE") & regex("عمومی"))
async def public_poll(message: Message):
incomplete_polls[message.author.id].is_anonymous = False
message.author.set_state("QUESTION")
await message.reply(texts.give_question, ReplyKeyboardRemove())
@bot.on_message(private & at_state("POLL_MODE") & regex("خصوصی"))
async def anonymous_poll(message: Message):
incomplete_polls[message.author.id].is_anonymous = True
message.author.set_state("QUESTION")
await message.reply(texts.give_question, ReplyKeyboardRemove())
@bot.on_message(private & at_state("QUESTION") & text)
async def question(message: Message):
poll = incomplete_polls[message.author.id]
if len(message.text) > 256:
return await message.reply(texts.question_too_long)
poll.question = " ".join(message.text.split())
message.author.set_state("OPTIONS")
await message.reply(texts.give_first_option)
@bot.on_message(private & at_state("OPTIONS") & text)
async def options(client: Client, message: Message):
poll = incomplete_polls[message.author.id]
if len(message.text) > 64:
return await message.reply(texts.option_too_long)
if (len(poll.options) >= 2 and message.text == "تکمیل نظرسنجی ☑️") or len(poll.options) >= 9:
if message.text != "تکمیل نظرسنجی ☑️":
poll.add_option(message.text)
if isinstance(poll, QuizPoll):
message.author.set_state("SELECTING_CORRECT_OPTION")
await message.reply(texts.select_correct_option, ReplyKeyboardRemove())
await message.reply("گزینه ها", poll.to_inline_keyboard("correct"))
return
poll.create_time = round(time())
Database.save_poll(poll)
await message.reply(str(poll), poll.to_inline_keyboard())
await client.send_message(message.chat.id, texts.command_usage)
await client.send_message(message.chat.id, f"/start {poll.code}")
await client.send_message(message.chat.id, texts.link_usage)
reply_markup = keyboards.admin_start if message.author.id in config.ADMINS else keyboards.start
await client.send_message(message.chat.id, f"https://ble.ir/VoterBot?start={poll.code}", reply_markup)
message.author.del_state()
return
if len(poll.options) >= 1:
poll.add_option(message.text)
options = "\n".join(f"• _{option.text}_" for option in poll.options)
await message.reply(texts.more_options.format(options=options), keyboards.complete_poll)
return
poll.add_option(message.text)
await message.reply(texts.give_second_option)
@bot.on_message(private & at_state("EXPLANATION"))
async def explanation(client: Client, message: Message):
poll = incomplete_polls[message.author.id]
if len(message.text) > 128:
return await message.reply(texts.explanation_too_long)
poll.explanation = message.text
poll.create_time = round(time())
Database.save_poll(poll)
await message.reply(str(poll), poll.to_inline_keyboard())
await client.send_message(message.chat.id, texts.command_usage)
await client.send_message(message.chat.id, f"/start {poll.code}")
await client.send_message(message.chat.id, texts.link_usage)
reply_markup = keyboards.admin_start if message.author.id in config.ADMINS else keyboards.start
await client.send_message(message.chat.id, f"https://ble.ir/VoterBot?start={poll.code}", reply_markup)
message.author.del_state()
@bot.on_callback_query(private & regex("^correct") & at_state("SELECTING_CORRECT_OPTION"))
async def correct(callback_query: CallbackQuery):
poll = incomplete_polls[callback_query.author.id]
_, __, option_index = callback_query.data.split(".")
option_index = int(option_index)
poll.correct_option = option_index
callback_query.author.set_state("EXPLANATION")
await callback_query.message.edit_text(texts.give_explanation)
@bot.on_callback_query(regex("^vote"))
async def vote(callback_query: CallbackQuery):
_, code, option_index = callback_query.data.split(".")
option_index = int(option_index)
poll = Database.load_poll(code)
poll.vote(callback_query.author.id, option_index)
Database.save_poll(poll)
await callback_query.message.edit_text(str(poll), poll.to_inline_keyboard())
@bot.on_callback_query(regex("^close"))
async def close(callback_query: CallbackQuery):
_, code = callback_query.data.split(".")
poll = Database.load_poll(code)
poll.is_closed = True
Database.save_poll(poll)
await callback_query.message.edit_text(poll.to_info())
if __name__ == "__main__":
bot.include(MonitoringChain(), commands_chain, admins_chain, statistics_chain)
bot.run()