Skip to content

Commit f1a1bf8

Browse files
committed
Brought over some changes from chipbot
1 parent 3579219 commit f1a1bf8

File tree

2 files changed

+130
-10
lines changed

2 files changed

+130
-10
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ Incrementing Points (Phrase)
4040

4141
*result: "any arbitrary words" will get +1 point*
4242

43+
Incrementing Karma/Points (Inline)
44+
- any++ arbitrary++ [user]++
45+
46+
*result: "any" will get +1 point*
47+
48+
*result: "arbitrary" will get +1 point*
49+
50+
*result: [user] will get +1 karma*
51+
4352
Checking karma/points rank
4453
- rank [user]
4554
- [user]~~
@@ -53,27 +62,53 @@ Stats are kept track of for each user that sends messages to the channel. That i
5362

5463
Checking stats
5564
- stats
65+
- stats [user]
5666

57-
*result: information about the top 5 speakers in the channel*
67+
*result: information about the top 5 speakers in the channel, or information on specific user*
5868

5969
#### Quality
6070

6171
Quality is calculated by dividing the number of times a user speaks by their rank. This gives a metric for the "quality" of a user's posts (i.e. what percent of them are "good")
6272

6373
Checking quality
6474
- quality
75+
- quality [user]
6576

6677
#### Generosity
6778

6879
Generosity tracks which users are giving the most karma using the ++ commands. Every time a user issues such a command, their generosity value increases. This information will give a metric of who is using the system the most to increase other user's karma.
6980

7081
Checking generosity
7182
- generosity
83+
- generosity [user]
7284

7385
#### Scrambling username
7486

7587
Sometimes it is annoying to be mentioned in the above "leaderboard" style commands. The following command is a workaround that will "scramble" one's username so that they won't be notified in IRC.
7688

7789
- bamboo: scramble
7890

