/
botproperties.py
370 lines (311 loc) · 14.3 KB
/
botproperties.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
from telegram import ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, ParseMode
from telegram.error import BadRequest
import captions
import helpers
import mdformat
import settings
import util
from components import admin
from const import BotStates, CallbackActions
from custemoji import Emoji
from dialog import messages
from lib import InlineCallbackButton
from models import Bot, Country, Keyword, Statistic, Suggestion, User, track_activity
from util import restricted
CLEAR_QUERY = "x"
def _is_clear_query(query):
return query.lower() == CLEAR_QUERY
def set_country_menu(bot, update, to_edit):
uid = util.uid_from_update(update)
countries = Country.select().order_by(Country.name).execute()
buttons = util.build_menu(
[InlineKeyboardButton(
'{} {}'.format(c.emojized, c.name),
callback_data=util.callback_for_action(
CallbackActions.SET_COUNTRY, {'cid': c.id, 'bid': to_edit.id})) for c in countries
], 3)
buttons.insert(0, [
InlineKeyboardButton(captions.BACK,
callback_data=util.callback_for_action(CallbackActions.EDIT_BOT,
{'id': to_edit.id})),
InlineKeyboardButton("None",
callback_data=util.callback_for_action(CallbackActions.SET_COUNTRY,
{
'cid': 'None',
'bid': to_edit.id
})),
])
return bot.formatter.send_or_edit(uid, util.action_hint(
"Please select a country/language for {}".format(to_edit)),
to_edit=util.mid_from_update(update),
reply_markup=InlineKeyboardMarkup(buttons))
def set_country(bot, update, to_edit, country):
user = User.from_update(update)
if check_suggestion_limit(bot, update, user):
return
if isinstance(country, Country):
value = country.id
elif country is None or country == 'None':
value = None
else:
raise AttributeError("Error setting country to {}.".format(country))
Suggestion.add_or_update(user, 'country', to_edit, value)
def set_text_property(bot, update, chat_data, property_name, to_edit=None):
uid = util.uid_from_update(update)
user = User.from_update(update)
if check_suggestion_limit(bot, update, user):
return
if to_edit:
text = (
util.escape_markdown(getattr(to_edit, property_name)) + "\n\n" if getattr(to_edit,
property_name) else '')
text += mdformat.action_hint(
messages.SET_BOTPROPERTY.format(
property_name,
util.escape_markdown(to_edit.username),
CLEAR_QUERY
))
if property_name == 'description':
text += ', markdown enabled.'
update.effective_message.reply_text(text, reply_markup=ForceReply(selective=True),
parse_mode=ParseMode.MARKDOWN)
chat_data['edit_bot'] = to_edit
elif update.message:
value = None
text = update.message.text
to_edit = chat_data.get('edit_bot', None)
def too_long(n):
bot.formatter.send_failure(uid, "Your {} text is too long, it must be shorter "
"than {} characters. Please try again.".format(
property_name, n))
util.wait(bot, update)
return admin.edit_bot(bot, update, chat_data, to_edit)
# Validation
if property_name == 'description' and len(text) > 300:
return too_long(300)
if property_name == 'username':
value = helpers.validate_username(text)
if value:
to_edit = chat_data.get('edit_bot', None)
else:
bot.formatter.send_failure(uid,
"The username you entered is not valid. Please try again...")
return admin.edit_bot(bot, update, chat_data, to_edit)
if not value:
value = text
if to_edit:
if _is_clear_query(text):
Suggestion.add_or_update(user, property_name, to_edit, None)
else:
Suggestion.add_or_update(user, property_name, to_edit, value)
admin.edit_bot(bot, update, chat_data, to_edit)
else:
bot.formatter.send_failure(uid, "An unexpected error occured.")
def toggle_value(bot, update, property_name, to_edit, value):
user = User.from_update(update)
if check_suggestion_limit(bot, update, user):
return
Suggestion.add_or_update(user, property_name, to_edit, bool(value))
def set_keywords_init(bot, update, chat_data, context):
to_edit = context.get('to_edit')
chat_data['set_keywords_msg'] = util.mid_from_update(update)
return set_keywords(bot, update, chat_data, to_edit)
@track_activity('menu', 'set keywords', Statistic.DETAILED)
def set_keywords(bot, update, chat_data, to_edit):
chat_id = util.uid_from_update(update)
keywords = Keyword.select().where(Keyword.entity == to_edit)
chat_data['edit_bot'] = to_edit
set_keywords_msgid = chat_data.get('set_keywords_msg')
pending = Suggestion.select().where(
Suggestion.executed == False,
Suggestion.subject == to_edit,
Suggestion.action << ['add_keyword', 'remove_keyword']
)
pending_removal = [y for y in pending if y.action == 'remove_keyword']
# Filter keywords by name to not include removal suggestions
# We don't need to do this for add_keyword suggestions, because duplicates are not allowed.
keywords = [k for k in keywords if k.name not in [s.value for s in pending_removal]]
kw_remove_buttons = [InlineCallbackButton(
'{} ✖️'.format(x),
callback_action=CallbackActions.REMOVE_KEYWORD,
params={'id': to_edit.id, 'kwid': x.id})
for x in keywords]
kw_remove_buttons.extend([InlineKeyboardButton(
'#{} 👓✖️'.format(x.value),
callback_data=util.callback_for_action(
CallbackActions.DELETE_KEYWORD_SUGGESTION,
{'id': to_edit.id, 'suggid': x.id}))
for x
in [y for y in pending if y.action == 'add_keyword']
])
kw_remove_buttons.extend([InlineKeyboardButton(
'#{} 👓❌'.format(x.value),
callback_data=util.callback_for_action(
CallbackActions.DELETE_KEYWORD_SUGGESTION,
{'id': to_edit.id, 'suggid': x.id}))
for x
in pending_removal
])
buttons = util.build_menu(kw_remove_buttons, 2, header_buttons=[
InlineKeyboardButton(captions.DONE,
callback_data=util.callback_for_action(
CallbackActions.ABORT_SETTING_KEYWORDS,
{'id': to_edit.id}))
])
reply_markup = InlineKeyboardMarkup(buttons)
msg = util.send_or_edit_md_message(
bot,
chat_id,
util.action_hint('Send me the keywords for {} one by one...\n\n{}'.format(
util.escape_markdown(to_edit.username), messages.KEYWORD_BEST_PRACTICES)),
to_edit=set_keywords_msgid,
reply_markup=reply_markup)
if msg:
# message might not have been edited if the user adds an already-existing keyword
# TODO: should the user be notified about this?
chat_data['set_keywords_msg'] = msg.message_id
return BotStates.SENDING_KEYWORDS
def add_keyword(bot, update, chat_data):
user = User.from_telegram_object(update.effective_user)
if check_suggestion_limit(bot, update, user):
return
kw = update.message.text
bot_to_edit = chat_data.get('edit_bot')
kw = helpers.format_keyword(kw)
# Sanity checks
if kw in settings.FORBIDDEN_KEYWORDS:
update.message.reply_text('The keyword {} is forbidden.'.format(kw))
return
if len(kw) <= 1:
update.message.reply_text('Keywords must be longer than 1 character.')
return
if len(kw) >= 20:
update.message.reply_text('Keywords must not be longer than 20 characters.')
# Ignore duplicates
try:
Keyword.get((Keyword.name == kw) & (Keyword.entity == bot_to_edit))
return
except Keyword.DoesNotExist:
pass
Suggestion.add_or_update(user=user, action='add_keyword', subject=bot_to_edit, value=kw)
set_keywords(bot, update, chat_data, bot_to_edit)
Statistic.of(update, 'added keyword to'.format(kw), bot_to_edit.username)
def delete_keyword_suggestion(bot, update, chat_data, context):
suggestion = context.get('suggestion')
suggestion.delete_instance()
set_keywords(bot, update, chat_data, context.get('to_edit'))
@restricted
def delete_bot_confirm(bot, update, to_edit):
chat_id = util.uid_from_update(update)
reply_markup = InlineKeyboardMarkup([[
InlineKeyboardButton("Yes, delete it!", callback_data=util.callback_for_action(
CallbackActions.DELETE_BOT, {'id': to_edit.id}
)),
InlineKeyboardButton(captions.BACK, callback_data=util.callback_for_action(
CallbackActions.EDIT_BOT, {'id': to_edit.id}
))
]]
)
bot.formatter.send_or_edit(chat_id, "Are you sure?", to_edit=util.mid_from_update(update),
reply_markup=reply_markup)
@restricted
def delete_bot(bot, update, to_edit: Bot):
username = to_edit.username
to_edit.disable(Bot.DisabledReason.banned)
to_edit.save()
bot.formatter.send_or_edit(
update.effective_user.id,
"Bot has been disabled and banned.",
to_edit=util.mid_from_update(update)
)
Statistic.of(update, 'disable', username, Statistic.IMPORTANT)
def change_category(bot, update, to_edit, category):
uid = update.effective_user.id
user = User.get(User.chat_id == uid)
if uid == 918962:
# Special for t3chno
to_edit.category = category
to_edit.save()
else:
if check_suggestion_limit(bot, update, user):
return
Suggestion.add_or_update(user, 'category', to_edit, category.id)
def check_suggestion_limit(bot, update, user):
cid = update.effective_chat.id
if Suggestion.over_limit(user):
bot.formatter.send_failure(cid,
"You have reached the limit of {} suggestions. Please wait for "
"the Moderators to approve of some of them.".format(
settings.SUGGESTION_LIMIT))
Statistic.of(update, 'hit the suggestion limit')
return True
return False
def change_suggestion(bot, update, suggestion, page_handover):
cid = update.effective_chat.id
mid = update.effective_message.message_id
text = '{}:\n\n{}'.format(str(suggestion), suggestion.value)
if suggestion.action == 'description':
callback_action = CallbackActions.EDIT_BOT_DESCRIPTION
elif suggestion.action == 'extra':
callback_action = CallbackActions.EDIT_BOT_EXTRA
elif suggestion.action == 'name':
callback_action = CallbackActions.EDIT_BOT_NAME
elif suggestion.action == 'username':
callback_action = CallbackActions.EDIT_BOT_USERNAME
else:
return # should not happen
buttons = [[
InlineKeyboardButton(captions.BACK,
callback_data=util.callback_for_action(
CallbackActions.SWITCH_SUGGESTIONS_PAGE,
{'page': page_handover}))
], [
InlineKeyboardButton("{} Accept".format(Emoji.WHITE_HEAVY_CHECK_MARK),
callback_data=util.callback_for_action(
CallbackActions.ACCEPT_SUGGESTION,
{'id': suggestion.id, 'page': page_handover}
)),
InlineKeyboardButton(captions.CHANGE_SUGGESTION, callback_data=util.callback_for_action(
callback_action, {'id': suggestion.id, 'page': page_handover}
)),
InlineKeyboardButton(Emoji.CROSS_MARK, callback_data=util.callback_for_action(
CallbackActions.REJECT_SUGGESTION, {'id': suggestion.id, 'page': page_handover}
))
]]
reply_markup = InlineKeyboardMarkup(buttons)
bot.formatter.send_or_edit(cid, text, to_edit=mid, disable_web_page_preview=True,
reply_markup=reply_markup)
def remove_keyword(bot, update, chat_data, context):
user = User.from_telegram_object(update.effective_user)
if check_suggestion_limit(bot, update, user):
return
to_edit = context.get('to_edit')
kw = context.get('keyword')
Suggestion.add_or_update(user=user, action='remove_keyword', subject=to_edit, value=kw.name)
return set_keywords(bot, update, chat_data, to_edit)
@restricted
def accept_suggestion(bot, update, suggestion: Suggestion):
user = User.from_telegram_object(update.effective_user)
suggestion.apply()
if suggestion.action == 'offline':
suggestion_text = '{} went {}.'.format(
suggestion.subject.str_no_md,
'offline' if suggestion.subject.offline else 'online')
else:
suggestion_text = str(suggestion)
suggestion_text = suggestion_text[0].upper() + suggestion_text[1:]
suggestion_text += '\nApproved by ' + user.markdown_short
bot.send_message(settings.BOTLIST_NOTIFICATIONS_ID, suggestion_text,
parse_mode='markdown', disable_web_page_preview=True)
if user != suggestion.user.chat_id:
submittant_notification = '*Thank you* {}, your suggestion has been accepted:' \
'\n\n{}'.format(util.escape_markdown(suggestion.user.first_name),
str(suggestion))
try:
bot.send_message(suggestion.user.chat_id, submittant_notification,
parse_mode='markdown', disable_web_page_preview=True)
except BadRequest:
update.effective_message.reply_text(
"Could not contact {}.".format(suggestion.user.markdown_short),
parse_mode='markdown', disable_web_page_preview=True)