~aleteoryx/ntalk

ref: d100582bea7c0ff7578a804006ae504ae9992352 ntalk/dee.py -rwxr-xr-x 6.2 KiB
d100582b — glenda plan 9 client! 14 days ago
                                                                                
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
#!/bin/env python

import botlib
from time import sleep
from random import randint, choice, shuffle
import re
from copy import copy

NAME = "dee"
MAN = 'https://aleteoryx.me/deeman.md'

### "AI" ###

def send(msg):
	global NAME
	botlib.send(f'{NAME}: {msg}')

def act(what):
	global NAME
	botlib.send(f'{NAME} {msg}')

def help(nick):
	global MAN
	send(f'{nick}, my help is at {MAN}')

last_cmd = ('dee, roll d6', ['dee,', 'roll', 'd6'], ['dee', 'roll', 'd6'])

def handle_msg(nick, line, words, pwords):
	global NAME, MAN, last_cmd

	print(nick, line, words, pwords)
	og_data = copy(line), copy(words), copy(pwords)

	if not botlib.strip_direct_address(NAME, [], words, pwords):
		return
	
	cmd = pwords[0]

	if cmd == 'again':
		return handle_msg(nick, *map(copy, last_cmd))
	else:
		last_cmd = og_data

	pwords.pop(0)
	words.pop(0)

	if cmd == 'help':
		help(nick)
	elif cmd not in ('roll', 'pick', 'shuf', 'shuffle', 'do', 'don\'t', 'should', 'are'):
		send(f'{nick}, I don\'t know how to do that! My help is at {MAN}')
	elif len(pwords) < 1:
		send(f'{nick}, {cmd} what?')
	elif cmd == 'roll':
		dice(nick, words, pwords)
	elif cmd == 'pick':
		pick(nick, words, pwords)
	elif cmd in ('shuf', 'shuffle'):
		shuf(nick, words, pwords)
	elif cmd == 'do':
		if pwords[0].lower() == 'i':
			do_i(nick, words[1:], pwords[1:])
		elif pwords[:2] == ['you', 'think']:
			dyt(nick)
		else:
			send(f'{nick}, do what?')
	elif cmd == 'should':
		if pwords[0].lower() == 'i':
			do_i(nick, words[1:], pwords[1:])
		else:
			dyt(nick)
	elif cmd == 'don\'t':
		if pwords[0].lower() == 'i':
			dyt(nick)
		elif pwords[:2] == ['you', 'think']:
			dyt(nick)
		else:
			send(f'{nick}, don\'t what?')
	elif cmd == 'are':
		dyt(nick)
		


def handle_action(line, words, pwords):
	pass


### CHOICES, CHOICES ###

def pick(nick, words, pwords, ignore=['or']):
	formats = [
		'my sources tell me {} is the best option.',
		'how about {}?',
		'I\'ve got a good feeling about {}.',
		'if I *had* to pick, I\'d say {}.',
		'signs point to {}.',
		'{}!',
		'try {}... if you dare!',
		'a terrible fate will befall you! ...unless you pick {}!',
		'after extensive deliberation, I think {} is the only option',
		'nothing ventured, nothing gained! try {}.',
	]

	terms = botlib.split_list(words, pwords, ignore)
	picked = " ".join(choice(terms)).rstrip('?')
	reply = choice(formats).format(picked)
	send(f'{nick}, {reply}')

def shuf(nick, words, pwords):
	terms = botlib.split_list(words, pwords, ['and'])
	shuffle(terms)
	send(f'{nick}: {", ".join(map(" ".join, terms))}')

def do_i(nick, words, pwords):
	if 'or' not in pwords:
		dyt(nick)
	else:
		for word in words:
			if word.endswith(','):
				pick(nick, words, pwords, ['or', 'do', 'should', 'i'])
				break
		else:
			dyt(nick)

