-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
920 lines (673 loc) · 29.9 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
import datetime
import discord
import auxiliary
import configuration as config
import interactive
import models
async def configure_channel_command(command, db_channel):
"""
Configure a channel with the given settings.
If this channel does not yet exit in the DB, then create it.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# Make sure the user changing the channel settings is an admin
if not command.author.guild_permissions.administrator:
msg = 'Only server administrators can change a channel\'s settings.'
await auxiliary.send_temp_message(msg, command.channel)
return
# The ids of the Discord channel and server where the message was sent
discord_channel_id = command.channel.id
discord_server_id = command.guild.id
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# If the command has an invalid number of parameters
if len(params) != 2:
return
# Filter the chosen setting
delete_commands = False
delete_all = False
if params[1] == '-dc':
delete_commands = True
elif params[1] == '-da':
delete_all = True
elif params[1] == '-ka':
delete_all = False
delete_commands = False
# Create or modify the channel with the correct configurations
if db_channel is None:
db_channel = models.Channel(discord_channel_id, discord_server_id, delete_commands, delete_all)
config.session.add(db_channel)
else:
db_channel.delete_commands = delete_commands
db_channel.delete_all = delete_all
config.session.commit()
print('Channel %s from %s was configured -> %s!' % (
command.channel.name, command.guild.name, command.content))
async def create_poll_command(command, db_channel):
"""
Create a new poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# The ids of the Discord channel and server where the message was sent
discord_server_id = command.guild.id
# Create channel if it does not already exist
if db_channel is None:
db_channel = auxiliary.create_channel(command)
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
weekly = False
pt = False
day_param_pos = -1
day_param_poll_pos = -1
multiple_options = False
only_numbers = False
new_options = False
allow_external = False
# Confirmation is necessary when there is a need to close a poll before this one is created
confirmation = False
poll_params = []
# Filter the available configurations for polls
for i in range(len(params)):
if i == 0:
continue
if params[i] == '-weekly':
weekly = True
pt = False
day_param_pos = i + 1
day_param_poll_pos = len(poll_params)
elif params[i] == '-weekly_pt':
weekly = True
pt = True
day_param_pos = i + 1
day_param_poll_pos = len(poll_params)
elif params[i].startswith('-'):
if params[i].__contains__('m'):
multiple_options = True
if params[i].__contains__('o'):
only_numbers = True
if params[i].__contains__('n'):
new_options = True
if params[i].__contains__('e'):
allow_external = True
if params[i].__contains__('y'):
confirmation = True
else:
# Add all non configuration parameters, ignoring quotation marks
poll_params.append(params[i].replace('"', ''))
# If the command has an invalid number of parameters
if len(poll_params) < 2:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the poll with this id
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_params[0]).first()
# If a poll with the same id already exists, delete it
if poll is not None:
if poll.discord_author_id == command.author.id:
# Confirmation required before deleting a poll
if confirmation:
await auxiliary.delete_poll(poll, db_channel, command.author.id)
else:
msg = 'A poll with that id already exists add **-y** to your command to confirm the deletion of the ' \
'previous poll.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
else:
msg = 'A poll with that id already exists and you cannot close it because you are not its author!'
await auxiliary.send_temp_message(msg, command.channel)
return
num_polls = config.session.query(models.Poll).filter(models.Poll.discord_server_id == discord_server_id).count()
# Limit the number of polls per server
if num_polls >= config.POLL_LIMIT_SERVER:
polls = config.session.query(models.Poll).filter(models.Poll.discord_server_id == discord_server_id) \
.filter(models.Poll.discord_author_id == command.author.id).all()
msg = 'The server you\'re in has reached its poll limit, creating another poll is not possible.'
if len(polls) == 0:
msg += 'Ask the authors of other polls to delete them.\nYour command: **%s**' % command.content
else:
msg += 'Delete one of your polls before continuing.\nList of your polls in this server:'
for p in polls:
msg += '\n%s - !poll_delete %s' % (p.poll_key, p.poll_key)
msg += '\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
# Create the new poll
new_poll = models.Poll(poll_params[0], command.author.id, poll_params[1], multiple_options, only_numbers,
new_options, allow_external, db_channel.id, discord_server_id)
config.session.add(new_poll)
# Send a private message to each member in the server
for m in command.channel.members:
if m != config.client.user and m.id != new_poll.discord_author_id:
try:
await m.send('A new poll (%s) has been created in %s!'
% (new_poll.poll_key, command.channel.mention))
except (discord.errors.Forbidden, discord.errors.HTTPException):
pass
# Necessary for the options to get the poll id
config.session.flush()
options = []
# Get the current dates
start_date = datetime.datetime.today()
num_options = max(6 - start_date.weekday(), 0)
end_date = start_date + datetime.timedelta(days=num_options)
# Calculate the date interval for the options
if weekly and day_param_poll_pos < len(poll_params):
try:
days = params[day_param_pos].split(',')
starting_day = int(days[0])
start_date = auxiliary.date_given_day(start_date, starting_day)
if len(days) > 1:
end_day = int(days[1])
end_date = auxiliary.date_given_day(start_date, end_day)
# Remove this option
poll_params.remove(poll_params[day_param_poll_pos])
except ValueError:
pass
# Create the options
if len(poll_params[2:]) != 0 or weekly:
# Add days of the week as options
if weekly:
weekly_options = auxiliary.create_weekly_options(start_date, end_date, pt=pt)
for option in weekly_options:
options.append(models.Option(new_poll.id, len(options) + 1, option))
for option in poll_params[2:]:
options.append(models.Option(new_poll.id, len(options) + 1, option))
# If no options were provided, then create the default Yes and No
else:
options.append(models.Option(new_poll.id, 1, 'Yes'))
options.append(models.Option(new_poll.id, 2, 'No'))
config.session.add_all(options)
# Create the message with the poll
msg = await command.channel.send(auxiliary.create_message(new_poll, options))
new_poll.discord_message_id = msg.id
# Add a reaction for each option
await auxiliary.add_options_reactions(msg, options)
config.session.commit()
print('Poll %s created -> %s!' % (new_poll.poll_key, command.content))
async def edit_poll_command(command, db_channel):
"""
Edit a poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to edit!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
add = False
remove = False
lock = False
unlock = False
multiple_options = False
only_numbers = False
new_options = False
allow_external = False
poll_params = []
# Filter the available options for polls
for i in range(len(params)):
if i == 0:
continue
if params[i] == '-add':
add = True
remove = False
lock = False
unlock = False
elif params[i] == '-rm':
remove = True
add = False
lock = False
unlock = False
elif params[i] == '-lock':
remove = False
add = False
lock = True
unlock = False
elif params[i] == '-unlock':
remove = False
add = False
lock = False
unlock = True
elif params[i].startswith('-'):
if params[i].__contains__('m'):
multiple_options = True
if params[i].__contains__('o'):
only_numbers = True
if params[i].__contains__('n'):
new_options = True
if params[i].__contains__('e'):
allow_external = True
else:
# Add all non configuration parameters, ignoring quotation marks
poll_params.append(params[i].replace('"', ''))
# If the command has an invalid number of parameters
if (len(poll_params) < 2 and (add or remove or lock or unlock)) or \
(len(poll_params) < 1 and not add and not remove and not lock and not unlock):
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
poll_key = poll_params[0]
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# If no poll was found with that id
if poll is None:
msg = 'There\'s no poll with that id for you to edit.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
# Only the author can edit
if poll.discord_author_id != command.author.id:
msg = 'Only the author of a poll can edit it!'
await auxiliary.send_temp_message(msg, command.channel)
return
edited = ''
# Get all options available in the poll
db_options = config.session.query(models.Option).filter(models.Option.poll_id == poll.id) \
.order_by(models.Option.position).all()
# Add the new options
if add:
new_options = poll_params[1:]
options = []
edited = 'new options added %s' % new_options
# Create the options
for option in new_options:
options.append(models.Option(poll.id, len(db_options) + len(options) + 1, option))
config.session.add_all(options)
# Get the message corresponding to the poll
c = config.client.get_channel(db_channel.discord_id)
discord_poll_msg = await c.fetch_message(poll.discord_message_id)
# Add a reaction for each new option
emoji = chr(ord(u'\u0031') + len(db_options))
# Max number of reactions that can be added
num_react = min(9, len(db_options) + len(options))
for i in range(max(0, num_react - len(db_options))):
await discord_poll_msg.add_reaction(emoji + u'\u20E3')
emoji = chr(ord(emoji) + 1)
db_options.extend(options)
# Remove, lock or unlock options
elif remove or lock or unlock:
rm_options = poll_params[1]
# Option is a number
try:
# Verify if the options are numbers
selected_options = []
for o in rm_options.split(','):
selected_options.append(int(o))
# Removes duplicates in the list
selected_options = list(set(selected_options))
if remove:
options = config.session.query(models.Option) \
.filter(models.Option.poll_id == poll.id) \
.filter(models.Option.position.in_(selected_options)) \
.all()
num_reactions = max(10 - len(db_options) - len(options), 0)
edited = 'options removed %s' % options
for option in options:
config.session.delete(option)
# Get the message corresponding to the poll
c = config.client.get_channel(db_channel.discord_id)
discord_poll_msg = await c.fetch_message(poll.discord_message_id)
db_options = config.session.query(models.Option).filter(models.Option.poll_id == poll.id) \
.order_by(models.Option.position).all()
for i in range(num_reactions):
emoji = chr(ord(u'\u0031') + len(db_options) + i)
await auxiliary.remove_reaction(discord_poll_msg, emoji)
# Update the positions
pos = 1
for option in db_options:
option.position = pos
pos += 1
elif lock or unlock:
if lock:
edited = 'options %s locked' % selected_options
else:
edited = 'options %s unlocked' % selected_options
for option in selected_options:
num_options = len(db_options)
# If it is a valid option
if 0 < option <= num_options:
if lock:
db_options[option - 1].locked = True
elif unlock:
db_options[option - 1].locked = False
# Option is not a number
except ValueError:
pass
else:
# Edit poll question
if len(poll_params) > 1:
poll.question = poll_params[1]
edited = 'question is now %s' % poll.question
# Edit poll settings
else:
poll.multiple_options = multiple_options
poll.only_numbers = only_numbers
poll.new_options = new_options
poll.allow_external = allow_external
edited = 'settings multiple_options=%r, only_numbers=%r, new_options=%r, allow_external=%r changed' \
% (multiple_options, only_numbers, new_options, allow_external)
# Edit message
c = config.client.get_channel(db_channel.discord_id)
try:
m = await c.fetch_message(poll.discord_message_id)
await m.edit(content=auxiliary.create_message(poll, db_options))
except discord.errors.NotFound:
config.session.delete(poll)
config.session.commit()
print('Poll %s was edited for %s -> %s!' % (poll.poll_key, edited, command.content))
async def close_poll_command(command, db_channel):
"""
Close a poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to close!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# If the command has an invalid number of parameters
if len(params) != 3:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
poll_key = params[1]
# Split the selected options
list_options = params[2].split(',')
# Options are all numbers
try:
# Verify if the options are numbers
selected_options = []
for o in list_options:
selected_options.append(int(o))
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# Edit the message with the poll
if poll is not None:
# Only the author can close the poll
if poll.discord_author_id == command.author.id:
options = config.session.query(models.Option).filter(models.Option.poll_id == poll.id) \
.order_by(models.Option.position).all()
# Send a private message to all participants in the poll
await auxiliary.send_closed_poll_message(options, command.guild, poll, command.channel)
await auxiliary.close_poll(poll, db_channel, selected_options)
config.session.commit()
print('Poll %s closed -> %s!' % (poll.poll_key, command.content))
else:
msg = 'There\'s no poll with that id for you to close.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
except ValueError:
pass
async def delete_poll_command(command, db_channel):
"""
Delete a poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to delete!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# If the command has an invalid number of parameters
if len(params) != 2:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
poll_key = params[1]
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# Delete the message with the poll
if poll is not None:
await auxiliary.delete_poll(poll, db_channel, command.author.id)
config.session.commit()
print('Poll %s deleted -> %s!' % (poll.poll_key, command.content))
else:
msg = 'There\'s no poll with that id for you to delete.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
async def vote_poll_command(command, db_channel):
"""
models.Vote a list of options in a poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to vote!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# Check for external voters
if params.__contains__('-e') and len(params) == 5:
author_id = params[4]
if author_id[0] != '"':
author_id = '"%s"' % author_id
else:
# If the command has an invalid number of parameters
if len(params) != 3:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
author_id = command.author.id
poll_key = params[1]
options = params[2]
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# If no poll was found with that id
if poll is None:
msg = 'There\'s no poll with that id for you to vote.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
# Get all options available in the poll
db_options = config.session.query(models.Option).filter(models.Option.poll_id == poll.id) \
.order_by(models.Option.position).all()
# If it is an vote for an external user and it is not allowed
if author_id is None and not poll.allow_external:
msg = 'models.Poll *%s* does not allow for external votes.\n' \
'If you need this option, ask the poll author to edit it.' % poll_key
await auxiliary.send_temp_message(msg, command.channel)
return
poll_edited = False
# Option is a list of numbers
try:
# Verify if the options are numbers
selected_options = []
for o in options.split(','):
selected_options.append(int(o))
for option in selected_options:
poll_edited |= auxiliary.add_vote(option, author_id, db_options, poll.multiple_options)
# Option is not a list of numbers
except ValueError:
if poll.new_options:
if not poll.multiple_options:
auxiliary.remove_prev_vote(db_options, author_id)
if options[0] == '"' and options[-1] == '"':
# Remove quotation marks
options = options.replace('"', '')
# Add the new option to the poll
options = models.Option(poll.id, len(db_options) + 1, options)
db_options.append(options)
config.session.add(options)
config.session.flush()
# Check the type of participant
# int means discord used
# string means external participant
if type(author_id) == str:
discord_participant_id = None
participant_name = author_id
else:
discord_participant_id = author_id
participant_name = None
vote = models.Vote(options.id, discord_participant_id, participant_name)
config.session.add(vote)
poll_edited = True
else:
msg = 'models.Poll *%s* does not allow for new votes.\n' \
'If you need this option, ask the poll author to edit it.' % poll_key
await auxiliary.send_temp_message(msg, command.channel)
return
# Edit the message
if poll_edited:
c = config.client.get_channel(db_channel.discord_id)
try:
m = await c.fetch_message(poll.discord_message_id)
await m.edit(content=auxiliary.create_message(poll, db_options))
except discord.errors.NotFound:
config.session.delete(poll)
config.session.commit()
print('%s voted in %s -> %s!' % (author_id, poll.poll_key, command.content))
async def unvote_poll_command(command, db_channel):
"""
Remove a vote from an option in a poll.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to unvote!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# Check for external voters
if params.__contains__('-e') and len(params) == 5:
author_id = params[4]
if author_id[0] != '"':
author_id = '"%s"' % author_id
else:
# If the command has an invalid number of parameters
if len(params) != 3:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
author_id = command.author.id
poll_key = params[1]
options = params[2]
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# If no poll was found with that id
if poll is None:
msg = 'There\'s no poll with that id for you to unvote.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
# Get all options available in the poll
db_options = config.session.query(models.Option).filter(models.Option.poll_id == poll.id) \
.order_by(models.Option.position).all()
poll_edited = False
# Option is a number
try:
# Verify if the options are numbers
selected_options = []
for o in options.split(','):
selected_options.append(int(o))
for option in selected_options:
poll_edited |= auxiliary.remove_vote(option, author_id, db_options)
if poll_edited:
# Edit the message
c = config.client.get_channel(db_channel.discord_id)
try:
m = await c.fetch_message(poll.discord_message_id)
await m.edit(content=auxiliary.create_message(poll, db_options))
except discord.errors.NotFound:
config.session.delete(poll)
config.session.commit()
print('%s removed vote from %s -> %s!' % (author_id, poll.poll_key, command.content))
# Option is not a number
except ValueError:
pass
async def refresh_poll_command(command, db_channel):
"""
Show a pole in a new message.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
msg = 'There\'s no poll in this channel for you to refresh!'
await auxiliary.send_temp_message(msg, command.channel)
return
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# If the command has an invalid number of parameters
if len(params) != 2:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
poll_key = params[1]
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key).first()
# Create the message with the poll
# and delete the previous message
if poll is not None:
await auxiliary.refresh_poll(poll, db_channel.discord_id)
print('Poll %s refreshed -> %s!' % (poll.poll_key, command.content))
async def poll_mention_message_command(command, db_channel):
"""
Create a message mentioning the voters of a given option.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# If the channel does not exist in the DB
if db_channel is None:
auxiliary.create_channel(command)
# Get the list of parameters in the message
params = auxiliary.parse_command_parameters(command.content)
# If the command has an invalid number of parameters
if len(params) != 4:
msg = 'Invalid parameters in command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
return
poll_key = params[1]
message = params[3]
try:
poll_option = int(params[2])
# Select the current poll
poll = config.session.query(models.Poll).filter(models.Poll.poll_key == poll_key,
models.Poll.discord_server_id == command.guild.id).first()
if poll is not None:
msg = auxiliary.create_poll_mention_message(poll_option, message, poll.id, command.author.id)
if msg is not None:
await command.channel.send(msg)
else:
msg = 'There\'s no poll with that id for you to mention.\nYour command: **%s**' % command.content
await auxiliary.send_temp_message(msg, command.channel)
except ValueError:
pass
async def help_message_command(command, db_channel):
"""
Show a help message with the available commands.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# Create channel if it doesn't already exist
if db_channel is None:
auxiliary.create_channel(command)
msg = 'Poll Me Bot Help\n' \
'----------------\n' \
'Creating a poll: *!poll poll_key "Question" "Option 1" "Option 2"*\n' \
'Voting: *!vote poll_key list_of_numbers_separated_by_comma*\n' \
'Removing votes: *!unvote poll_key list_of_numbers_separated_by_comma*\n' \
'(More options and details are available at https://github.com/correia55/models.PollMeBot)\n' \
'(This message will self-destruct in 30 seconds.)'
await auxiliary.send_temp_message(msg, command.channel)
async def start_interactive_command(command: discord.message.Message, db_channel):
"""
Show a help message with the available commands.
:param command: the command used.
:param db_channel: the corresponding channel entry in the DB.
"""
# Create channel if it doesn't already exist
if db_channel is None:
auxiliary.create_channel(command)
msg = interactive.header % 'menu'
for i in range(len(interactive.menu_options)):
msg += '\n' + str(i + 1) + ' - ' + interactive.menu_options[i]
await auxiliary.show_interactive_message(msg, command.channel, interactive.menu_options)