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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
|
%global _empty_manifest_terminate_build 0
Name: python-re-ircbot
Version: 1.6.5
Release: 1
Summary: A simple async irc bot framework with regex command definitions and data permanency
License: MIT License
URL: https://github.com/matheusfillipe/ircbot
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/de/19/68fa02f7d2fbaccd283860e9546e3c827dafa32e3aff964ab690fb00db1d/re-ircbot-1.6.5.tar.gz
BuildArch: noarch
Requires: python3-trio
Requires: python3-cachetools
Requires: python3-trio-asyncio
%description
[](https://pypi.org/project/re-ircbot/)
[](https://mangle.ga/irc)
# re-ircbot, a simple irc bot library
This is a simple irc bot library that uses trio for async callback processing and allows you to
use persistent data based on user's nick and channels. The callbacks for
commands are defined by regular expressions. You might want to take a look on
the examples folder.
## How to install
`pip3 install re-ircbot`
Then take one of the examples and start modifying!
## Basic usage
### Define regex commands
Commands are defines with regexps using a decorator around the callback
function. Everything is async. Either user the utils.regex_cmd for
simplicity or utils.regex_cmd_with_messsage if you need to know info about the
sender and if it is a private chat.
```python
@utils.regex_cmd("^!command$")
def handler(m):
return "hi!"
@utils.regex_cmd("^lol$")
def lol(m):
return "YOU SHALL NOT SAY LOL"
```
The first argument of a handler function will always be a Match object: https://docs.python.org/3/library/re.html#match-objects
```python
@utils.regex_cmd_with_messsage("^who is (.*)$")
def whoami(m, message):
m = m.group(1)
return f"You are {message.sender_nick} and I have no idea who {m} is"
```
With regex_cmd_with_messsage there will be 2 arguments for the handler function which are the Match and the message object. The important parameters of Message are sender_nick, channel, message, is_private.
The callback/handler functions can g a string, Message objects, Color objects or a list of any of those, to be sent back by the bot, a Message object, a ReplyIntent or None to send nothing.
### Define simple argument commands
If all you want is a bot that take simple commands like `!start` `!help` etc... You can use the `utils.arg_command` decorator:
```python
@utils.arg_command("test", "Oh this is just some test", "This command is used for like testing")
def extra(args, message):
return Color("Random", Color.random())
```
Where you can define the command `test` and with that you are also adding it to the help menu(`utils.help_menu`) and defining the message to display for `help test`. This will automatically create the help command. args there is still a regex match object, so the arguments start from 1, like if you call `test a b c` args[1] is a, args[2] is b and so on.
You can also use the `utils.setCommands` function to add commands all together from defined functions by passing in a dict. To change the commands prefix use `utils.setPrefix`. Both setCommands and the arg_command wrapper will use `simplify=True` by default, which means the bot will accept the minimal prefix versions of the defined commands.
Look at the examples to learn more.
### Launch the bot
```python
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.run()
```
### Colors
You can use the Color class from IrcBot.
* Color(text, fg, bg) Colorize text with the fg foreground and bg backdround
* Color.COLORS is the list of number codes for colors
* Color.random() gs a random color
* Color.str string version of the Color. Useful if you want to combine this with other Colors or text
### Data permanency
Data permanency is based on sqlite3. Define a variable like:
```python
people = persistentData("filename.db", "table_name" , ["name", "age", "nickname"])
```
The first two arguments are self explanatory. The last is a list of columns that the table will have and that you will be able to access through people.data, e.g. `people.data[0]["name"]` to see the name of the first registered person. people.data will be a list of dicts with the given keys/columns.
Then pass it to the bot constructor as the tables argument. Notice it takes a list since you might want to have multiple of these.:
```python
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, tables=[people])
```
### Wait for user response
Still continuing the data permanency example, you can now modify the variable on callback functions by either using the push, pop or update methods:
```python
@utils.regex_cmd_with_messsage("^!register (.*) (.*) (.*)$") # name, age, nickname
def handler(omsg, message):
people.push([m.group(1), m.group(2), m.group(3)])
# or
#people.push({"name": m.group(1), "age": m.group(2),"nickname": m.group(3)})
return Message(channel=message.)
@utils.regex_cmd_with_messsage("^!register.*$")
def register(m, message):
return ReplyIntent("Enter your name, age and nickname", lambda msg: handler(original_message, msg))
```
The regexps are matched in the other they are declared and there will be only one callback for each user input, which corresponds to the first defined callback that is matched. Like in this case the handler function will run directly if some user sends "!register name 23 nickname" but the register function won't run. If they were in swapped order then register would just always run.
A ReplyIntent means that the next message this same user sends will be handled with the passed in function. This function will just receive the string message that is sent by the user next. You can specify a different channel, user, by passing in a Message object with the desired parameters for the ReplyIntent first argument.
### Run independently of user input
You can call an async function after the bot connects and join a channel if it was passed to the Bot's constructor. This way you can get the bot respond independently, like send messages even if not command or regexp was sent by a user. To do this, instead of calling `bot.run()` do `bot.runWithCallback(asyncFun)`:
```python
(...)
async def asyncFun(bot):
await bot.sleep(1) # Not necessary, I am just showing you can wait for 1 second like this
await bot.join("#somechannel")
await bot.send_message("Hello guys!!!!")
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.runWithCallback(mainLoop)
```
Take a look at the patternSpam.py example for more details.
### wait_for (event)
You can also use async handlers to await for user input using the bot's method `wait_for`. Here is an example to check and await for user's input:
```python
async def ask(
bot: IrcBot,
nick: str,
question: str,
expected_input=None,
repeat_question=None,
loop: bool = True,
timeout_message: str = "Response timeout!",
):
await bot.send_message(question, nick)
resp = await bot.wait_for("privmsg", nick, timeout=10)
while loop:
if resp:
if expected_input is None or resp.get("text").strip() in expected_input:
break
await bot.send_message(repeat_question if repeat_question else question, nick)
else:
await bot.send_message(timeout_message, nick)
break
resp = await bot.wait_for("privmsg", nick, timeout=600)
return resp.get('text').strip() if resp else None
@utils.arg_command("test")
async def test(bot: IrcBot, args, msg):
nick = msg.nick
resp = await ask(bot, msg.nick, f"{nick}: say yes/no", ["yes", "no"], repeat_question="You must reply with yes or no")
return f"Your answer is: '{resp}'"
```
Notice that awaiting for `resp = await bot.wait_for("privmsg", nick, timeout=10)` will pause the execution until either a matching event from `nick` of type `privmsg` (which means both channel messages or DM's) arrives or the timeout happens.
Here is another example of using wait_for to detect if a user is identified with the nickserv:
``` python
async def is_identified(bot: IrcBot, nick):
global nick_cache
nickserv = "NickServ"
await bot.send_message(f"status {nick}", nickserv)
msg = await bot.wait_for(
"notice",
nickserv,
timeout=5,
cache_ttl=60,
# We need filter because multiple notices from nickserv can come at the same time
# if multiple requests are being made to this function all together
filter_func=lambda m: nick in m["text"],
)
return msg.get("text").strip() == f"{nick} 3 {nick}" if msg else False
```
Here we used a lower timeout because nickserv should reply faster than any user, the `cache_ttl` option will keep results cached for the given time, and `filter_func` must be a callable to be ran against any matched event (even if it is a cached result). The event will be only considered valid `if filter_func` (is True).
### Middlewares
Currently simple middlewares are supported for every event handler (regex or command). A middleware is an async function that will take the bot instance and the Message and this method will run before every handler. You can decide to proceed and call the handler if this method returns `True` or deny it if it returns `False` or `None`. On that case execution will be stopped.
The method used to add a middleware function is: `bot.add_middleware(callback)`.
Here is an example for not letting the bot respond to other bots by checking the "+B" mode flag:
```python
# define some handlers here with utils.arg_commands_with_message
# or utils.regex_commands_with_message or whatever....
async def check_no_bot(bot: IrcBot, message: Message):
await bot.send_raw(f"WHO {message.nick}")
resp = await bot.wait_for("who", message.nick, timeout=10, cache_ttl=10)
if "B" in resp["modes"]:
return False
return True
async def on_connect(bot: IrcBot):
await bot.send_raw(f"MODE {bot.nick} +B")
if __name__ == "__main__":
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, strip_messages=False, use_ssl=SSL)
bot.add_middleware(check_no_bot)
bot.runWithCallback(on_connect)
```
## Tips and tricks (logging, async, etc)
First of all: currently it is only possible to have one bot only per python call. This means you should define only one bot per file.
None, strings, a list of strings and Message can be used on the same places through this library.
* None: Means no message to send, like if you just `return` on some function
* str: If you want to just send a string or f-string
* [str1, str2, ...]: To send multiple messages as separated messages.
* Message: If you want to specify the channel, or send a private message.
* ReplyIntent: If you want to set a callback for the next user message and
establish a dialog.
* Color: To send a colored message.
Everything is asynchronously handled so you might not want to user something
besides the push, pop and update methods for the data permanency functions and
also not try to send messages manually.
Echo bot.
```python
def echo(msg):
if msg == "stop":
return
return ReplyIntent(msg, echo)
@utils.regex_cmd_with_messsage(getcmd("^!include.*$", ACCEPT_PRIVATE_MESSAGES)
def include(m, message):
return ReplyIntent(Message(channel=message.sender_nick, sender_nick=message.sender_nick, message="Hello! echo mode activated"), echo)
```
## FAQ
Don't know how to use re match objects? convert it to a list with: `utils.m2list(args)`
To add a color with a string use `color.str + another_string`
Q. **Why aren't my regex expressions being matched?**
The regex commands you defined last will be matched first. That said, check up
the documentation for python's re module. Also notice that regex expressions do
not work for utils.arg_command or using setCommands, they commands will be
escaped by re.escape.
Q. **Can I use async functions on my callbacks?**
Yes, you can use any of the decorators with async functions as well
Q. **What happens if i send colored messages?**
Currently they will be simply treated as normal text. This bot does not care
about user input colors but it can still send colored messages.
Q. **Why my async callback doesn't work? (or how does it even work)**
Compared to non async callback functions, if you try to use an async callback
like in one of util's wrappers, you will need to pass an extra argument in the
first position that is 'bot', an IrcBot object which represents the current
bot. With this you can use IrcBot's methods within a callback function like for
a command or custom_handler.
Q. **How can I add actions for user, join, part etc.**
Take a look at the utils.custom_handler decorator.
## ROADMAP
For version 2.0:
1. [ ] SASL AUTHENTICATION
2. [x] Handle weird character's on input (Colors, fonts, underlines, glyphs etc)
3. [ ] Convert utils to an actual class that can be used as a context
4. [x] Dcc file transfer
%package -n python3-re-ircbot
Summary: A simple async irc bot framework with regex command definitions and data permanency
Provides: python-re-ircbot
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-re-ircbot
[](https://pypi.org/project/re-ircbot/)
[](https://mangle.ga/irc)
# re-ircbot, a simple irc bot library
This is a simple irc bot library that uses trio for async callback processing and allows you to
use persistent data based on user's nick and channels. The callbacks for
commands are defined by regular expressions. You might want to take a look on
the examples folder.
## How to install
`pip3 install re-ircbot`
Then take one of the examples and start modifying!
## Basic usage
### Define regex commands
Commands are defines with regexps using a decorator around the callback
function. Everything is async. Either user the utils.regex_cmd for
simplicity or utils.regex_cmd_with_messsage if you need to know info about the
sender and if it is a private chat.
```python
@utils.regex_cmd("^!command$")
def handler(m):
return "hi!"
@utils.regex_cmd("^lol$")
def lol(m):
return "YOU SHALL NOT SAY LOL"
```
The first argument of a handler function will always be a Match object: https://docs.python.org/3/library/re.html#match-objects
```python
@utils.regex_cmd_with_messsage("^who is (.*)$")
def whoami(m, message):
m = m.group(1)
return f"You are {message.sender_nick} and I have no idea who {m} is"
```
With regex_cmd_with_messsage there will be 2 arguments for the handler function which are the Match and the message object. The important parameters of Message are sender_nick, channel, message, is_private.
The callback/handler functions can g a string, Message objects, Color objects or a list of any of those, to be sent back by the bot, a Message object, a ReplyIntent or None to send nothing.
### Define simple argument commands
If all you want is a bot that take simple commands like `!start` `!help` etc... You can use the `utils.arg_command` decorator:
```python
@utils.arg_command("test", "Oh this is just some test", "This command is used for like testing")
def extra(args, message):
return Color("Random", Color.random())
```
Where you can define the command `test` and with that you are also adding it to the help menu(`utils.help_menu`) and defining the message to display for `help test`. This will automatically create the help command. args there is still a regex match object, so the arguments start from 1, like if you call `test a b c` args[1] is a, args[2] is b and so on.
You can also use the `utils.setCommands` function to add commands all together from defined functions by passing in a dict. To change the commands prefix use `utils.setPrefix`. Both setCommands and the arg_command wrapper will use `simplify=True` by default, which means the bot will accept the minimal prefix versions of the defined commands.
Look at the examples to learn more.
### Launch the bot
```python
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.run()
```
### Colors
You can use the Color class from IrcBot.
* Color(text, fg, bg) Colorize text with the fg foreground and bg backdround
* Color.COLORS is the list of number codes for colors
* Color.random() gs a random color
* Color.str string version of the Color. Useful if you want to combine this with other Colors or text
### Data permanency
Data permanency is based on sqlite3. Define a variable like:
```python
people = persistentData("filename.db", "table_name" , ["name", "age", "nickname"])
```
The first two arguments are self explanatory. The last is a list of columns that the table will have and that you will be able to access through people.data, e.g. `people.data[0]["name"]` to see the name of the first registered person. people.data will be a list of dicts with the given keys/columns.
Then pass it to the bot constructor as the tables argument. Notice it takes a list since you might want to have multiple of these.:
```python
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, tables=[people])
```
### Wait for user response
Still continuing the data permanency example, you can now modify the variable on callback functions by either using the push, pop or update methods:
```python
@utils.regex_cmd_with_messsage("^!register (.*) (.*) (.*)$") # name, age, nickname
def handler(omsg, message):
people.push([m.group(1), m.group(2), m.group(3)])
# or
#people.push({"name": m.group(1), "age": m.group(2),"nickname": m.group(3)})
return Message(channel=message.)
@utils.regex_cmd_with_messsage("^!register.*$")
def register(m, message):
return ReplyIntent("Enter your name, age and nickname", lambda msg: handler(original_message, msg))
```
The regexps are matched in the other they are declared and there will be only one callback for each user input, which corresponds to the first defined callback that is matched. Like in this case the handler function will run directly if some user sends "!register name 23 nickname" but the register function won't run. If they were in swapped order then register would just always run.
A ReplyIntent means that the next message this same user sends will be handled with the passed in function. This function will just receive the string message that is sent by the user next. You can specify a different channel, user, by passing in a Message object with the desired parameters for the ReplyIntent first argument.
### Run independently of user input
You can call an async function after the bot connects and join a channel if it was passed to the Bot's constructor. This way you can get the bot respond independently, like send messages even if not command or regexp was sent by a user. To do this, instead of calling `bot.run()` do `bot.runWithCallback(asyncFun)`:
```python
(...)
async def asyncFun(bot):
await bot.sleep(1) # Not necessary, I am just showing you can wait for 1 second like this
await bot.join("#somechannel")
await bot.send_message("Hello guys!!!!")
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.runWithCallback(mainLoop)
```
Take a look at the patternSpam.py example for more details.
### wait_for (event)
You can also use async handlers to await for user input using the bot's method `wait_for`. Here is an example to check and await for user's input:
```python
async def ask(
bot: IrcBot,
nick: str,
question: str,
expected_input=None,
repeat_question=None,
loop: bool = True,
timeout_message: str = "Response timeout!",
):
await bot.send_message(question, nick)
resp = await bot.wait_for("privmsg", nick, timeout=10)
while loop:
if resp:
if expected_input is None or resp.get("text").strip() in expected_input:
break
await bot.send_message(repeat_question if repeat_question else question, nick)
else:
await bot.send_message(timeout_message, nick)
break
resp = await bot.wait_for("privmsg", nick, timeout=600)
return resp.get('text').strip() if resp else None
@utils.arg_command("test")
async def test(bot: IrcBot, args, msg):
nick = msg.nick
resp = await ask(bot, msg.nick, f"{nick}: say yes/no", ["yes", "no"], repeat_question="You must reply with yes or no")
return f"Your answer is: '{resp}'"
```
Notice that awaiting for `resp = await bot.wait_for("privmsg", nick, timeout=10)` will pause the execution until either a matching event from `nick` of type `privmsg` (which means both channel messages or DM's) arrives or the timeout happens.
Here is another example of using wait_for to detect if a user is identified with the nickserv:
``` python
async def is_identified(bot: IrcBot, nick):
global nick_cache
nickserv = "NickServ"
await bot.send_message(f"status {nick}", nickserv)
msg = await bot.wait_for(
"notice",
nickserv,
timeout=5,
cache_ttl=60,
# We need filter because multiple notices from nickserv can come at the same time
# if multiple requests are being made to this function all together
filter_func=lambda m: nick in m["text"],
)
return msg.get("text").strip() == f"{nick} 3 {nick}" if msg else False
```
Here we used a lower timeout because nickserv should reply faster than any user, the `cache_ttl` option will keep results cached for the given time, and `filter_func` must be a callable to be ran against any matched event (even if it is a cached result). The event will be only considered valid `if filter_func` (is True).
### Middlewares
Currently simple middlewares are supported for every event handler (regex or command). A middleware is an async function that will take the bot instance and the Message and this method will run before every handler. You can decide to proceed and call the handler if this method returns `True` or deny it if it returns `False` or `None`. On that case execution will be stopped.
The method used to add a middleware function is: `bot.add_middleware(callback)`.
Here is an example for not letting the bot respond to other bots by checking the "+B" mode flag:
```python
# define some handlers here with utils.arg_commands_with_message
# or utils.regex_commands_with_message or whatever....
async def check_no_bot(bot: IrcBot, message: Message):
await bot.send_raw(f"WHO {message.nick}")
resp = await bot.wait_for("who", message.nick, timeout=10, cache_ttl=10)
if "B" in resp["modes"]:
return False
return True
async def on_connect(bot: IrcBot):
await bot.send_raw(f"MODE {bot.nick} +B")
if __name__ == "__main__":
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, strip_messages=False, use_ssl=SSL)
bot.add_middleware(check_no_bot)
bot.runWithCallback(on_connect)
```
## Tips and tricks (logging, async, etc)
First of all: currently it is only possible to have one bot only per python call. This means you should define only one bot per file.
None, strings, a list of strings and Message can be used on the same places through this library.
* None: Means no message to send, like if you just `return` on some function
* str: If you want to just send a string or f-string
* [str1, str2, ...]: To send multiple messages as separated messages.
* Message: If you want to specify the channel, or send a private message.
* ReplyIntent: If you want to set a callback for the next user message and
establish a dialog.
* Color: To send a colored message.
Everything is asynchronously handled so you might not want to user something
besides the push, pop and update methods for the data permanency functions and
also not try to send messages manually.
Echo bot.
```python
def echo(msg):
if msg == "stop":
return
return ReplyIntent(msg, echo)
@utils.regex_cmd_with_messsage(getcmd("^!include.*$", ACCEPT_PRIVATE_MESSAGES)
def include(m, message):
return ReplyIntent(Message(channel=message.sender_nick, sender_nick=message.sender_nick, message="Hello! echo mode activated"), echo)
```
## FAQ
Don't know how to use re match objects? convert it to a list with: `utils.m2list(args)`
To add a color with a string use `color.str + another_string`
Q. **Why aren't my regex expressions being matched?**
The regex commands you defined last will be matched first. That said, check up
the documentation for python's re module. Also notice that regex expressions do
not work for utils.arg_command or using setCommands, they commands will be
escaped by re.escape.
Q. **Can I use async functions on my callbacks?**
Yes, you can use any of the decorators with async functions as well
Q. **What happens if i send colored messages?**
Currently they will be simply treated as normal text. This bot does not care
about user input colors but it can still send colored messages.
Q. **Why my async callback doesn't work? (or how does it even work)**
Compared to non async callback functions, if you try to use an async callback
like in one of util's wrappers, you will need to pass an extra argument in the
first position that is 'bot', an IrcBot object which represents the current
bot. With this you can use IrcBot's methods within a callback function like for
a command or custom_handler.
Q. **How can I add actions for user, join, part etc.**
Take a look at the utils.custom_handler decorator.
## ROADMAP
For version 2.0:
1. [ ] SASL AUTHENTICATION
2. [x] Handle weird character's on input (Colors, fonts, underlines, glyphs etc)
3. [ ] Convert utils to an actual class that can be used as a context
4. [x] Dcc file transfer
%package help
Summary: Development documents and examples for re-ircbot
Provides: python3-re-ircbot-doc
%description help
[](https://pypi.org/project/re-ircbot/)
[](https://mangle.ga/irc)
# re-ircbot, a simple irc bot library
This is a simple irc bot library that uses trio for async callback processing and allows you to
use persistent data based on user's nick and channels. The callbacks for
commands are defined by regular expressions. You might want to take a look on
the examples folder.
## How to install
`pip3 install re-ircbot`
Then take one of the examples and start modifying!
## Basic usage
### Define regex commands
Commands are defines with regexps using a decorator around the callback
function. Everything is async. Either user the utils.regex_cmd for
simplicity or utils.regex_cmd_with_messsage if you need to know info about the
sender and if it is a private chat.
```python
@utils.regex_cmd("^!command$")
def handler(m):
return "hi!"
@utils.regex_cmd("^lol$")
def lol(m):
return "YOU SHALL NOT SAY LOL"
```
The first argument of a handler function will always be a Match object: https://docs.python.org/3/library/re.html#match-objects
```python
@utils.regex_cmd_with_messsage("^who is (.*)$")
def whoami(m, message):
m = m.group(1)
return f"You are {message.sender_nick} and I have no idea who {m} is"
```
With regex_cmd_with_messsage there will be 2 arguments for the handler function which are the Match and the message object. The important parameters of Message are sender_nick, channel, message, is_private.
The callback/handler functions can g a string, Message objects, Color objects or a list of any of those, to be sent back by the bot, a Message object, a ReplyIntent or None to send nothing.
### Define simple argument commands
If all you want is a bot that take simple commands like `!start` `!help` etc... You can use the `utils.arg_command` decorator:
```python
@utils.arg_command("test", "Oh this is just some test", "This command is used for like testing")
def extra(args, message):
return Color("Random", Color.random())
```
Where you can define the command `test` and with that you are also adding it to the help menu(`utils.help_menu`) and defining the message to display for `help test`. This will automatically create the help command. args there is still a regex match object, so the arguments start from 1, like if you call `test a b c` args[1] is a, args[2] is b and so on.
You can also use the `utils.setCommands` function to add commands all together from defined functions by passing in a dict. To change the commands prefix use `utils.setPrefix`. Both setCommands and the arg_command wrapper will use `simplify=True` by default, which means the bot will accept the minimal prefix versions of the defined commands.
Look at the examples to learn more.
### Launch the bot
```python
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.run()
```
### Colors
You can use the Color class from IrcBot.
* Color(text, fg, bg) Colorize text with the fg foreground and bg backdround
* Color.COLORS is the list of number codes for colors
* Color.random() gs a random color
* Color.str string version of the Color. Useful if you want to combine this with other Colors or text
### Data permanency
Data permanency is based on sqlite3. Define a variable like:
```python
people = persistentData("filename.db", "table_name" , ["name", "age", "nickname"])
```
The first two arguments are self explanatory. The last is a list of columns that the table will have and that you will be able to access through people.data, e.g. `people.data[0]["name"]` to see the name of the first registered person. people.data will be a list of dicts with the given keys/columns.
Then pass it to the bot constructor as the tables argument. Notice it takes a list since you might want to have multiple of these.:
```python
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, tables=[people])
```
### Wait for user response
Still continuing the data permanency example, you can now modify the variable on callback functions by either using the push, pop or update methods:
```python
@utils.regex_cmd_with_messsage("^!register (.*) (.*) (.*)$") # name, age, nickname
def handler(omsg, message):
people.push([m.group(1), m.group(2), m.group(3)])
# or
#people.push({"name": m.group(1), "age": m.group(2),"nickname": m.group(3)})
return Message(channel=message.)
@utils.regex_cmd_with_messsage("^!register.*$")
def register(m, message):
return ReplyIntent("Enter your name, age and nickname", lambda msg: handler(original_message, msg))
```
The regexps are matched in the other they are declared and there will be only one callback for each user input, which corresponds to the first defined callback that is matched. Like in this case the handler function will run directly if some user sends "!register name 23 nickname" but the register function won't run. If they were in swapped order then register would just always run.
A ReplyIntent means that the next message this same user sends will be handled with the passed in function. This function will just receive the string message that is sent by the user next. You can specify a different channel, user, by passing in a Message object with the desired parameters for the ReplyIntent first argument.
### Run independently of user input
You can call an async function after the bot connects and join a channel if it was passed to the Bot's constructor. This way you can get the bot respond independently, like send messages even if not command or regexp was sent by a user. To do this, instead of calling `bot.run()` do `bot.runWithCallback(asyncFun)`:
```python
(...)
async def asyncFun(bot):
await bot.sleep(1) # Not necessary, I am just showing you can wait for 1 second like this
await bot.join("#somechannel")
await bot.send_message("Hello guys!!!!")
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD)
bot.runWithCallback(mainLoop)
```
Take a look at the patternSpam.py example for more details.
### wait_for (event)
You can also use async handlers to await for user input using the bot's method `wait_for`. Here is an example to check and await for user's input:
```python
async def ask(
bot: IrcBot,
nick: str,
question: str,
expected_input=None,
repeat_question=None,
loop: bool = True,
timeout_message: str = "Response timeout!",
):
await bot.send_message(question, nick)
resp = await bot.wait_for("privmsg", nick, timeout=10)
while loop:
if resp:
if expected_input is None or resp.get("text").strip() in expected_input:
break
await bot.send_message(repeat_question if repeat_question else question, nick)
else:
await bot.send_message(timeout_message, nick)
break
resp = await bot.wait_for("privmsg", nick, timeout=600)
return resp.get('text').strip() if resp else None
@utils.arg_command("test")
async def test(bot: IrcBot, args, msg):
nick = msg.nick
resp = await ask(bot, msg.nick, f"{nick}: say yes/no", ["yes", "no"], repeat_question="You must reply with yes or no")
return f"Your answer is: '{resp}'"
```
Notice that awaiting for `resp = await bot.wait_for("privmsg", nick, timeout=10)` will pause the execution until either a matching event from `nick` of type `privmsg` (which means both channel messages or DM's) arrives or the timeout happens.
Here is another example of using wait_for to detect if a user is identified with the nickserv:
``` python
async def is_identified(bot: IrcBot, nick):
global nick_cache
nickserv = "NickServ"
await bot.send_message(f"status {nick}", nickserv)
msg = await bot.wait_for(
"notice",
nickserv,
timeout=5,
cache_ttl=60,
# We need filter because multiple notices from nickserv can come at the same time
# if multiple requests are being made to this function all together
filter_func=lambda m: nick in m["text"],
)
return msg.get("text").strip() == f"{nick} 3 {nick}" if msg else False
```
Here we used a lower timeout because nickserv should reply faster than any user, the `cache_ttl` option will keep results cached for the given time, and `filter_func` must be a callable to be ran against any matched event (even if it is a cached result). The event will be only considered valid `if filter_func` (is True).
### Middlewares
Currently simple middlewares are supported for every event handler (regex or command). A middleware is an async function that will take the bot instance and the Message and this method will run before every handler. You can decide to proceed and call the handler if this method returns `True` or deny it if it returns `False` or `None`. On that case execution will be stopped.
The method used to add a middleware function is: `bot.add_middleware(callback)`.
Here is an example for not letting the bot respond to other bots by checking the "+B" mode flag:
```python
# define some handlers here with utils.arg_commands_with_message
# or utils.regex_commands_with_message or whatever....
async def check_no_bot(bot: IrcBot, message: Message):
await bot.send_raw(f"WHO {message.nick}")
resp = await bot.wait_for("who", message.nick, timeout=10, cache_ttl=10)
if "B" in resp["modes"]:
return False
return True
async def on_connect(bot: IrcBot):
await bot.send_raw(f"MODE {bot.nick} +B")
if __name__ == "__main__":
utils.setLogging(LEVEL, LOGFILE)
bot = IrcBot(HOST, PORT, NICK, CHANNELS, PASSWORD, strip_messages=False, use_ssl=SSL)
bot.add_middleware(check_no_bot)
bot.runWithCallback(on_connect)
```
## Tips and tricks (logging, async, etc)
First of all: currently it is only possible to have one bot only per python call. This means you should define only one bot per file.
None, strings, a list of strings and Message can be used on the same places through this library.
* None: Means no message to send, like if you just `return` on some function
* str: If you want to just send a string or f-string
* [str1, str2, ...]: To send multiple messages as separated messages.
* Message: If you want to specify the channel, or send a private message.
* ReplyIntent: If you want to set a callback for the next user message and
establish a dialog.
* Color: To send a colored message.
Everything is asynchronously handled so you might not want to user something
besides the push, pop and update methods for the data permanency functions and
also not try to send messages manually.
Echo bot.
```python
def echo(msg):
if msg == "stop":
return
return ReplyIntent(msg, echo)
@utils.regex_cmd_with_messsage(getcmd("^!include.*$", ACCEPT_PRIVATE_MESSAGES)
def include(m, message):
return ReplyIntent(Message(channel=message.sender_nick, sender_nick=message.sender_nick, message="Hello! echo mode activated"), echo)
```
## FAQ
Don't know how to use re match objects? convert it to a list with: `utils.m2list(args)`
To add a color with a string use `color.str + another_string`
Q. **Why aren't my regex expressions being matched?**
The regex commands you defined last will be matched first. That said, check up
the documentation for python's re module. Also notice that regex expressions do
not work for utils.arg_command or using setCommands, they commands will be
escaped by re.escape.
Q. **Can I use async functions on my callbacks?**
Yes, you can use any of the decorators with async functions as well
Q. **What happens if i send colored messages?**
Currently they will be simply treated as normal text. This bot does not care
about user input colors but it can still send colored messages.
Q. **Why my async callback doesn't work? (or how does it even work)**
Compared to non async callback functions, if you try to use an async callback
like in one of util's wrappers, you will need to pass an extra argument in the
first position that is 'bot', an IrcBot object which represents the current
bot. With this you can use IrcBot's methods within a callback function like for
a command or custom_handler.
Q. **How can I add actions for user, join, part etc.**
Take a look at the utils.custom_handler decorator.
## ROADMAP
For version 2.0:
1. [ ] SASL AUTHENTICATION
2. [x] Handle weird character's on input (Colors, fonts, underlines, glyphs etc)
3. [ ] Convert utils to an actual class that can be used as a context
4. [x] Dcc file transfer
%prep
%autosetup -n re-ircbot-1.6.5
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-re-ircbot -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 1.6.5-1
- Package Spec generated
|