-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.py
864 lines (739 loc) · 30.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
from __future__ import annotations
import asyncio
import io
import random
import textwrap
from functools import wraps
from typing import Awaitable
from typing import Callable
from typing import Generator
from typing import Optional
from typing import Protocol
import discord
import pandas as pd
import pendulum
import requests
from PIL import Image
from sqlalchemy import and_
from sqlalchemy import delete
from sqlalchemy import desc
from sqlalchemy import or_
from sqlalchemy import select
from sqlalchemy import update
import diffle
from bernardynki import Bernardynki
from botka_script.utils import interpret_source
from carrotson import CONTEXT_SIZE
from carrotson import split_into_paths
from command import Command
from database import get_db
from decorators import daily
from decorators import run_every
from difflanek import difflanek
from difflanek import opencv
from exceptions import CommandNotFound
from exceptions import DiscordMessageMissingException
from getenv import getenv
from logger import get_logger
from message_context import MessageContext
from models import Carrot
from models import CommandModel
from models import Markov2
from models import Markov3
from models import VariableModel
from settings import COMMON_PREFIXES
from settings import DEFAULT_PREFIX
from settings import DISCORD_MESSAGE_LIMIT
from settings import MARKOV_MIN_WORD_COUNT
from settings import RANDOM_MARKOV_MESSAGE_CHANCE
from settings import RANDOM_MARKOV_MESSAGE_COUNT
from utils import Buf
from utils import format_fraction
from utils import get_markov_weights
from utils import shuffle_str
from utils import triggered_chance
from utils import window
class CommandFunc(Protocol):
def __call__(self, context: MessageContext, client: discord.Client) -> Awaitable[MessageContext]: ...
logger = get_logger(__name__)
COMMANDS = {}
HIDDEN_COMMANDS = {}
SPECIAL_COMMANDS = {}
def parse_pipe(message: str, prefix: str = DEFAULT_PREFIX) -> list[Command]:
ret = []
cmds = _parse_commands(message, prefix)
for cmd in cmds:
if get_builtin_command(cmd.name):
ret.append(cmd)
continue
custom_cmd = get_custom_command(cmd.name)
if custom_cmd is not None:
ret.extend(custom_cmd)
continue
raise CommandNotFound(cmd.name)
return ret
def _parse_commands(message: str, prefix: str = DEFAULT_PREFIX) -> List[Command]:
def _split_by_pipe(message: str) -> Generator[str, None, None]:
buf = ''
escaped = False
while True:
if len(message) == 0:
if escaped:
raise ValueError('did not find closing ```')
if buf:
yield buf
return None
if message.startswith('```'):
buf += message[:3]
message = message[3:]
escaped = not escaped
continue
if message.startswith('||'):
buf += message[:2]
message = message[2:]
continue
if message.startswith('|') and not escaped:
yield buf
buf = ''
message = message[1:]
continue
buf += message[0]
message = message[1:]
parts = list(_split_by_pipe(message))
commands = [Command.from_str(part.lstrip(), prefix) for part in parts]
return commands
def get_builtin_command(cmd_name: str) -> CommandFunc | None:
return COMMANDS.get(cmd_name)
def get_custom_command(cmd_name: str) -> list[Command] | None:
with get_db() as db:
command = db.execute(
select(CommandModel)
.where(CommandModel.name == cmd_name),
).scalar_one_or_none()
if command is None:
return None
return parse_pipe(command.command, prefix=DEFAULT_PREFIX)
def get_command(cmd_name: str) -> CommandFunc | CommandModel | None:
cmd = get_builtin_command(cmd_name)
if cmd is None:
cmd = get_custom_command(cmd_name)
if cmd is None:
return None
return cmd
def command(*, name: str, hidden: bool = False, special: bool = False) -> Callable[[CommandFunc], CommandFunc]:
def decorator(func: CommandFunc) -> CommandFunc:
@wraps(func)
async def wrapper(context: MessageContext, client: discord.Client) -> MessageContext:
return await func(context, client)
if not hidden:
COMMANDS[name] = wrapper
else:
HIDDEN_COMMANDS[name] = wrapper
if special:
SPECIAL_COMMANDS[name] = wrapper
return wrapper
return decorator
@command(name='ping')
async def ping(context: MessageContext, client: discord.Client) -> MessageContext:
return context.updated(result='pong')
@command(name='bing')
async def bing(context: MessageContext, client: discord.Client) -> MessageContext:
return context.updated(result='bong')
@command(name='commands')
async def commands(context: MessageContext, client: discord.Client) -> MessageContext:
prefix = client.prefix
result = ', '.join(f'{prefix}{command}' for command in sorted(COMMANDS))
return context.updated(result=result)
@command(name='random')
async def random_(context: MessageContext, client: discord.Client) -> MessageContext:
try:
logger.debug('%s', context.result)
cmd = context.command
if len(cmd.args) == 0:
return context.updated(result=str(random.random()))
elif len(cmd.args) == 1:
return context.updated(result=str(random.randint(0, int(cmd.args[0]))))
elif len(cmd.args) == 2:
return context.updated(result=str(random.randint(int(cmd.args[0]), int(cmd.args[1]))))
return context.updated(result='Co za dużo to niezdrowo')
except ValueError as e:
logger.exception(e)
return context.updated(result=str(e))
return context.updated(result='Coś ty narobił')
@command(name='echo')
async def echo(context: MessageContext, client: discord.Client) -> MessageContext:
if context.command.raw_args:
content = context.command.raw_args
else:
content = ' '.join([context.result] * 2)
return context.updated(result=content)
@command(name='scream')
async def scream(context: MessageContext, client: discord.Client) -> MessageContext:
return context.updated(result=context.result.upper())
@command(name='shrug')
async def shrug(context: MessageContext, client: discord.Client) -> MessageContext:
if context.command.raw_args:
content = context.command.raw_args
else:
content = context.result
result = rf'¯\\\_{content}_/¯'
return context.updated(result=result)
@command(name='shuffle_words')
async def shuffle_words(context: MessageContext, client: discord.Client) -> MessageContext:
if context.command.raw_args:
content = context.command.raw_args
else:
content = context.result
return context.updated(result=' '.join(shuffle_str(word) for word in content.split()))
@command(name='chid')
async def chid(context: MessageContext, client: discord.Client) -> MessageContext:
return context.updated(result=str(context.message.channel.id))
@command(name='myid')
async def myid(context: MessageContext, client: discord.Client) -> MessageContext:
return context.updated(result=str(context.message.author.id))
@command(name='markov2', hidden=True)
async def markov2(context: MessageContext, client: discord.Client) -> MessageContext:
parts = context.message.content.split()
if len(parts) < 2:
return context
with get_db() as db:
for word1, word2 in window([None] + parts + [None], n=2):
result = db.execute(
update(Markov2)
.where(
and_(
Markov2.word1 == word1,
Markov2.word2 == word2,
Markov2.channel_id == context.message.channel.id,
Markov2.guild_id == context.message.guild.id,
),
)
.values(counter=Markov2.counter + 1),
)
if result.rowcount == 0: # type: ignore
db.add(
Markov2(
word1=word1,
word2=word2,
channel_id=context.message.channel.id,
guild_id=context.message.guild.id,
),
)
db.commit()
return context
@command(name='markov3', hidden=True)
async def markov3(context: MessageContext, client: discord.Client) -> MessageContext:
parts = context.message.content.split()
if len(parts) < 3:
return context
with get_db() as db:
for word1, word2, word3 in window([None] + parts + [None], n=3):
result = db.execute(
update(Markov3)
.where(
and_(
Markov3.word1 == word1,
Markov3.word2 == word2,
Markov3.word3 == word3,
Markov3.channel_id == context.message.channel.id,
Markov3.guild_id == context.message.guild.id,
),
)
.values(counter=Markov3.counter + 1),
)
if result.rowcount == 0: # type: ignore
db.add(
Markov3(
word1=word1,
word2=word2,
word3=word3,
channel_id=context.message.channel.id,
guild_id=context.message.guild.id,
),
)
db.commit()
return context
async def carrot(context: MessageContext, client: discord.Client) -> MessageContext:
with get_db() as db:
for path in split_into_paths(context.message.content):
result = db.execute(
update(Carrot)
.where(
and_(
Carrot.context == path.context,
Carrot.following == path.following,
Carrot.channel_id == context.message.channel.id,
Carrot.guild_id == context.message.guild.id,
),
)
.values(counter=Carrot.counter + 1),
)
if result.rowcount == 0: # type: ignore
db.add(
Carrot(
context=path.context,
following=path.following,
channel_id=context.message.channel.id,
guild_id=context.message.guild.id,
),
)
db.commit()
return context
@command(name='inspire')
async def inspire(context: MessageContext, client: discord.Client) -> MessageContext:
logger.info('Sending an inspiring message.')
response = requests.get('https://inspirobot.me/api?generate=true')
image = Image.open(requests.get(response.text, stream=True).raw)
with io.BytesIO() as image_binary:
image.save(image_binary, 'PNG')
image_binary.seek(0)
channel = client.get_channel(getenv('INSPIRATIONAL_MESSAGE_CHANNEL_ID', as_=int))
await channel.send(
file=discord.File(fp=image_binary, filename='daily_inspiration.png'),
)
return context
@daily(at='8:00')
@command(name='daily_inspiration', hidden=True)
async def daily_inspiration(context: MessageContext, client: discord.Client) -> MessageContext:
channel = client.get_channel(getenv('INSPIRATIONAL_MESSAGE_CHANNEL_ID', as_=int))
await channel.send('Miłego dnia i smacznej kawusi <3')
await inspire(context=context, client=client) # type: ignore
return context
@command(name='train_markov', hidden=True)
async def train_markov(context: MessageContext, client: discord.Client) -> MessageContext:
i = 1
async for message in context.message.channel.history(limit=None):
i += 1
logger.debug('Training on channel %s: message no %s', message.channel, i)
if (
message.author != client.user
and not message.content.startswith(client.prefix)
and len(message.content.split()) > MARKOV_MIN_WORD_COUNT
):
await markov2(MessageContext(original_message=message), client)
await markov3(MessageContext(original_message=message), client)
logger.debug('DONE TRAINING ON CHANNEL %s', context.message.channel)
return context
@command(name='train_carrot')
async def train_carrot(context: MessageContext, client: discord.Client) -> MessageContext:
i = 1
async for message in context.message.channel.history(limit=None):
i += 1
logger.debug('Training on channel %s: message no %s', message.channel, i)
if (
message.author != client.user
and not message.content.startswith((client.prefix, *COMMON_PREFIXES))
and len(message.content) >= CONTEXT_SIZE
):
await carrot(MessageContext(original_message=message), client)
logger.debug('DONE TRAINING ON CHANNEL %s', context.message.channel)
return context
@command(name='m')
async def generate_markov2(context: MessageContext, client: discord.Client) -> MessageContext:
try:
markov_message = context.command.args
previous_message: Optional[str] = context.command.args[-1]
except (IndexError, DiscordMessageMissingException):
markov_message = []
previous_message = None
with get_db() as db:
while True:
candidates = db.execute(
select(Markov2)
.where(
Markov2.word1 == previous_message,
),
).scalars().all()
if len(candidates) == 0:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
[candidate] = random.choices(candidates, get_markov_weights(candidates))
if candidate is None:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
previous_message = candidate.word2
if previous_message is None or len(' '.join(markov_message + [previous_message])) > DISCORD_MESSAGE_LIMIT:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
markov_message.append(previous_message)
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
@command(name='m3')
async def generate_markov3(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) != 0:
return context.updated(result="Currently command does not take any arguments. Sorry 'bout that.")
markov_message: list[str] = []
previous_message = Buf(size=2)
if previous_message.get() == [None, None]:
with get_db() as db:
candidates = db.execute(
select(Markov3),
).scalars().all()
[candidate] = random.choices(candidates, get_markov_weights(candidates))
previous_message.push(candidate.word3)
markov_message.append(candidate.word3)
candidates = db.execute(
select(Markov3)
.where(
or_(
Markov3.word1 == candidate.word3,
Markov3.word2 == candidate.word3,
),
),
).scalars().all()
[candidate] = random.choices(candidates, get_markov_weights(candidates))
previous_message.push(candidate.word3)
markov_message.append(candidate.word3)
markov_message = [w for w in markov_message if w is not None]
with get_db() as db:
while True:
candidates = db.execute(
select(Markov3)
.where(
Markov3.word1 == previous_message.get()[0],
Markov3.word2 == previous_message.get()[1],
),
).scalars().all()
if len(candidates) == 0:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
[candidate] = random.choices(candidates, get_markov_weights(candidates))
if candidate is None or candidate.word3 is None:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
previous_message.push(candidate.word3)
if previous_message is None or len(' '.join(markov_message + [previous_message.last])) > DISCORD_MESSAGE_LIMIT:
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
markov_message.append(previous_message.last)
return context.updated(result=context.result + ' ' + ' '.join(markov_message))
def _get_carrot_candidates(db, context: str) -> list[Carrot]:
is_partial = 0 < len(context) < CONTEXT_SIZE
candidates = db.execute(
select(Carrot)
.where(
(Carrot.context == context) if not is_partial else Carrot.context.startswith(context),
)
.order_by(desc(Carrot.counter)),
).scalars().all()
return candidates
@command(name='carrot')
async def generate_carrot(context: MessageContext, client: discord.Client) -> MessageContext:
msg_context = context.command.raw_args
with get_db() as db:
while len(msg_context) < DISCORD_MESSAGE_LIMIT:
candidates = _get_carrot_candidates(db, context=msg_context[-CONTEXT_SIZE:] if msg_context else '')
if not candidates:
return context.updated(result=msg_context)
random.shuffle(candidates)
if 0 < len(msg_context) < CONTEXT_SIZE:
msg_context = candidates[0].context + candidates[0].following
else:
msg_context += candidates[0].following
return context.updated(result=msg_context)
@command(name='addcmd', special=True)
async def add_command(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) == 0:
return context.updated(result=f'Usage: `{client.prefix}addcmd <command_name> <instructions>`')
cmd_name = context.command.args[0]
try:
with get_db() as db:
db.add(
CommandModel(
name=cmd_name,
command=context.command.raw_args.split(' ', maxsplit=1)[1],
),
)
db.commit()
except Exception as e:
return context.updated(result=str(e))
return context.updated(result=f'Command `{cmd_name}` added')
@command(name='updatecmd', special=True)
async def update_command(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) == 0:
return context.updated(result=f'Usage: `{client.prefix}updatecmd <command_name> <instructions>`')
cmd_name = context.command.args[0]
try:
new_cmd_value = context.command.raw_args.split(' ', maxsplit=1)[1]
commands = parse_pipe(new_cmd_value, prefix=client.prefix)
commands_str = ' | '.join(
cmd.to_str()
for cmd in commands
)
with get_db() as db:
result = db.execute(
update(CommandModel)
.where(CommandModel.name == cmd_name)
.values(command=commands_str),
)
db.commit()
if result.rowcount == 0: # type: ignore
return context.updated(result=f'Command `{cmd_name}` not found')
except Exception as e:
return context.updated(result=str(e))
return context.updated(result=f'Command `{cmd_name}` updated')
@command(name='delcmd', special=True)
async def delete_command(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) != 1:
return context.updated(result=f'Usage: `{client.prefix}delcmd <command_name>`')
cmd_name = context.command.args[0]
try:
with get_db() as db:
db.execute(
delete(CommandModel)
.where(CommandModel.name == cmd_name),
)
db.commit()
except Exception as e:
return context.updated(result=str(e))
return context.updated(result=f'Command `{cmd_name}` deleted')
@command(name='showcmd', special=True)
async def show_command(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) != 1:
return context.updated(result=f'Usage: `{client.prefix}showcmd <command_name>`')
cmd_name = context.command.args[0]
try:
if cmd_name in COMMANDS:
return context.updated(result=f'Command `{cmd_name}` is builtin')
with get_db() as db:
command = db.execute(
select(CommandModel)
.where(CommandModel.name == cmd_name),
).scalar_one_or_none()
if command is not None:
cmd_display = command.command.replace('`', '\\`')
return context.updated(result=f'Command `{cmd_name}` is defined as: ```{cmd_display}\n```')
else:
return context.updated(result=f'Command `{cmd_name}` is not defined')
except Exception as e:
return context.updated(result=str(e))
@command(name='set', special=True)
async def set_variable(context: MessageContext, client: discord.Client) -> MessageContext:
if len(context.command.args) < 2:
return context.updated(result=f'Usage: `{client.prefix}set <variable_name> <value>`')
try:
var_name, var_value = context.command.raw_args.split(' ', maxsplit=1)
with get_db() as db:
result = db.execute(
update(VariableModel)
.where(VariableModel.name == var_name)
.values(value=context.command.raw_args.split(' ', maxsplit=1)[1]),
)
db.commit()
if result.rowcount == 0: # type: ignore
db.add(
VariableModel(
name=var_name,
value=var_value,
),
)
db.commit()
return context.updated(result=f'Variable `{var_name}` set to {var_value}')
except Exception as e:
return context.updated(result=str(e))
async def generate_markov_at_random_time(context: MessageContext, client: discord.Client) -> None:
while True:
await asyncio.sleep(1 * 60)
with get_db() as db:
random_markov_chance = db.execute(
select(VariableModel)
.where(VariableModel.name == 'RANDOM_MARKOV_CHANCE'),
).scalar_one_or_none()
if random_markov_chance is None:
random_markov_chance = RANDOM_MARKOV_MESSAGE_CHANCE
else:
try:
random_markov_chance = float(random_markov_chance.value)
except ValueError:
random_markov_chance = RANDOM_MARKOV_MESSAGE_CHANCE
if triggered_chance(random_markov_chance):
random_markov_message_count = db.execute(
select(VariableModel)
.where(VariableModel.name == 'RANDOM_MARKOV_MESSAGE_COUNT'),
).scalar_one_or_none()
if random_markov_message_count is None:
random_markov_message_count = RANDOM_MARKOV_MESSAGE_COUNT
else:
try:
random_markov_message_count = int(random_markov_message_count.value)
except ValueError:
random_markov_message_count = RANDOM_MARKOV_MESSAGE_CHANCE
for _ in range(random_markov_message_count):
markov_message = await generate_markov2(context=MessageContext.empty(), client=client)
if triggered_chance(0.5):
markov_message = await scream(markov_message, client=client)
await client.get_channel(
getenv('RANDOM_MARKOV_MESSAGE_CHANNEL_ID', as_=int),
).send(markov_message.result)
@run_every(days=1, condition=lambda dt: (Bernardynki.next_after(dt).when - dt).in_days() in (7, 3, 1, 0))
@command(name='next_bernardynki', special=True)
async def next_bernardynki(context: MessageContext, client: discord.Client) -> MessageContext:
now = pendulum.now(pendulum.UTC)
year_in_words_mapping = {
1: 'pierwszego',
2: 'drugiego',
3: 'trzeciego',
4: 'czwartego',
5: 'piątego',
}
next_bernardynki = Bernardynki.first
while next_bernardynki < now:
next_bernardynki += 1
sign = '+'
offset = context.command.raw_args.strip()
if offset.startswith(('-', '+')):
sign, offset = offset[0], offset[1:]
try:
offset = int(offset)
except ValueError:
offset = 0
if sign == '+':
next_bernardynki += offset
else:
next_bernardynki -= offset
date_fmt = next_bernardynki.when.format('dddd DD.MM.YYYY', locale='pl')
days = next_bernardynki.when.diff(now).in_days()
year_in_words = year_in_words_mapping[next_bernardynki.year]
if days == 0:
days_fmt = 'dzisiaj <3'
elif days == 1:
days_fmt = 'jutro!'
else:
days_fmt = f'za {days} dni'
msg = f'{next_bernardynki.ordinal}. bernardynki roku {year_in_words} {days_fmt} ({date_fmt})'
if context is None:
await client.get_channel(
getenv('RANDOM_MARKOV_MESSAGE_CHANNEL_ID', as_=int),
).send(msg)
else:
return context.updated(result=msg)
@command(name='suggest')
async def suggest(context: MessageContext, client: discord.Client) -> MessageContext:
await context.message.add_reaction('⬆️')
await context.message.add_reaction('⬇️')
return context
@command(name='yywrap')
async def yywrap(context: MessageContext, client: discord.Client) -> MessageContext:
logger.debug('yy > %s', context.command.raw_args)
source = context.command.raw_args
if source.startswith('```'):
source = source[3:]
if source.endswith('```'):
source = source[:-3]
if not source:
return context.updated(
result=textwrap.dedent(f'''
```
yywrap - wykonywacz kodu źródłowego
użycie: {client.prefix}yywrap <SOURCE_CODE>
przykład: {client.prefix}yywrap (message "XD")
przykład: {client.prefix}yywrap (message (2+2))
```'''),
)
extra = {
'current_message_context': context.result,
}
if context.message.reference is not None:
referenced_message = await context.message.channel.fetch_message(context.message.reference.message_id)
extra['referenced_message'] = referenced_message.content
result = interpret_source(
source,
**extra,
exc=Exception,
)
logger.debug('yy > %s', result)
if result.success:
await context.message.add_reaction('✔️')
return context.updated(result=result.stdout)
else:
await context.message.add_reaction('❌')
return context.updated(result=result.stderr)
@command(name='dfl')
async def dfl(context: MessageContext, client: discord.Client) -> MessageContext:
_help = """\
```
jak wpisywać słowa:
szary - x
żółty - X
zielony:
pojedyncze litery - (x)
sekwencje - (xyz)
poprawny początek/koniec słowa - [x)yzx(yz]
```
"""
if len(context.command.args) == 0:
return context.updated(result=_help)
guess = context.command.args[0]
solver = diffle.Solver(diffle.DICT)
solver.guess(guess)
try:
matches = solver.get_matches()
except diffle.InvalidSyntaxException as e:
return context.updated(result=str(e))
df = pd.DataFrame(matches, columns=['match'])
df.index += 1
df_str = df.to_string(header=False, max_rows=10)
if len(df.index) == 0:
return context.updated(result='0 matches')
else:
match_str = '1 match' if len(df.index) == 1 else f'{len(df.index)} matches'
return context.updated(result=f'{match_str}\n```\n{df_str}\n```')
@command(name='difflanek')
async def _difflanek(context: MessageContext, client: discord.Client) -> MessageContext:
if context.command.raw_args:
params = [p for p in context.command.raw_args.split(' ') if p]
is_polish_word = params[0] == 'pl'
words = params[int(is_polish_word):]
result_all = sorted(filter(lambda x: len(x) >= 3, difflanek.find_solution(words, is_polish_word)))
n = 50
if len(result_all) > n:
result = sorted(set(random.sample(result_all, n)))
else:
result = result_all
dict_size = difflanek.get_total_count()
percentage = format_fraction(100 * len(result_all), dict_size)
message = f'{len(result)}/{len(result_all)} | {percentage}%: {result}'
context = context.updated(result=message[:2000], input=result_all)
return context
else:
return context.updated(result=difflanek.get_help())
@command(name='rand')
async def _rand(context: MessageContext, client: discord.Client) -> MessageContext:
help = '''\
```
!rand <a> [b [c [d ...]]]
!echo a b c d | !rand
```
'''
if context.input is not None and isinstance(context.input, list):
return context.updated(result=random.choice(context.input))
target = (context.result or context.command.raw_args).split()
if target:
return context.updated(result=random.choice(target))
else:
return context.updated(result=help)
@command(name='all')
async def _all(context: MessageContext, client: discord.Client) -> MessageContext:
if context.input is not None and isinstance(context.input, list):
input_str = str(context.input)
if len(input_str) <= DISCORD_MESSAGE_LIMIT:
return context.updated(result=input_str)
else:
with io.BytesIO() as buf:
buf.write(
'\n'.join(context.input).encode('utf8'),
)
buf.seek(0)
context.attachment = discord.File(fp=buf, filename='all.txt')
return context
return context
@command(name='dflocr')
async def _dflocr(context: MessageContext, client: discord.Client) -> MessageContext:
if context.message.reference is None:
return context.updated(result='You need to respond to a message with an image')
referenced_message = await context.message.channel.fetch_message(context.message.reference.message_id)
for attachment in referenced_message.attachments:
ct = attachment.content_type
if ct and ct.startswith('image'):
logger.debug('Found image with type: %s', ct)
image = await attachment.read()
try:
dfl = opencv.get_difflanek(image)
except opencv.DifflanekException:
return context.updated(result='no rectangles found')
else:
return context.updated(result=' '.join(dfl))
return context.updated(result='no images found')