-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartbot.py
executable file
·373 lines (351 loc) · 12.5 KB
/
startbot.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
#! /usr/bin/env python3
'''
shadowbot - an IRC bot to talk to another IRC bot
Copyright (C) 2022 Chase Phelps
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 <https://www.gnu.org/licenses/>.
'''
###
# startbot.py
# This is a driver script for shadowirc.py and bot.py
##
# Author: Chase LP
###
import sys, time
from irchandler import IRCHandler
from bot import ShadowThread
passfilename = 'pass'
if __name__ == '__main__' and len(sys.argv) == 2:
passfilename = sys.argv[1]
# The ShadowThread object we will interact with
thread = None
# Read the user/pass, connect to irc, instantiate the thread object
with open(passfilename) as infile:
lines = infile.readlines()
# The username is by itself on the first line
username = lines[0].strip()
# The password is by itself on the second line
userpass = lines[1].strip()
# Then the bot nick
lambnick = lines[2].strip()
# The server is next
server = lines[3].strip()
# Connect to irc with the IRCHandler
# The IRCHandler object is given to the ShadowThread constructor
print('| Connecting as user ' + username)
thread = ShadowThread(IRCHandler( server=server,
port=6667,
botnick=username,
botpass=userpass
),
lambnick)
del username, userpass, lambnick, server
# These are methods of the ShadowThread object that are for internal use only
# These methods should not be called in the printloop method
unavailfuncs = ['islambmsg',
'getlambmsg',
'sleepreceive',
'awaitresponse',
'cityrank',
'walkpath',
'gotoloc',
'printloop',
'handlecombat',
'invflush',
'setlambbot',
'colorprint',
'print',
'togglecolors'
]
# Get a list of available functions
# This is used for printing available functions
# and confirming that a valid function is selected (when changed)
availfuncs = []
# Go through the dir() of the ShadowThread object
for func in dir(thread):
# Don't take any attribute or method that begins with an underscore
if func.startswith('_'):
pass
# Only take methods that can be called
elif not callable(getattr(thread,func)):
pass
# These methods are for internal use only
elif func in unavailfuncs:
pass
# This is a user-defined method for the printloop method
else:
availfuncs.append(func)
# The previous recipient of a private message from this bot
# Used to ease sending repeated manual messages to some nick
lastrecipient = ''
def botcmdmenu():
while True:
print(' ___')
print('| These commands will be ran before the function loop starts')
print('| Each either:')
print('| Issue a command to the Lambbot, e.g., \"#cast berzerk\"')
print('| Send a player a message, e.g., \"msg nick a message\"')
print('| Sleep for about some time, e.g., \"sleep(30)\"')
print('|')
print('| Current commands: ' +str(thread.precmds))
print('| 1) Add a command')
if len(thread.precmds) > 0:
print('| 2) Remove a command')
print('| 3) Clear commands')
print('| 0) Return to bot menu')
response = input('| Enter your selection: ')
if response == '1':
newcmd = input('| Enter a new command: ')
if newcmd != '':
thread.precmds.append(newcmd)
elif response == '2' and len(thread.precmds) > 0:
delcmd = input('| Enter the command to remove: ')
try:
thread.precmds.remove(delcmd)
except Exception as e:
print('| Exception: ' + str(e))
elif response == '3' and len(thread.precmds) > 0:
thread.precmds = []
elif response == '0':
break
else:
time.sleep(1)
def botescortmenu():
while True:
print(' ___')
print('| Escort options')
print('| Escorting: ' + thread.escortnick)
print('| 1) Set escort nick')
print('| 2) Set prohibited commands for the \"docmd\" command')
print('| 3) Accept configuration')
if thread.escortcasts:
print('| 4) Set that your escort can\'t cast spells')
else:
print('| 4) Set that your escort can cast spells')
print('| 0) Cancel escort')
response = input('| Enter your selection: ')
if response == '1':
thread.escortnick = input('| Enter the nick to escort: ')
elif response == '2':
while True:
print('| Block when any word is matched in an escorted players docmd')
print('| Prohibited words: ' + str(thread.badcmds))
print('|')
print('| Short+long forms of many commands are prohibited by default')
print('| Commands affecting our player are also prohibited')
print('|')
print('| 1) Add prohibited word (must match a word of the command)')
print('| 2) Remove word from prohibited list')
print('| 0) Return to escort options')
response = input('| Enter your selection: ')
if response == '1':
response = input('| Enter new prohibited word: ')
if response != '':
thread.badcmds.append(response)
elif response == '2':
response = input('| Enter word to remove: ')
try:
thread.badcmds.remove(response)
except Exception as e:
print('Exception: ' + str(e))
elif response == '0':
break
else:
time.sleep(1)
elif response == '3':
if thread.escortnick != '':
return 'escort'
return None
elif response == '4':
if thread.escortcasts: thread.escortcasts = False
else: thread.escortcasts = True
elif response == '0':
return None
else:
time.sleep(1)
def travelmenu():
while True:
print(' ___')
print('| Travel and combat options:')
print('| 1) Set word to say on \'Meet\' events (' + str(thread.meetsay) + ')')
print('| 2) Set number of bums needed to kill (' + str(thread.bumsleft) +')')
if thread.attacklow:
print('| 3) Set attack priority to high levels first')
else:
print('| 3) Set attack priority to low levels first (the default)')
if thread.cancast:
print('| 4) Disable casting (teleport + calm + heal)')
else:
print('| 4) Enable casting (teleport + calm + heal)')
print('| 0) Return to the bot menu')
response = input('| Enter your selection: ')
if response == '1':
newword = input('| Enter what the bot says on meet: ')
thread.meetsay = newword
elif response == '2':
print(' ___')
print('| Currently need to kill ' + str(thread.bumsleft) + ' more bum',end='')
if thread.bumsleft != 1: print('s',end='')
newval = input('\n| Enter the number of bums to kill: ')
try:
newval = int(newval)
if newval < 0:
newval = 0
thread.bumsleft = newval
except Exception as e:
print('| Exception: ' + str(e))
elif response == '3':
if thread.attacklow: thread.attacklow = False
else: thread.attacklow = True
elif response == '4':
if thread.cancast: thread.cancast = False
else: thread.cancast = True
elif response == '0':
break
else:
time.sleep(1)
def botmenu():
while True:
print(' ___')
print('| Bot (' + thread.irc.username + ') Configuration:')
print('| 1) Set the Lamb bot nick (' + thread.lambbot + ')')
print('| 2) Set the stop value for selling / pushing items to bank (' + str(thread.invstop) +')')
print('| 3) Set travel and combat options')
print('| 4) Set escort options')
print('| 5) Set command list to run before function loop')
print('| 6) Change the function loop (' + str(thread.doloop) + ')')
if thread.doloop is not None:
print('| 7) Clear current function loop')
print('| 0) Return to the main menu')
response = input('| Enter your selection: ')
if response == '1':
newnick = input('| Enter the Lamb bot\'s nick: ')
thread.setlambbot(newnick)
elif response == '2':
print('| Currently the stop value is ' + str(thread.invstop))
print('| Less than 1 indicates to not shed inventory')
print('| A positive value indicates the highest number of inventory to sell')
newval = input('| Enter the inventory stop position: ')
try:
newval = int(newval)
if newval < 0:
newval = 0
thread.invstop = newval
except Exception as e:
print('| Exception: ' + str(e))
elif response == '3':
travelmenu()
elif response == '4':
botescortmenu()
elif response == '5':
botcmdmenu()
elif response == '6':
print(' ___')
print('| Bot function is ' + str(thread.doloop))
print('| Available functions are ' + ', '.join(availfuncs))
newfunc = input('| Enter a function name: ')
if newfunc in availfuncs:
if newfunc == 'escort':
newfunc = botescortmenu()
thread.doloop = newfunc
else:
print('| Invalid function name')
elif response == '7' and thread.doloop is not None:
thread.doloop = None
elif response == '0':
break
else:
time.sleep(1)
def ircmenu():
global lastrecipient
while True:
print(' ___')
print('| IRC Commands:')
print('| 1) Join a channel')
print('| 2) Send a message to a nick or channel')
print('| 3) Send a message to ' + thread.lambbot)
if lastrecipient != '':
print('| 4) Send a message to ' + lastrecipient)
print('| 0) Return to the main menu')
response = input('| Enter your selection: ')
if response == '1':
chan = input('| Enter the channel name to join: ')
if chan != '1':
thread.irc.joinchan(chan)
elif response == '2':
recipient = input('| Enter the recipient: ')
if recipient == '2':
continue
if recipient != thread.lambbot and recipient != '':
lastrecipient = recipient
msg = input('| Enter the message: ')
if msg != '' and msg != '2':
thread.irc.privmsg(recipient, msg, delay=0)
elif response == '3':
msg = input('| Enter the message: ')
if msg != '' and msg != '3':
thread.irc.privmsg(thread.lambbot, msg, delay=0)
elif response == '4' and lastrecipient != '':
msg = input('| Enter the message: ')
if msg != '4':
thread.irc.privmsg(lastrecipient, msg, delay=0)
elif response == '0':
break
else:
time.sleep(1)
def mainmenu():
while True:
print(' ___')
print('| Main Menu:')
print('| 1) Bot configuration')
print('| 2) IRC commands')
print('| 3) Quit')
print(f'| 4) Toggle colors ({thread.colors})')
print('| 5) Hide menu')
response = input('| Enter your selection: ')
if response == '1':
botmenu()
elif response == '2':
ircmenu()
elif response == '3':
print(' ___')
print('| Quit')
print('| 1) Allow thread to finish its current activity before exit')
print('| 2) More immediately abort thread and quit')
print('| (anything else to cancel)')
response = input('| Enter your selection: ')
if response == '1':
thread.doloop = None
thread.softquit = True
print('| Finishing current activity, joining thread . . .')
thread.th.join()
print('| Goodbye')
break
if response == '2':
print('| Aborting current activity, joining thread . . .')
thread.doloop = None
thread.doquit = True
#time.sleep(2)
thread.th.join()
print('| Goodbye')
break
elif response == '4':
thread.togglecolors()
elif response == '5':
print(' ___')
input('| Hiding menu until the enter key is pressed . . .\n')
else:
time.sleep(1)
print('\n _____\n| shadowbot Copyright (C) 2022 Chase Phelps\n' + \
'| This program comes with ABSOLUTELY NO WARRANTY.\n' + \
'| This is free software, and you are welcome to redistribute it under certain conditions\n _____')
# Begin the driver loop
mainmenu()