def dyt(nick):
	replies = [
		'yes',
		'yeah!',
		'absolutely!',
		'sure',
		'signs point to yes',
		'totally!',
		'no',
		'nah',
		'signs point to no',
		'never in a million years!',
		'maybe',
		'dunno',
		'not sure',
		'ask again later'
	]

	send(f'{nick}, {choice(replies)}')


### DICE ###

class DiceParseException(Exception):
	pass

def parse_number(word):
	return int(word)
		
	
'''
dice = 'then'? spec offset?

spec = 'another' num? 'more'?
spec = 'another'? num? 'more'? size
spec = 'another'? num 'more'

size = 'd' num

offset = op num
op = 'plus' | 'minus' | '+' | '-'

num = int() or human-readable number
'''

last_dice = 6
last_line = ['1', 'd6']
def parse_dice(nick, words):
	global last_dice, last_line
	
	if words[0] == 'then':
		words.pop(0)
		if len(words) == 0:
			return 0, 0, 0
	
	if words[0] == 'again':
		return parse_dice(nick, last_line)
	else:
		last_line = copy(words)
	
	has_another = False
	if words[0] == 'another':
		has_another = True
		words.pop(0)
		if len(words) == 0:
			return 1, last_dice, 0

	can_more = True
	try:
		count = parse_number(words[0])
		words.pop(0)
	except (IndexError, ValueError):
		count = 1
		can_more = False
	
	if len(words) == 0:
		if has_another:
			return count, last_dice, 0
		else:
			raise DiceParseException(f'roll {count} what?')
	
	if d := re.match('d([0-9]+|[A-Za-z]+)', words[0]):
		try:
			last_dice = parse_number(d.group(1))
		except (IndexError, ValueError):
			raise DiceParseException('I don\'t have any dice like that!')
		words.pop(0)
		if len(words) > 0 and words[0] == 'more':
			words.pop(0)
		if len(words) == 0:
			return count, last_dice, 0
	elif words[0] == 'more':
		if not can_more:
			raise DiceParseException(f'roll how many more d{last_dice}?')
		words.pop(0)
		if len(words) == 0:
			return count, last_dice, 0

	if words[0] in ('plus', 'add', '+', 'minus', 'sub', 'subtract', '-'):
		try:
			offset = parse_number(words[1])
		except (IndexError, ValueError):
			raise DiceParseException(f'roll {count} d{last_dice} {words[0]} what?')
		
		if words[0] not in ('plus', 'add', '+'):
			offset *= -1
	
		return count, last_dice, offset

	raise DiceParseException(f'I don\'t know how to roll that!')

def dice(nick, words, pwords):
	global MAN

	result = nick

	terms = botlib.split_list(words, pwords, ['and'])
	print(f'{terms=}')
	for term in terms:
		try:
			count, size, offset = parse_dice(nick, term)
		except DiceParseException as e:
			send(f'{nick}, {e.args[0]} See {MAN} for syntax.')
			return

		if count == 0:
			continue
		if count > 50:
			send(f'{nick}, roll them yourself!')
			return

		rollsum = offset

		# string formatting
		if count == 1:
			result += f', 1 d{size}'
		else:
			result += f', {count} d{size}s'
		if offset > 0:
			offset = f' + {offset}'
		elif offset < 0:
			offset = f' - {-offset}'
		else:
			offset = ''
		result += f'{offset}:'

		for i in range(count):
			if size < 1:
				roll = size
			else:
				roll = randint(1, size)
			rollsum += roll
			if i > 0:
				result += f' + {roll}'
			else:
				result += f' {roll}'
		
		if count > 1 or offset != '':
			result += f'{offset} = {rollsum}'
		
		if size == 0:
			result += ', obviously'
	
	if result == nick:
		send(f'{nick}, done, i guess')
	elif len(result) < 100:
		send(result)
	else:
		send(f'{nick}, roll them yourself!')


### BOOT ###

botlib.parse_args()

while True:
	sleep(5)
	botlib.writeln(f"SKIP {botlib.lastmsg}")
	lines = botlib.readmany()

	for line in lines:
		print(line)
		botlib.handle_line(line, NAME, handle_msg, handle_action)