91+
#### Searching
92+
93+
Chipbot is able to search a couple different sites, including xkcd, and youtube. It will report
94+
the top result, including a link, to the channel. More can be added if requested.
95+
96+
Search youtube
97+
- .yt [search terms]
98+
99+
Search xkcd
100+
- .xkcd [search terms]
101+
102+
#### Quoting
103+
104+
Chipbot is able to remember single-line quotes.
105+
106+
Add a quote:
107+
- .quote [quote]
108+
109+
Post a quote:
110+
- .getquote (random)
111+
- .getquote [# of quote]
112+
113+
79114

bot.py

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@
4343
parser.add_argument("-k", "--karmafile", nargs='?', default=".karmascores")
4444
parser.add_argument("-a", "--statsfile", nargs='?', default=".stats")
4545
parser.add_argument("-z", "--scramblefile", nargs='?', default=".scrambles")
46+
parser.add_argument("-q", "--quotefile", nargs='?', default=".quotes")
47+
parser.add_argument("-u", "--userfile", nargs='?', default=".users")
4648
parser.add_argument("-d", "--debug", action="store_true")
4749
parser.add_argument("-g", "--generousfile", nargs='?', default=".generous")
4850
parser.add_argument("-t", "--tls", action="store_true")
4951
args = parser.parse_args(sys.argv[1:])
5052

5153
readbuffer = ""
5254
currentusers = []
55+
quotes = []
5356
shared_source = False
5457

5558
def loadData(object):
@@ -65,6 +68,20 @@ def loadData(object):
6568
generous = loadData(args.generousfile)
6669
scrambleTracker = loadData(args.scramblefile)
6770

71+
try:
72+
with open(args.quotefile, 'rb') as f:
73+
for line in f:
74+
quotes.append(line)
75+
except:
76+
quotes = []
77+
78+
try:
79+
with open(args.userfile, 'rb') as f:
80+
for line in f:
81+
currentusers.append(line[:-1])
82+
except:
83+
currentusers = []
84+
6885
# connect to the server
6986
s = socket.socket()
7087
if args.tls:
@@ -180,10 +197,31 @@ def politelyDoNotEngage(sender):
180197
response = "[AUTO REPLY] I am not a human, apologies for any confusion."
181198
sendTo(sender, response)
182199

200+
def helpMessage(sender):
201+
response = "README can be found here: https://github.com/vgmoose/bamboo"
202+
sendTo(sender, response)
203+
204+
def quoteMessage(sender, message):
205+
206+
if message == "" or message.split(' ') == []:
207+
return
208+
209+
message = message[1:]
210+
quotes.append(message)
211+
212+
with open(args.quotefile, 'a') as f:
213+
f.write(quotes[len(quotes)-1] + '\n')
214+
215+
response = "Quote #" + str(len(quotes)) + " added by " + sender
216+
sendTo(args.channel, response)
217+
183218
def anonSay(message):
184219
print message
185220
sendTo(args.channel, message)
186221

222+
def anonDo(message):
223+
s.send('PRIVMSG ' + args.channel + ' :\x01ACTION ' + message + '\x01\n')
224+
187225
# depends on git pull in shell while loop
188226
def updateBamboo():
189227
exit(0)
@@ -202,6 +240,14 @@ def youtube(searchTerm):
202240
if "http://www.youtube.com/watch" in result.url:
203241
return result.title + " " + result.url
204242

243+
def specificQuote(num):
244+
n = int(num) - 1
245+
if len(quotes) > n:
246+
return quotes[n]
247+
248+
def randomQuote():
249+
return quotes[random.randint(0, len(quotes)-1)]
250+
205251
# returns the response given a sender, message, and channel
206252
def computeResponse(sender, message, channel):
207253
global args
@@ -275,7 +321,7 @@ def computeResponse(sender, message, channel):
275321
return "%s has never used karma" % subject
276322

277323
# can't give yourself karma
278-
if subject == sender and symbol != "~~":
324+
if subject.lower() == sender.lower() and symbol != "~~":
279325
return
280326

281327
# if it's a user, give them karma, else give points to the phrase
@@ -408,6 +454,14 @@ def computeResponse(sender, message, channel):
408454
args.nick = message[len(args.nick)+7:].lstrip().rstrip()
409455
s.send(bytes("NICK " + args.nick + "\r\n"))
410456

457+
elif func == ".quote":
458+
return quoteMessage(sender, message[6:])
459+
460+
elif func == ".getquote":
461+
spltmsg = message.split(' ')
462+
if len(spltmsg) > 1:
463+
return specificQuote(spltmsg[1])
464+
return randomQuote()
411465

412466
while 1:
413467
# read in lines from the socket
@@ -429,16 +483,30 @@ def computeResponse(sender, message, channel):
429483

430484
# 353 = initial list of users in channel
431485
elif line[1] == "353":
432-
currentusers = [args.nick]
486+
if not args.nick in currentusers:
487+
currentusers.append(args.nick)
433488
newusers = line[6:]
434489
for u in newusers:
435490
u = u.lstrip("@").lstrip(":").lower()
436-
currentusers.append(u)
491+
if u[0] == "+":
492+
u = u[1:]
493+
if not u in currentusers:
494+
currentusers.append(u)
495+
with open(args.userfile, 'a') as f:
496+
f.write(u + '\n')
437497

498+
499+
438500
# update list of users when a nick is changed
439501
elif line[1] == "NICK":
440502
if not line[2] in currentusers:
441-
currentusers.append(line[2].lstrip("@").lstrip(":").lower())
503+
u = line[2].lstrip("@").lstrip(":").lower()
504+
if u[0] == "+":
505+
u = u[1:]
506+
if not u in currentusers:
507+
currentusers.append(u)
508+
with open(args.userfile, 'a') as f:
509+
f.write(u + '\n')
442510

443511
elif line[1] == "433":
444512
args.nick = line[2]
@@ -447,7 +515,11 @@ def computeResponse(sender, message, channel):
447515
elif line[1] == "JOIN":
448516
sender = parseSender(line)
449517
if not sender in currentusers:
450-
currentusers.append(parseSender(line).lstrip("@").lstrip(":").lower())
518+
u = parseSender(line).lstrip("@").lstrip(":").lower()
519+
if not u in currentusers:
520+
currentusers.append(u)
521+
with open(args.userfile, 'a') as f:
522+
f.write(u + '\n')
451523

452524
# this if statement responds to received messages
453525
elif line[1] == "PRIVMSG":
@@ -462,14 +534,27 @@ def computeResponse(sender, message, channel):
462534
splitmsg =message.split(' ')
463535
func = splitmsg[0]
464536
arglist = splitmsg[1:]
465-
if arglist == []:
466-
politelyDoNotEngage(sender)
467-
continue
468-
elif func == "say":
537+
538+
539+
if func == "update":
540+
updateBamboo()
541+
542+
elif func == "say" and arglist != []:
469543
anonSay(' '.join(arglist))
544+
545+
elif func == "action" and modflag and arglist != []:
546+
anonDo(' '.join(arglist))
547+
548+
elif func == "help":
549+
helpMessage(sender)
550+
551+
elif func == "quote" and arglist !=[]:
552+
quoteMessage(sender, ' '.join(arglist))
553+
470554
else:
471555
politelyDoNotEngage(sender)
472556
continue
557+
473558

474559
# decide what type of response to have based on the message
475560
response = computeResponse(sender, message, channel)

0 commit comments

Comments
 (0)