-
Notifications
You must be signed in to change notification settings - Fork 5
/
make-bot-chat.py
executable file
·100 lines (84 loc) · 3.23 KB
/
make-bot-chat.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
#!/usr/bin/env python3
# “NikkyBot”
# Copyright ©2012-2016 Travis Evans
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import functools
import textwrap
from sys import argv, exit
from time import sleep
import nikkyai
import personalitiesrc
NUMBER_OF_ROUNDS = 50
real_print = print
@functools.wraps(real_print)
def print(s, *args, **kwargs):
"""Print arbitrary strings which may contain bad encoding without
raising a UnicodeDecodeError; just do the best we can and move on.
"""
s = str(s).encode('utf-8', errors='backslashreplace').decode()
real_print(s, *args, **kwargs)
def usage_exit():
print('Usage: {} personality1 personality2'.format(argv[0]))
print('\n\nPersonalities:\n')
print(', '.join(sorted(personalities)))
exit(2)
personalities = personalitiesrc.personalities
if len(argv) != 3:
usage_exit()
nick1, nick2 = argv[1], argv[2]
if nick1 != 'nikky' and nick1 not in personalities:
usage_exit()
elif nick2 != 'nikky' and nick2 not in personalities:
usage_exit()
if nick1 == nick2:
tag1, tag2 = '1', '2'
else:
tag1 = tag2 = None
bot1 = nikkyai.NikkyAI(recurse_limit=10, debug=False, max_lf_l=10, max_lf_r=10,
personality=nick1, id='*botchat*', search_time=.1)
bot1.clear_last_replies()
bot2 = nikkyai.NikkyAI(recurse_limit=10, debug=False, max_lf_l=10, max_lf_r=10,
personality=nick2, id='*botchat*', search_time=.1)
# No need to also clear bot2's last replies list because it runs off of the same
# ID and therefore shares the list with bot1
tw = textwrap.TextWrapper(subsequent_indent=' '*20, expand_tabs=True, width=80)
def get_response(bot, nick, target_nick, msg):
msg = '<{}> {}'.format(target_nick, msg)
reply = bot.reply(msg)
return reply
def format_response(nick, msg, tag=None):
msg = msg.split('\n')
if nick == 'nikky':
display_nick = nick + 'bot'
else:
display_nick = nick + '-bot'
if tag is not None:
display_nick = display_nick + '-' + tag
formatted_msg = []
for line in msg:
if line.startswith('\x01ACTION ') and line.endswith('\x01'):
line = line[8:-1]
tw.initial_indent = ' * {} '.format(display_nick)
else:
tw.initial_indent = '{:<20}'.format('<' + display_nick + '>')
formatted_msg.append(tw.fill(line))
formatted_msg = '\n'.join(formatted_msg)
return formatted_msg
response = ''
for i in range(NUMBER_OF_ROUNDS):
response = get_response(bot1, nick1, nick2, response)
print(format_response(nick1, response, tag1) + '\n')
response = get_response(bot2, nick2, nick1, response)
print(format_response(nick2, response, tag2) + '\n')